Thread: creating a button

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    creating a button

    I've looked everywhere, and done a weeks worth of searching and cannot find anything that tells me how to set up using a button. I've looked through MSDN, this site, and others. I'm trying to create an OK pushbutton that will just make a MessageBox() when clicked, for now. I don't know where to even begin, I thought i found what to do, but keep getting confused by something else MSDN says to use. I'm just beginning windows applications, but have done some console programs in the past. I'm using Devcpp compiler.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    You want to use CreateWindow() along with the appropriate class name which I think is "BUTTON" for a button, but I'm not exactly sure. As for handling the messages, you want to handle the WM_COMMAND message and I think the notification is BN_PRESSED or BN_CLICKED. Check out winprog.org. It's THE most recommended tutorial for starting out Win32 GUI programming.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Yes, check out http://www.winprog.org/ for a tut, and seeing how you have dev, there is a sample project (fully working project) that you can get on the init dialog that comes up after pushing the "New Project" item. Take a look at it, it might help, it did for me.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Ok so now I have a start. I created a radio button (even though i want a push button), but I can't figure out how to make it open a MessageBox when clicked.

    Code:
            CreateWindowEx(NULL, "BUTTON", "", WS_VISIBLE,
                150, 150, 50,50,hwnd, (HMENU)IDC_MAIN_EDIT, g_hInst, NULL);
    It's not moving the button to where I want, or making it the correct height/width. I also don't know how to link to the MessageBox. Is that first NULL correct? What should be placed there?

    That winprog.org site was a lot of help too, thanks.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Take another look at the winprog samples, you will find something similar to the next piece of code on the window main procedure (if the button is in a popup, you will have to search that on the popup window main procedure):

    Code:
    LRESULT CALLBACK mainproc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
    switch(msg)
        {
        ...
        case WM_COMMAND:
            {
            switch(LOWORD(wParam))
                    {
                    case IDC_MAIN_EDIT:
                        {
                        MessageBox();
                        }
                    break;
                    }
            }
        break;
    ...
    The notification of all standard bottonclicks will come via WM_COMMAND message; that message is send as UINT argument to the window procedure, and some information values are appended as LPARAM and WPARAM. You are searching for the 'WM_COMMAND' notification, so first check it uot from the 'UINT msg' argument. Once you have find it, you also know that with WM_COMMAND messages, the loworder word of the WPARAM is the idetifier of the control that have generated the message, so use the macro 'LOWORD()' to get the loworder and check if is the id of you button (in the code you posted should be 'IDC_MAIN_EDIT', if you find it just call the messagebox.

    Take a look at the clipped thread on that board for lots of tutorials, samples, etc...
    And hope that helps

    Niara

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Thanks Niara, that helped me understand it a lot more, but I still can't get it to work.

    Code:
    switch (message){
         case WM_PAINT:
              hDC = BeginPaint(hwnd,&paintstruct);
    			
              TextOut(hDC,0,0,string,sizeof(string));
    			
              CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "",
              WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
              150, 150, 50, 50, hwnd, (HMENU)IDC_MAIN_EDIT, g_hInst, NULL);
            	
              EndPaint(hwnd,&paintstruct);
              break;
              
         case WM_COMMAND:
              switch(LOWORD(wParam)){
    	case IDC_MAIN_EDIT:
    	     MessageBox(NULL,"Button pressed!!","HEY",NULL);
              }
              break;
         break;
    }
    Am I suppose to create the button inside WM_COMMAND or WM_PAINT? The button doesn't even show up when I try it in either.
    Last edited by scwizzo; 06-05-2007 at 04:16 PM.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yikes! Why would you create a new button every time you're told to paint?

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by MacGyver View Post
    Yikes! Why would you create a new button every time you're told to paint?
    Good question, i dont know (i feel stupid)

    So it should be created in COMMAND then? I still cannot get it to appear inside the window, and if I use CreateWindow() instead of CreateWindowEx() then my compiler says it is undeclared.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Try creating the button when your Windows callback proc receives a WM_CREATE message.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by MacGyver View Post
    Try creating the button when your Windows callback proc receives a WM_CREATE message.
    I LOVE YOU!!!

  11. #11
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Quote Originally Posted by scwizzo View Post
    Ok so now I have a start. I created a radio button (even though i want a push button), but I can't figure out how to make it open a MessageBox when clicked.

    Code:
            CreateWindowEx(NULL, "BUTTON", "", WS_VISIBLE,
                150, 150, 50,50,hwnd, (HMENU)IDC_MAIN_EDIT, g_hInst, NULL);
    This creates a floating window with a button in it, not a radio button.

  12. #12
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Quote Originally Posted by scwizzo View Post
    Ok so now I have a start. I created a radio button (even though i want a push button), but I can't figure out how to make it open a MessageBox when clicked.
    ...
    It's not moving the button to where I want, or making it the correct height/width. I also don't know how to link to the MessageBox. Is that first NULL correct? What should be placed there?
    Quote Originally Posted by scwizzo View Post
    That winprog.org site was a lot of help too, thanks.
    Which of these statements is a lie?
    If you truly read through winprog.org you wouldn't have to ask such spoon-feeding questions. Sit down, read it section-by-section from top to bottom, then see how helpful it really is.

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by @nthony View Post
    Which of these statements is a lie?
    If you truly read through winprog.org you wouldn't have to ask such spoon-feeding questions. Sit down, read it section-by-section from top to bottom, then see how helpful it really is.
    Who says either of them are a lie? Quit nitpicking my ignorance. Just because I said the site was a lot of help doesn't mean it helped me accomplish something. It helped me understand how stuff works.

  14. #14
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    You can look for yourself, and see that in the winprog tutorial about buttons, never is mentioned how to create a button.

  15. #15
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    http://www.winprog.org/tutorial/

    "App Part 1: Creating controls at runtime"

    http://www.winprog.org/tutorial/app_one.html

    Bottom of the page, "I'm not going to give examples of dynamically creating the other controls like LISTBOX, BUTTON, etc... because it's basically the same..."

    From the examples on that page (and MSDN) it helped me figure out some stuff for a button. Why do you guys have to be relentless, I wanted some help, got the help I needed, now let it be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Creating a function pointer from a button click event?
    By dxfoo in forum Windows Programming
    Replies: 20
    Last Post: 01-29-2008, 03:35 AM
  3. creating a button without a focus rect
    By X PaYnE X in forum Windows Programming
    Replies: 0
    Last Post: 05-11-2005, 10:16 PM
  4. Creating my own button
    By minesweeper in forum Windows Programming
    Replies: 1
    Last Post: 07-19-2002, 01:38 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM