c++windowswinapibuttoncreatewindow

WinAPI create window + child windows, process a button press?


I'm new to WinApi and I'm looking to create a simple window inside my program containing a blank parent window and two smaller child buttons "button1" & "button2". with this button I'm hoping to change a bool value from false to true and visa versa, but nearly all the examples I have seen are quite hard to understand, it seems like you have to return an MSG value of some kind which I don't know how to process.

I have a pseudocode below of what I'm trying to do, I have left comments explaining what I want to do at each moment, am I going about it the right way?:

#include <windows.h>

static int buildwindow(){

    MSG msg;

    //create parent window
    HWND hWnd = CreateWindow(TEXT("scrollbar"), TEXT("Parent"), WS_VISIBLE | WS_POPUP,
    10, 10, 800, 500, NULL, NULL, NULL,  NULL);

    //create child window
    HWND hWnd1 = CreateWindow(TEXT("button"), TEXT("button1"), WS_CHILD|WS_VISIBLE | WS_POPUP,
    10, 10, 80, 25, hWnd, NULL, NULL,  NULL);

    //create child window2
    HWND hWnd2 = CreateWindow(TEXT("button"), TEXT("button2"), WS_CHILD|WS_VISIBLE | WS_POPUP,
    100, 100, 80, 25, hWnd, NULL, NULL,  NULL);

    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);
    ShowWindow(hWnd1, SW_SHOW);
    UpdateWindow(hWnd1);
    ShowWindow(hWnd2, SW_SHOW);
    UpdateWindow(hWnd2);

    //wait for buttonpress
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    //return the buttonpress
    return (int) msg.wParam;
}



int main(void)
{

   //create window inside the buttonpress method
   int buttonpress = buildwindow();

   //check which button was pressed
   if(buttonpress = button1){
      //do something
   }
   elseif(buttonpress = button2){
      //do something else
   }
   //finish
   return(0);

}

Solution

  • The message loop (GetMessage) won't end until a WM_QUIT message arrives.

    You need to implement callback functions for the button click events.

    I suggest reading more on button messages here:
    https://msdn.microsoft.com/en-us/library/windows/desktop/bb775941(v=vs.85).aspx