Thread: Button positioning

  1. #61
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Unfortunately, I just couldn't get it to work with just CreateWindow (it worked most of the time, but not all). I'm sorry.

  2. #62
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This is an example from Dev-C++:
    Code:
    /*
     * A basic example of Win32 programmiwng in C.
     *
     * This source code is in the PUBLIC DOMAIN and has NO WARRANTY.
     *
     * Colin Peters <[email protected]>
     */
    
    #include <windows.h>
    #include <string.h>
    #include <iostream>
    
    /*
     * This is the window function for the main window. Whenever a message is
     * dispatched using DispatchMessage (or sent with SendMessage) this function
     * gets called with the contents of the message.
     */
    LRESULT CALLBACK
    MainWndProc (HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
    {
    /* The window handle for the "Click Me" button. */
    static HWND hwndButton = 0;
    static int cx, cy;/* Height and width of our button. */
    
    HDC hdc;/* A device context used for drawing */
    PAINTSTRUCT ps;/* Also used during window drawing */
    RECT rc;/* A rectangle used during drawing */
    /*
     * Perform processing based on what kind of message we got.
     */
    switch (nMsg)
    {
    case WM_CREATE:
    {
    /* The window is being created. Create our button
     * window now. */
    TEXTMETRIC tm;
    
    /* First we use the system fixed font size to choose
     * a nice button size. */
    hdc = GetDC (hwnd);
    SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
    GetTextMetrics (hdc, &tm);
    cx = tm.tmAveCharWidth * 30;
    cy = (tm.tmHeight + tm.tmExternalLeading) * 2;
    ReleaseDC (hwnd, hdc);
    
    /* Now create the button */
    hwndButton = CreateWindow (
    "button",/* Builtin button class */
    "Click Here",
    WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
    0, 0, cx, cy,
    hwnd,/* Parent is this window. */
    (HMENU) 1,/* Control ID: 1 */
    ((LPCREATESTRUCT) lParam)->hInstance,
    NULL
    );
    
    return 0;
    break;
    }
    
    case WM_DESTROY:
    /* The window is being destroyed, close the application
     * (the child button gets destroyed automatically). */
    PostQuitMessage (0);
    return 0;
    break;
    
    case WM_PAINT:
    /* The window needs to be painted (redrawn). */
    hdc = BeginPaint (hwnd, &ps);
    GetClientRect (hwnd, &rc);
    
    /* Draw "Hello, World" in the middle of the upper
     * half of the window. */
    rc.bottom = rc.bottom / 2;
    DrawText (hdc, "Hello, World!", -1, &rc,
    DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    
    EndPaint (hwnd, &ps);
    return 0;
    break;
    
    case WM_SIZE:
    /* The window size is changing. If the button exists
     * then place it in the center of the bottom half of
     * the window. */
    if (hwndButton &&
    (wParam == SIZEFULLSCREEN ||
     wParam == SIZENORMAL)
       )
    {
    rc.left = (LOWORD(lParam) - cx) / 2;
    rc.top = HIWORD(lParam) * 3 / 4 - cy / 2;
    MoveWindow (
    hwndButton,
    rc.left, rc.top, cx, cy, TRUE);
    }
    break;
    
    case WM_COMMAND:
    /* Check the control ID, notification code and
     * control handle to see if this is a button click
     * message from our child button. */
    if (LOWORD(wParam) == 1 &&
        HIWORD(wParam) == BN_CLICKED &&
        (HWND) lParam == hwndButton)
    {
    /* Our button was clicked. Close the window. */
    DestroyWindow (hwnd);
    }
    return 0;
    break;
    }
    
    /* If we don't handle a message completely we hand it to the system
     * provided default window function. */
    return DefWindowProc (hwnd, nMsg, wParam, lParam);
    }
    
    
    int STDCALL
    WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
    {
    HWND hwndMain;/* Handle for the main window. */
    MSG msg;/* A Win32 message structure. */
    WNDCLASSEX wndclass;/* A window class structure. */
    char*szMainWndClass = "WinTestWin";
    /* The name of the main window class */
    
    /*
     * First we create a window class for our main window.
     */
    
    /* Initialize the entire structure to zero. */
    memset (&wndclass, 0, sizeof(WNDCLASSEX));
    
    /* This class is called WinTestWin */
    wndclass.lpszClassName = szMainWndClass;
    
    /* cbSize gives the size of the structure for extensibility. */
    wndclass.cbSize = sizeof(WNDCLASSEX);
    
    /* All windows of this class redraw when resized. */
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    
    /* All windows of this class use the MainWndProc window function. */
    wndclass.lpfnWndProc = MainWndProc;
    
    /* This class is used with the current program instance. */
    wndclass.hInstance = hInst;
    
    /* Use standard application icon and arrow cursor provided by the OS */
    wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
    
    /* Color the background white */
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    
    /*
     * Now register the window class for use.
     */
    RegisterClassEx (&wndclass);
    
    /*
     * Create our main window using that window class.
     */
    hwndMain = CreateWindow (
    szMainWndClass,/* Class name */
    "Hello",/* Caption */
    WS_OVERLAPPEDWINDOW,/* Style */
    CW_USEDEFAULT,/* Initial x (use default) */
    CW_USEDEFAULT,/* Initial y (use default) */
    CW_USEDEFAULT,/* Initial x size (use default) */
    CW_USEDEFAULT,/* Initial y size (use default) */
    NULL,/* No parent window */
    NULL,/* No menu */
    hInst,/* This program instance */
    NULL/* Creation parameters */
    );
    
    /*
     * Display the window which we just created (using the nShow
     * passed by the OS, which allows for start minimized and that
     * sort of thing).
     */
    ShowWindow (hwndMain, nShow);
    UpdateWindow (hwndMain);
    
    /*
     * The main message loop. All messages being sent to the windows
     * of the application (or at least the primary thread) are retrieved
     * by the GetMessage call, then translated (mainly for keyboard
     * messages) and dispatched to the appropriate window procedure.
     * This is the simplest kind of message loop. More complex loops
     * are required for idle processing or handling modeless dialog
     * boxes. When one of the windows calls PostQuitMessage GetMessage
     * will return zero and the wParam of the message will be filled
     * with the argument to PostQuitMessage. The loop will end and
     * the application will close.
             */
    while (GetMessage (&msg, NULL, 0, 0))
    {
    TranslateMessage (&msg);
    DispatchMessage (&msg);
    }
    return msg.wParam;
    }

  3. #63
    Registered User
    Join Date
    May 2005
    Posts
    207
    Quote Originally Posted by Dante Shamest
    Unfortunately, I just couldn't get it to work with just CreateWindow (it worked most of the time, but not all). I'm sorry.
    Microsoft is so consistently inconsistent...

    mw
    Blucast Corporation

  4. #64
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Would it be okay for you then to just use ShowWindow?

  5. #65
    Registered User
    Join Date
    May 2005
    Posts
    207
    I guess so.

    It looks like the ShowWindow (maximized) has to be used in conjunction with CreateWindow (maximized) to get the centering to work properly. I guess that "makes sense" but it sure is a dirty trick for them to play on programmers.

    If I find a different method that's more logical I'm sure I'll post it! :-)

    mw
    Blucast Corporation

  6. #66
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    A basic example of Win32 programmiwng in C.

    Code:
    while (GetMessage (&msg, NULL, 0, 0))
    {
    TranslateMessage (&msg);
    DispatchMessage (&msg);
    }
    return msg.wParam;
    }
    Indentation is a virtue . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #67
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Looks like you are getting this working...


    Got to be careful with the windows defined values. A simple typo can be all it takes..........

    >>ShowWindow (hwndMain, WS_MAXIMIZE);

    This will not work.

    WS_MAXIMIZE is a STYLE (and defined as 0x01000000L != 3)

    SW_MAXIMISE or SW_SHOWMAXIMISED is what you want (both are defined as three).
    "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

  8. #68
    Registered User
    Join Date
    May 2005
    Posts
    207
    I can at least get the window to open in full screen by adjusting the nWidth and nHeight values.

    Can the height/width values for "WS_MAXIMIZE" be defined and plugged into those variables?

    mw
    Blucast Corporation

  9. #69
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    If the window covers the whole screen, you can just calculate the width and height of the screen using the methods on page 1 of this topic.

  10. #70
    Registered User
    Join Date
    May 2005
    Posts
    207
    Yeah, I've already managed to display the window full screen; but you said that "full screen" and "maximized" are different and I was wondering if "maximized" can be translated into "height and width".

    If yes, then I can force a maximized effect like this:

    hwndMain = CreateWindow (
    szMainWndClass,
    "Hello",
    WS_OVERLAPPED,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    WS_MAXIMIZED_x_value,
    WS_MAXIMIZED_y_value,
    NULL,
    NULL,
    hInst,
    NULL
    );

    If "WS_MAXIMIZED" can be defined in terms of "height" and "width", then I don't need to use a style to make a maximized window. When you specify a specific height and width, then Windows has NO CHOICE but to display the window in that size.

    mw
    Blucast Corporation

  11. #71
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Quote Originally Posted by Lionmane
    but you said that "full screen" and "maximized" are different
    Do whatever looks good to you (or to your boss).

  12. #72
    Registered User
    Join Date
    May 2005
    Posts
    207
    This was all me. I was still trying to put the smackdown on Microsoft.

    mw
    Blucast Corporation

  13. #73
    Registered User
    Join Date
    May 2005
    Posts
    207
    I've figured some stuff out about CreateWindow and ShowWindow!

    If you specify the height/width of your window during CreateWindow (including CW_USEDEFAULT), then that overrides anything else you use to effect the size of your window.

    In other words: if you put WS_MAXIMIZE as your window style, and then put CW_USEDEFAULT for your window height and width then your window will be default sized. Period.

    ShowWindow (hwnd, SW_SHOWMAXIMIZED) will "override" the heigh/width arguments in CreateWindow but it DOES NOT change the window. It works kind of like StretchBitmap: it will stretch your window when it shows it, but your window is still the size specified in height/width. This means that if you try to position your buttons, you'll be positioning them according to the height/width (even though you specified maximized in ShowWindow).

    Also, ShowWindow is not necessary in my program. CreateWindow shows them just fine.

    mw
    Blucast Corporation

  14. #74
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    ShowWindow (hwnd, SW_SHOWMAXIMIZED) will "override" the heigh/width arguments in CreateWindow but it DOES NOT change the window.
    Are you sure? Or are you just not repositioning your controls after they're maximized? Did you calculate the controls before ShowWindow, or after?

    Edit: I've performed a little experiment to see if ShowWindow (hwnd, SW_SHOWMAXIMIZED) changes the window size. All I did was print the dimensions of the window before calling the function, and printing them again after calling the function. The window size does seem to change.
    Last edited by Dante Shamest; 10-20-2005 at 10:59 AM.

  15. #75
    Registered User
    Join Date
    May 2005
    Posts
    207
    How do you display text and values so casually?!

    I had to look at the screen and guess! :-)

    mw
    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