Thread: Determine which control a message came from

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    Determine which control a message came from

    I have a dialog with several checkboxes. The code that is executed when one of the checkboxes is clicked is very similar for all checkboxes. I would like to be able to set the ON_BN_CLICKED macro for all my checkboxes to execute the same function then determine from which checkbox the message came.
    Does anyone know of a way to determine what control a message came from?

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Take a look at the Windows Programming Reference, the part about buttons/notification messages:

    Code:
    When the user selects a button, its state changes, and the button sends notification messages to its parent window. For example, a push button control sends the BN_CLICKED notification message whenever the user chooses the button. In all cases, the low-order word of the wParam parameter contains the control identifier, the high-order word of wParam contains the notification code, and the lParam parameter contains the control window handle.
    Also take a look at how BM_CLICKED message is sent:

    Code:
    The parent window of the button receives this notification message through the WM_COMMAND message.
    So just get the WM_COMMAND message and search for a HIWORD(wParam) that matches BN_CLICKED; there you will have the checkbox id as LOWORD(wParam) and the checkbox handle as lParam

    Hope that helps

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    The LOWORD(wParam) will only be vaild if the child control had the HMENU param set (to an ID number) when it was created (or the control was created using a resource editor that stores a list of ID numbers in resource.h).

    Code:
    case WM_COMMAND:
    
    int CtrlID = LOWORD(wParam);
    
    switch (CtrlID)
    {
         case CB_NUM1:
         case CB_NUM2:
         .....
         case CB_NUMX:
            if(HIWORD(wParam) == BN_CLICKED)
            {
                  return  DoCBProcessing(CtrlID);//handler determines return to OS
            }
         break;
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roakes499 View Post
    I have a dialog with several checkboxes. The code that is executed when one of the checkboxes is clicked is very similar for all checkboxes. I would like to be able to set the ON_BN_CLICKED macro for all my checkboxes to execute the same function then determine from which checkbox the message came.
    Does anyone know of a way to determine what control a message came from?
    Here's a section of the resource file that creates a dialog box...
    Code:
    SETUP0 DIALOGEX DISCARDABLE 8, 18, 299, 178
    STYLE DS_SHELLFONT|WS_CHILD|WS_VISIBLE
    EXSTYLE WS_EX_CONTROLPARENT|WS_EX_TOPMOST
    FONT 8, "Tahoma", 0, 0, 1
    {
      CONTROL "", 4001, "ListBox", LBS_MULTIPLESEL|LBS_SORT|LBS_NOINTEGRALHEIGHT|LBS_NOTIFY|WS_VSCROLL|WS_BORDER|WS_TABSTOP, 22, 19, 256, 44
      CONTROL "Add", 4002, "Button", BS_FLAT|WS_TABSTOP, 22, 65, 50, 14
      CONTROL "Remove", 4003, "Button", BS_FLAT|WS_TABSTOP, 92, 65, 50, 14
      CONTROL "Clear", 4004, "Button", BS_FLAT|WS_TABSTOP, 228, 65, 50, 14
      CONTROL "", 4005, "ListBox", LBS_MULTIPLESEL|LBS_SORT|LBS_NOINTEGRALHEIGHT|LBS_NOTIFY|WS_VSCROLL|WS_BORDER|WS_TABSTOP, 22, 107, 256, 44
      CONTROL "Add", 4006, "Button", BS_FLAT|WS_TABSTOP, 22, 153, 50, 14
      CONTROL "Remove", 4007, "Button", BS_FLAT|WS_TABSTOP, 92, 153, 50, 14
      CONTROL "Clear", 4008, "Button", BS_FLAT|WS_TABSTOP, 228, 153, 50, 14
      CONTROL " VC++  Program paths for X86 ", 4050, "Button", BS_GROUPBOX, 12, 7, 275, 79, WS_EX_TRANSPARENT
      CONTROL " VC++ Program paths for X64 ", 4051, "Button", BS_GROUPBOX, 12, 94, 275, 79
    }
    See all the numbers starting with 4001 ... 4051 ... those are control ID numbers that are passed to message tosser in the lowword of the WM_COMMAND message's WPARAM. So you need to

    Code:
    switch (LOWORD(wParam))
       case ...
    As Novacain has already explained.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determine sum of every other value in list.
    By levitylek in forum C Programming
    Replies: 4
    Last Post: 12-08-2010, 02:03 PM
  2. Determine the location of gcc (using gcc)
    By Kennedy in forum Linux Programming
    Replies: 10
    Last Post: 03-16-2010, 08:04 AM
  3. Control message handling
    By Homunculus in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2006, 05:56 PM
  4. Determine MessageBox
    By jkw2068 in forum Windows Programming
    Replies: 2
    Last Post: 08-04-2003, 04:01 PM
  5. Replies: 4
    Last Post: 01-06-2002, 06:22 PM