CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2022
    Posts
    1

    Question How do I lock a button in window to the lower right corner.

    Hi
    I got a standard window to display with a button inside but I don't know how to lock the button to the corner.

    I put the code for the button in WM_CREATE:

    Code:
        case WM_CREATE:
            {
            HWND hwndButton = CreateWindow(
                L"BUTTON",  // Predefined class; Unicode assumed 
                L"OK",      // Button text 
                WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
                10,         // x position 
                10,         // y position 
                100,        // Button width
                100,        // Button height
                hWnd,     // Parent window
                NULL,       // No menu.
                (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
                NULL);      // Pointer not needed.
            }
            break;
    Now I am trying to put the button in the lower right corner with GetWindowRect and SetWindowPos but I don't know how to can anybody help?

    Code:
        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hWnd, &ps);
                
                BOOL GetWindowRect(
                    HWND   hWnd,
                    LPRECT lpRect
                );
    
                //Trying to set the Variables for SetWindowPos so the button is always in the lower right corner
                X = lpRect.right - ButtonHSize - ButtonRightPadding
                Y = lpRect.bottom - ButtonVSize - ButtonBottomPadding
                cx = lpRect.right - ButtonRightPadding
                cy = lpRect.bottom - ButtonBottomPadding
    
                BOOL SetWindowPos(
                    [in]           HWND hwndButton,
                    NULL,
                    [in]           int  X,
                    [in]           int  Y,
                    [in]           int  cx,
                    [in]           int  cy,
                    [in]           UINT uFlags
                );
    
                EndPaint(hWnd, &ps);
            }
            break;
    Is that the right spot for that code and how do I make it work?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How do I lock a button in window to the lower right corner.

    1. case WM_PAINT is the wrong place for moving the child control position. You have to use the case WM_SIZE.
    2. you have to read/learn the Microsoft docs (including the samples) to understand how the all this process with windows works. See, for instance this article: https://docs.microsoft.com/en-us/win.../using-windows
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured