CrazyEngineers
  • How to add buttons in a window using Windows API?

    Updated: Oct 27, 2024
    Views: 9.0K
    I am a beginner in Windows API. I want to add a button to a window. Please share some light on Buttons in Windows API.
    😀
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • vishnu priya

    MemberJun 13, 2010

    I think this coding should help you as this one creates a button!

    HWND button =
    CreateWindow("BUTTON", /* this makes a "button" */"Class Options", /* this is the text which will appear in the button */WS_VISIBLE | WS_CHILD,5, /* these four lines are the position and dimensions of the button */60,105,20,hWnd, /* this is the buttons parent window */(HMENU)IDB_CLASS_OPTIONS, /* these next two lines pretty much tell windows what to do when the button is pressed */(HINSTANCE)GetWindowLong(insert, GWL_HINSTANCE),NULL);
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberJun 15, 2010

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        int wmId;
        
        switch (message)
        {
    
        [COLOR=DarkRed]case WM_COMMAND:
            wmId    = LOWORD(wParam);
            if (wmId == 1)
                MessageBox (hWnd, TEXT("Clicked"), TEXT("You Clicked!"), MB_OK);[/COLOR]
            break;
        
        [COLOR=Blue]case WM_CREATE:
                CreateWindow (TEXT ("BUTTON"), TEXT ("Click Me!"), WS_CHILD | WS_VISIBLE, 60, 80, 60, 20, hWnd, (HMENU) 1, NULL, NULL);[/COLOR]
                break;
    
    //--------further code------
    The blue code creates a button. Note that a button is also a window. The code given by Vishnu priya also creates a button and is similar to the blue code above. Notice the 'third last' parameter passed to the CreateWindow function, it is the ID passed in the WM_COMMAND message when the button is clicked. We created the button when our window receives a WM_CREATE message (this msg is received when the window is created)

    The brown code is the handler for button click. The ID is always present in the LOWORD of wParam. So check the Id (using if-else or switch-case) and do required tasks. Hence we have done this coding under case WM_COMMAND which our main window receives when the button is clicked.

    Note that the above code is just a snippet of Window Procedure. If you are familiar with basic windows programming, then you may understand what I said.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register