Thread: This is a last resort, could some1 please give me a brief tutorial about buttons

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question This is a last resort, could some1 please give me a brief tutorial about buttons

    I have read a load of tutorials/guides etc and still have real trouble with this, so as a last resort.
    I'm writing a program which is a chat program (like msn) and have got the menu sorted out. I made some buttons with CreateWindowEx(); but can't get them to do anything when you press them. I am hoping some1 will be able to explain this in a way that I will understand (dont make it too simple, I'm not a simpleton) either that or post a little code to help me.
    Thanks in advance


  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Buttons are just child windows, but I am sure you already know that. So when you create your child window you assign it an id unique to that window(button) right? That is important so you can distinguish them later on.

    Now when something happens to the button it sends the parent window a message. This message is a WM_COMMAND message, so all button messages will be trapped in here:
    Code:
    case WM_COMMAND:
         //All button messages
    So with that out of the way, we can now look at the actual parameters sent to the parent window with the WM_COMMAND message.

    WPARAM - this contains the child window ID that you assigned to it when you created the window. It is contained in the Lower word of this parameter. So to get the child window ID one would:
    Code:
    childWindowID = LOWORD(wParam);
    Now that we know what button the message is coming from we need to know what the actual message is, right? This is also contained in the WPARAM parameter, in the high word. So to view the actual message one would:
    Code:
    notificationCode = HIWORD(wParam);
    Now these notification codes are defined in the windows header files and are:
    Code:
    BN_CLICKED                    0
    BN_PAINT                      1
    BN_HILITE or BN_PUSHED        2
    BN_UNHILITE or BN_UNPUSHED    3
    BN_DISABLE                    4
    BN_DOUBLECLICKED or BN_DBLCLK 5
    BN_SETFOCUS                   6
    BN_KILLFOCUS                  7
    And finally the LPARAM parameter returns the handle to the child window sending the message. So if we wanted to capture when our button with ID = 1 was clicked we would:
    Code:
    case WM_COMMAND:
         if(LOWORD(wParam) == 1)
              if(HIWORD(wParam) == BN_CLICKED)
                   //Do something because I was clicked
         break;
    Here is a sample app that demonstrates the above mentioned steps to capture button messages:
    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){
    
        TCHAR title[] = TEXT("MY Button App");
        MSG msg;
        HWND hwnd;
        WNDCLASS wc;
    
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hInstance = hInstance;
        wc.lpfnWndProc = WndProc;
        wc.lpszClassName = title;
        wc.lpszMenuName = NULL;
        wc.style = CS_HREDRAW | CS_VREDRAW;
    
        RegisterClass(&wc);
    
        hwnd = CreateWindow(title, TEXT("My Button App"), WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                            NULL, NULL, hInstance, NULL);
    
        ShowWindow(hwnd, iCmdShow);
        UpdateWindow(hwnd);
        
        while(GetMessage(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        
        return (int)msg.wParam;
    }
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
    
        static HWND hwndButton;
        static int cxChar, cyChar;
    
        switch(message){
            case WM_CREATE:
                cxChar = LOWORD(GetDialogBaseUnits());
                cyChar = HIWORD(GetDialogBaseUnits());
    
                hwndButton = CreateWindow(TEXT("button"), //class name
                                          TEXT("MyPushButton"), //text displayed by button
                                          WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, //button style
                                          0, //xPos
                                          0, //yPos
                                          20*cxChar, //width
                                          7*cyChar/4 , //height
                                          hwnd, //parent window
                                          (HMENU)1, //child ID
                                          ((LPCREATESTRUCT)lParam)->hInstance, //instance handle
                                          NULL); //Extra parameters
                return 0;
            case WM_SIZE:
                
                MoveWindow(hwndButton, (LOWORD(lParam) / 2) - 10*cxChar, HIWORD(lParam) / 2, 20*cxChar, 7*cyChar/4, TRUE);
                break;
    
            case WM_COMMAND:
                if(LOWORD(wParam)==1)
                    if(HIWORD(wParam)==BN_CLICKED)
                        MessageBox(NULL, TEXT("Don't click so hard, that hurts"), TEXT("Button Message"), MB_ICONINFORMATION);
                break;
            case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    Last edited by andyhunter; 02-19-2005 at 06:47 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    thank you, that makes much more sense than all the guides that I read before.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 07:38 PM
  2. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  3. Grouping radio buttons
    By Bazz in forum Windows Programming
    Replies: 1
    Last Post: 08-28-2001, 07:15 AM