Thread: (Ken Fitlike) buttons

  1. #1
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    (Ken Fitlike) buttons

    I'm trying to use command buttons in my window as described by Ken (by the way, great site, Ken!) and I need to know what the WPARAM and LPARAM values represent when clicking on the buttons.
    Here's the code:

    Code:
    //in my message handling procedure
    switch(umsg)
    {
     // I create the buttons
    case WM_CREATE:
     {
      CreateWindowEx(0,"Button","Button1",WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 245,5,60,30,hwnd,NULL,hInstMain,NULL);
      CreateWindowEx(0,"Button","Button2",WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 245,150,60,30,hwnd,NULL,hInstMain,NULL);
     return(0);
    }break;
    //the UINT that represents clicking on a button
    case 273:
    {
      //some simple debugging stuff to get the return values
      int Num1=LOWORD(lparam), Num2=HIWORD(lparam);
      char buff1[20], buff2[20], cSep[5]=" , ";
      _itoa(Num1,buff1,10);
      _itoa(Num2,buff2,10);
      strcat(buff1,cSep);
      strcat(buff1,buff2);
      MessageBox(NULL,buff1,"lparam test",NULL);
    This returns 302 for Num1(LOWORD(lparam)) for Button1 and 294 for Num1 for Button2, and for Num2 it returns the same number for both of them. Though each time I run the program it returns one number higher than the last (right now I'm on 44). The none of the numbers match the x,y coords or size of the buttons.
    Anyone know what they represent? Thanks in advance for you help.

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    First you shouldnt use magic number!!!!!!!! That means dont use case 273: , use cas WM_COMMAND: b/c if you dont your not going to get people to help you because we dont have time to decrypt your program for you. Next when your trying to find out what "button" caused the message to be triggered you do LOWORD(wParam) and this is the HANDLE to the "button", then you do HIWORD(wParam) to find out what "button" message it was such as BN_DBLCLCK, BN_CLICKED and so forth.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  3. #3
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by xds4lx
    First you shouldnt use magic number!!!!!!!!
    Sorry. I didn't know what the constant for that message was. I found out if was 273, by doing this:

    Code:
    //inside my message handler
    char buff[20];
    _itoa(umsg,buff,10);
    MessageBox(NULL,buff,"Message Numbers:",NULL);
    Which of course meant I went through a ton of messages before I could even see the window on the screen! Sorry for my ignorance, I'm still new to Windows programming with C++.
    Thanks for the quick answer.
    Last edited by jdinger; 03-15-2002 at 12:14 PM.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by xds4lx
    Next when your trying to find out what "button" caused the message to be triggered you do LOWORD(wParam)
    Actually the LOWORD(wParam) doesnt give you the handle...it gives you a control specific identifier.......lParam gives you the hwnd

    Code:
    #include <windows.h>
    
    #define ID_BUT1 40000
    #define ID_BUT2 40001
    
    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
    
    char szWinName[] = "MyWin"; 
    HINSTANCE hInstMain;
    
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, 
                       LPSTR lpszArgs, int nWinMode)
    {
      HWND hwnd;
      MSG msg;
      WNDCLASSEX wcl;
    
      hInstMain = hThisInst;
     
      wcl.cbSize = sizeof(WNDCLASSEX); 
      wcl.hInstance = hThisInst; 
      wcl.lpszClassName = szWinName;
      wcl.lpfnWndProc = WindowFunc; 
      wcl.style = 0; 
      wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
      wcl.hIconSm = LoadIcon(NULL, IDI_WINLOGO); 
      wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
      wcl.lpszMenuName = NULL; 
      wcl.cbClsExtra = 0;
      wcl.cbWndExtra = 0; 
      wcl.hbrBackground = (HBRUSH) GetStockObject(COLOR_3DFACE +1); 
    
     
      if(!RegisterClassEx(&wcl)) return 0;
    
     
      hwnd = CreateWindow(szWinName,"Buttons",WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, 
        HWND_DESKTOP,NULL, hThisInst,NULL);
    
    
      ShowWindow(hwnd, nWinMode);
      UpdateWindow(hwnd);
    
     
      while(GetMessage(&msg, NULL, 0, 0))
      {
        TranslateMessage(&msg);
        DispatchMessage(&msg); 
      }
      return msg.wParam;
    }
    
    
    LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,
                                WPARAM wParam, LPARAM lParam)
    {
    	static HWND hBut1,hBut2;
      switch(message) {
    	case WM_CREATE:
    hBut1 = CreateWindowEx(0,"Button","Button1",
       WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
       245,5,60,30,hwnd,(HMENU)ID_BUT1,hInstMain,NULL);
    hBut2 = CreateWindowEx(0,"Button","Button2",
       WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 
       245,150,60,30,hwnd,(HMENU)ID_BUT2,hInstMain,NULL);
    		break;
    	 case WM_COMMAND:
     if(LOWORD(wParam) == ID_BUT1)//test identifier
    MessageBox(hwnd,"Button 1","",MB_OK);
    if((HWND)lParam == hBut2)//test handle
    MessageBox(hwnd,"Button 2","",MB_OK);
    		 break;
         case WM_DESTROY:
          PostQuitMessage(0);
          break;
        default:
          
          return DefWindowProc(hwnd, message, wParam, lParam);
      }
      return 0;
    }

  5. #5
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Thanks, Fordy. Once I found out that it returned WM_COMMAND, I just looked it up in the MSVC++ docs. And found out the correct returns (as you've described).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To Ken Fitlike and his Countrymen
    By Fordy in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-26-2006, 08:35 PM
  2. Ownerdraw buttons (2)
    By maes in forum Windows Programming
    Replies: 7
    Last Post: 09-11-2003, 05:50 AM
  3. Radio Buttons in Visual C++ 6
    By Ripper1 in forum Windows Programming
    Replies: 22
    Last Post: 05-16-2003, 07:54 AM
  4. Word of the day, ken
    By gamegod3001 in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 10-09-2001, 10:54 PM
  5. Grouping radio buttons
    By Bazz in forum Windows Programming
    Replies: 1
    Last Post: 08-28-2001, 07:15 AM