-
Calculating window sizes
Hi,
I've always have difficulties calculating the sizes of windows. Now I know that I can use AdjustWindowRect() for it. But what to do if you want to resize a listview or a combobox? Especially wen the integral height styles are on.
With the combobox, I have a rather small combobox, with longer strings in it. I tried calculating the maximum string length (with GetTextExtentPoint32()), but you have to adjust the width for the borders. And I don't know how.
Also the calculation has to work with different font settings, and sometimes that's also hard.
With the height of the combobox I also had some difficulties, but I think I solved it with this calculation (but again, I don't know the border with or something, because I'm always 2 off, so I always add 2)
This is for 7 items in the list view section...
Code:
GetWindowRect(GetDlgItem(hDlg, IDC_ONCE), &rCmbBox);
width = rCmbBox.right - rCmbBox.left;
height = SendDlgItemMessage(hDlg, IDC_ONCE, CB_GETITEMHEIGHT, 0 , 0);
height *= 7;
height += rCmbBox.bottom - rCmbBox.top + 2;
SetWindowPos(GetDlgItem(hDlg, IDC_ONCE) , NULL, 0, 0, width, height,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
Is there a better way to do this? (and a general way to calculate text width? with border + margin)
Joren
-
If you are trying to calculate the size so you can put strings inside the combo try
GetClientRect()
as this will give you the internal size not the size including the edges. You can more accurately fiind the borders (they are roughly the differnce between the window rect and the client rect)
PS I have had problems with SetWindowPos(). If it hits a flag for the first option (hWndInsertAfter) it ignores ALL the other param's including the last flag (uFlags). This means it is not possible to set the zorder and style at the same time. Or use a zorder flag and the position or width or height.
-
I don't know about your problems with the SetWindowPos, because I've never used the first flag. If I want to change the Z-order I just use SetForeGroundWindow or something.
I know I can get the client rect (btw is that the client rect of the edit field of the combo box or the client rect of the list box) but I want to know the width of the strings I want to add, so I can adjust the client rect of the combo box
Joren
-
I use ListView_GetColmnWidth() and height to calulate the size.
I do this in the paint function in response to a WM_DRAWITEM call. I then have the HDC inside the DRAWITEMSTRUCT struct to work with so I can test the string size with the correct font in the HDC. Needs OWNERDRAW on.
You could also look at ListView_GetViewRect()