How to add buttons in a window using Windows API?

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.
😀

Replies

  • vishnu priya
    vishnu priya
    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);
  • gaurav.bhorkar
    gaurav.bhorkar
    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.

You are reading an archived discussion.

Related Posts

What ideas would you recommend for fixing the world's worst ever oil spill - BP Oil Spill? Waiting for ideas. Few of the ideas BP is considering (as far as...
hi every one,i m in urgent need of this one, please if anyone can help me, i will be grateful to you all people,.i need c compiler for 64 bit...
If you have strong determination no one can stop you to achieve what you want? chennai: There were many hurdles on Patricia Narayan's way to become an entrepreneur. She has...
BSNL (& MTNL) haven't been able to do much with early allotment of the 3G spectrum by government. BSNL has asked DoT for a full refund of INR. 10,186.58 crore...
I am going to take admission in 1st year of engineering this year. I am interested in pursuing engineering in Computer related field, but confused as there are many branches...