Thread: Calculating window sizes

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    41

    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

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    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.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    41
    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

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    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()
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM