Thread: Button positioning

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    207

    Button positioning

    Hi! I'm trying to position my buttons on the screen based on the 1) video resolution and 2) the size of the window (maximized, etc).

    I wanted to have four buttons on the bottom half of the screen. Two centered on the left side and two centered on the right.

    I understand how to draw the buttons with "hard coded" coordinates, but I don't even know where/how to begin to draw them any other way.

    01) How do I get the CURRENT resolution setting?
    02) How do I detect the current size of the client area?
    03) How do I cram this information into my button creation command? I assume it's a formula, but how do you calculate the position of something for a small area of the screen?

    mw
    Blucast Corporation

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    01) How do I get the CURRENT resolution setting?
    Code:
      int screenWidth  = GetSystemMetrics(SM_CXSCREEN) ;
      int screenHeight = GetSystemMetrics(SM_CYSCREEN) ;
    02) How do I detect the current size of the client area?
    GetClientRect

    03) How do I cram this information into my button creation command? I assume it's a formula, but how do you calculate the position of something for a small area of the screen?
    Perform the calculations, then pass the values to CreateWindow(Ex). If the size of your window changes, do the same calculations in WM_SIZE and call SetWindowPos to update the positions of your buttons.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    207
    Ok, I'm really starting to hate buttons now! :-)

    I created my buttons "on the fly" (ie: in my Window Procedure) and it doesn't seem to be well suited to what I want to do.

    I tried to declare the child buttons normally, but I can't seem to make it fit.

    Here's my parent window:

    Code:
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon (GetModuleHandle (NULL), MAKEINTRESOURCE (IDI_BOOTICON));
    wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;
    Please note that I'm using WNDCLASS and *not* WNDCLASSEX.

    Here's the child button that I created on the fly:

    Code:
    mainButtons = CreateWindow
    (
    TEXT ("button"),
    TEXT ("New Game"),
    WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
    300, 500,
    110, 30,
    hwnd,
    (HMENU) IDB_NEWGAMEBUTTON,
    GetModuleHandle (NULL),
    NULL
    );
    How do I make a button child window for the parent window?

    mw
    Blucast Corporation

  4. #4
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    How do I make a button child window for the parent window?
    I'm sorry, I don't understand. Didn't you just post the code to create the button?

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    207
    Yeah, but the code to create the button was in WM_CREATE. The code to declare the parent window is in WinMain.

    Can you declare a child window in the WinMain function or do you have to put it in WM_CREATE?

    mw
    Blucast Corporation

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> Can you declare a child window in the WinMain function <<

    Yes. So long as you've first created the parent window, of course!

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    207
    How do I declare the button in WinMain?

    At first I simply tried to plug the information from the "CreateWindow" command in WM_CREATE for my button into a window declaration in WinMain:

    Code:
    WNDCLASS wndclass, btnclass;
    
    //parent window
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon (GetModuleHandle (NULL), MAKEINTRESOURCE (IDI_BOOTICON));
    wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;
    
    //button window
    btnclass.style = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON
    btnclass.lpfnWndProc = BtnProc;
    btnclass.cbClsExtra = 0;
    btnclass.cbWndExtra = 0;
    btnclass.hInstance = hInstance;
    btnclass.hIcon = LoadIcon (GetModuleHandle (NULL), MAKEINTRESOURCE (IDI_BOOTICON));
    btnclass.hCursor = LoadCursor (NULL, IDC_ARROW);
    btnclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    btnclass.lpszMenuName = NULL;
    btnclass.lpszClassName = szBtnName;
    But this doesn't look right...

    mw
    Blucast Corporation

  8. #8
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Unless I have a reason or need I just use my already registered window class for my button as well. Like this:
    Code:
    MSG msg;
    HWND hwnd,hButton;
    WNDCLASS wc;
    static TCHAR szAppName[]=TEXT("Window");
    
    wc.style=CS_HREDRAW|CS_VREDRAW;
    wc.cbWndExtra=0;
    wc.cbClsExtra=0;
    wc.hInstance=hInstance;
    wc.lpfnWndProc=WndProc;
    wc.hbrBackground=(HBRUSH)(COLOR_WINDOW);
    wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wc.hCursor=LoadCursor(NULL,IDC_ARROW);
    wc.lpszMenuName=NULL;
    wc.lpszClassName=szAppName;
    
    if(!RegisterClass(&wc))
    {
    MessageBox(NULL,"Error registering WNDCLASS!","ERROR",MB_ICONERROR);
    return 0;
    }
    
    hwnd=CreateWindow(szAppName,szAppName,WS_POPUPWINDOW|WS_CAPTION|WS_MINIMIZEBOX,350,150,300,210,NULL,NULL,hInstance,NULL);
    hButton=CreateWindow(TEXT("BUTTON"),TEXT("Button"),WS_CHILD|WS_VISIBLE,130,50,100,23,hwnd,NULL,hInstance,NULL);
    I took this code from a calculator I made a while back(I made a few changes though), so that's why it's a popup window and positioned the way it is.
    Last edited by jmd15; 10-09-2005 at 08:11 PM.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    207
    Oh wow! That was SO EASY! Thanks!

    So all I had to do was move the "Create Window" command to my WinMain area.

    How do I tell what code I can put in the WinMain function and what I must put in the window procedure? I thought WinMain was where you declared the initial window, but then you had to put everything that deals with the window in the windows procedure...

    For instance:

    I tried to put my CreateWindow (for my button) in WM_PAINT. I figured that since I'm using GetClientRect there already I may as well do my buttons too. You can probably imagine what happened...

    mw
    Last edited by Lionmane; 10-09-2005 at 10:41 PM.
    Blucast Corporation

  10. #10
    Registered User
    Join Date
    May 2005
    Posts
    207
    Ok, this is what I've got so far on positioning my buttons. I have 4 buttons that are each 30 pixels high and 110 pixels wide.

    I want to position them on the bottom half of the screen, centered vertically and horizontally:

    Code:
    GetClientRect (hwnd, &rect);
    
    top_left_button_x = (rect.right - (2 * btnWidth)) / 3;
    top_right_button_x = rect.right - ((rect.right - (2 * btnWidth)) / 3) - btnWidth;
    Right now I'm only trying to center them horizontally.

    Both buttons are positioned too far to the left.

    It's supposed to display something like:

    (spaces) button (spaces) button (spaces)

    mw
    Last edited by Lionmane; 10-10-2005 at 12:01 AM.
    Blucast Corporation

  11. #11
    Registered User
    Join Date
    May 2005
    Posts
    207
    Ok, I'm thinking that GetClientRect is broken just like LoadBitmap. I'm fairly certain my calculations are correct, but my buttons are still off-centered to the left.

    Is my client area affected by my stretching a bitmap over it?

    mw
    Blucast Corporation

  12. #12
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    GetClientRect works just fine. I use it all the time. Stretching a bitmap over a client area should not affect it's size at all.

  13. #13
    Registered User
    Join Date
    May 2005
    Posts
    207
    Ok, I figured out what's going on with centering my buttons. For some reason my client area doesn't extend to the edge of the window.

    My RECT.right value is 762. To find out where on the screen that is located, I subtracted the width of my button from that (110) and drew my button from that starting point.

    The button was drawn about three quarters of the way across the window.

    What can cause this?

    mw
    Blucast Corporation

  14. #14
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    You're drawing the button inside the window right? When you create a button with CreateWindow, you're using client coordinates of the main window. So don't you just need to know how big your window's client area is? Why do you need to care how big the screen is?

  15. #15
    Registered User
    Join Date
    May 2005
    Posts
    207
    Oops!

    Please substitute the words "client area" for every use of "screen" that I made in the previous post.

    My parent window is maximized, and I'm attempting to print my buttons ONLY on my window's client area.

    When I attempt to draw my button on the right edge of the client area, it draws the button three quarters of the way across instead of at the edge of the window.

    mw
    Last edited by Lionmane; 10-10-2005 at 05:26 PM.
    Blucast Corporation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-28-2008, 08:30 PM
  2. elliptical button
    By geek@02 in forum Windows Programming
    Replies: 0
    Last Post: 11-21-2006, 02:15 AM
  3. Remove dialog button border
    By RedZone in forum Windows Programming
    Replies: 4
    Last Post: 08-21-2006, 01:20 PM
  4. writing text over a deleted button
    By algi in forum Windows Programming
    Replies: 4
    Last Post: 05-02-2005, 11:32 AM
  5. Window won't display on button command!?
    By psychopath in forum Windows Programming
    Replies: 6
    Last Post: 06-22-2004, 08:12 PM