#include "alfpriv.h" /* BUTTON */ static LRESULT CALLBACK ALF__ButtonSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) { (void)uIdSubclass; (void)dwRefData; if (uMsg == ALF_WM_QUERYSIZE) { HDC hdc = GetDC(hwnd); HFONT font = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0); HFONT oldFont = 0; if (font) oldFont = SelectFont(hdc, font); // calc drawtext style DWORD style = GetWindowLong(hwnd, GWL_STYLE); UINT format = DT_LEFT | DT_EXPANDTABS | DT_CALCRECT; if ((style & BS_MULTILINE) == 0) format |= DT_SINGLELINE; RECT r = { 0, 0, 0x7FFFFFFF, 100 }; int textlen = GetWindowTextLength(hwnd); TCHAR *textbuf = ALF_New(TCHAR, textlen + 1); GetWindowText(hwnd, textbuf, textlen+1); DrawText(hdc, textbuf, -1, &r, format); ALF_Free(textbuf); int xpadding = ALF_Compat_GetSystemMetricsForDpi(SM_CXEDGE, ALF_CentipointsToPixels(GetParent(hwnd), 7200)) * 2 + 6; int ypadding = ALF_Compat_GetSystemMetricsForDpi(SM_CYEDGE, ALF_CentipointsToPixels(GetParent(hwnd), 7200)) * 2 + 4; SIZE *pSize = (SIZE*)(void*)lParam; if (pSize->cx < r.right - r.left + xpadding) { pSize->cx = r.right - r.left + xpadding; } if (pSize->cy < r.bottom - r.top + ypadding) { pSize->cy = r.bottom - r.top + ypadding; } if (pSize->cx < pSize->cy) { pSize->cx = pSize->cy; } if (font) SelectFont(hdc, oldFont); ReleaseDC(hwnd, hdc); } else if (uMsg == WM_DESTROY) { ALF_Compat_RemoveWindowSubclass(hwnd, ALF__ButtonSubclassProc, 0); } return ALF_Compat_DefSubclassProc(hwnd, uMsg, wParam, lParam); } HWND ALF_AddButton(HWND win, WORD id, UINT x, UINT y, const TCHAR *text) { HWND hwndButton = CreateWindowEx(0, TEXT("BUTTON"), text, WS_CHILD | WS_TABSTOP | WS_VISIBLE | BS_PUSHBUTTON | BS_MULTILINE, 0, 0, 100, 100, win, (HMENU)(int)id, (HINSTANCE)GetWindowLongPtr(win, GWLP_HINSTANCE), NULL); ALF_Compat_SetWindowSubclass(hwndButton, ALF__ButtonSubclassProc, 0, 0); SetWindowPos(hwndButton, NULL, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED); ALFWidgetLayoutParams p; ZeroMemory(&p, sizeof(p)); p.hwnd = hwndButton; p.x = x; p.y = y; p.width = 0; p.height = 0; p.flags = ALF_QUERYSIZE | ALF_MESSAGEFONT; ALF_AddWidgetEx(win, &p); return hwndButton; } void ALF_SetDefaultButton(HWND win, WORD id) { SendMessage(win, DM_SETDEFID, (WPARAM)id, 0); }