Thread: getting the size of the window

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    157

    getting the size of the window

    i started with the main window and i simply want to place a pushbutton at the bottom left hand corner of the window. and i was going to use a function to get the width/length of the window (NOT the screen) so i can calculate exactly where i want the button to be placed instead of using just hard coded numbers (pixel values).

    i know of GetSystemMetrics(SM_CXSCREEN) and GetSystemMetrics(SM_CYSCREEN) to get the total width and length of the entire screen, but i just want the width and lenght of the main window i'm working on.

    is there a function to do this? if so how is it implemented?

    thanks.

  2. #2

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    so i should just use variable type RECT to get from the function such as used like:

    Code:
    GetClientRect(hwnd, clientrect);
    and clientrect would be defined as such

    Code:
    RECT clientrect;
    right? and what would i do with this RECT structure afterwards? for instance, what kind of units are they in? why would you want teh upper right and left values, because wouldnt they be zero because it's the top?


  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    OK, handle the WM_SIZE message. The width/height of client area are passed in the lParam.
    Code:
    clientWidth = LOWORD(lParam);  // width of client area 
    clientHeight = HIWORD(lParam); // height of client area
    After that call MoveWindow on your button.
    Code:
    // buttonWidth, buttonHeight previously declared.
    MoveWindow( hWndButton, /*handle to button*/
                            30, /*pixels from left side of window*/
                            clientHeight - buttonHeight - 30, /* distance from bottom*/
                            buttonWidth,
                            buttonHeight,
                            1, /*tell it to redraw the window*/ );
    Hope that helps.
    -Futura
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Sorry, I responded kinda quick To answer your questions:

    >RECT clientrect;
    >GetClientRect(hwnd, clientrect);

    This won't work because GetClientRect expects the memory address of a rect. Do this:
    Code:
    GetClientRect(hwnd, &clientrect);

    >what would i do with this RECT structure afterwards? for instance, what kind of units are they in? why would you want teh upper right and left values, because wouldnt they be zero because it's the top?

    You don't need the left / top values - they will always be set to zero anyways the other two values will be the width and height of the window's client area in pixels.

    Hope at least one of my posts helps...

    -Futura
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    You can call GetWindowRect() to get the screen coordinates of the main window. Then based on these values you can set the coords of your button.

    For example, imagine the coords of your main dialog are: <50, 75, 300, 400> and that corresponds to <left, top, right, bottom>. After doing this:
    Code:
    RECT rect;
    GetWindowRect(hwnd, &rect);
    The rect struct will be filled with those values.

    Then figure out the coords for your button. If you want it to be 40 units tall and 70 units wide at 5 units away from either border do this:
    Code:
    int height = 40;
    int width = 70;
    int buf = 5;
    
    RECT btnRect;
    btnRect.bottom = rect.bottom - buf;
    btnRect.top = btnRect.bottom - height;
    btnRect.left = rect.left + buf;
    btnRect.right = btnRect.left + width;
    Now you have a rect containing the coords of your button. Keep in mind I wouldn't code anything like this (i mean the way I created variables just to do the addition with), but it's for clarity.

    HTH.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    okay, thanks all! i figured it was just easier to handle the wm_size message so that's what i ended up doing. thanks again!

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Lucky, your method may not work as well as using GetClientRect(), because GetWindowRect() will give you screen co-ordinates, whereas the idea here is to get dimensions relative to (0,0) of the client area. Using GetWindowRect() will not account for borders, menus, captions, etc.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    You're absolutely right. Thanks for the correction!

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    No problem. The best way to learn is to make mistakes! That's what the CBoard is all about really.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 05:55 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM