Thread: Control message handling

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    Control message handling

    Ok, I have created a button control during runtime, and I want to know how I can handle it. I tried using GetDlgItem() in many different ways but it doesn't work. My WM_CREATE message looks like this:

    Code:
            case WM_CREATE:
                 {
                     HFONT hfDefault;
                     HWND hEdit;
                     HWND hButton;
                     
                     hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
                                            "EDIT",
                                            "",
                                            WS_VISIBLE | WS_CHILD,
                                            0,0,0,0,
                                            hwnd,
                                            (HMENU)IDC_MAIN_EDIT,
                                            GetModuleHandle(NULL),
                                            NULL);
                     
                     hButton = CreateWindowEx(0,
                                              "BUTTON",
                                              "whatever",
                                              WS_VISIBLE | WS_CHILD,
                                              120,150,100,20,
                                              hwnd,
                                              (HMENU)IDC_MAIN_BUTTON,
                                              GetModuleHandle(NULL),
                                              NULL);
                     
                     hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
                     SendMessage(hEdit,WM_SETFONT,(WPARAM)hfDefault,MAKELPARAM(false,0));
                 }
            break;
    also, can someone explain to me how using the MAKELPARAM() macro and filling it in with false and 0 make a lparam appropriate for my message.
    Last edited by Homunculus; 02-04-2006 at 11:56 PM.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    First, GetDlgItem() only works for Dialogs. (Duh.) Dialogs use WM_INITDIALOG message. So, are you using a dialog or a window? The above appears to be a window. If it's a dialog, I'd look up WM_INITDIALOG, and see I might perhaps create those controls in a resource file.

    Regardless of it is a window or a dialog, if the button is being created, all is good. To get a button "click", you can process the WM_COMMAND message. And bunch of controls will sent this whenever something happens to them, including buttons. The WM_COMMAND message contains a notification code, a handle to the window (button) that generated the message, and (possibly) the id of the control. (Usually in dialogs, where you know the ID and not the handle) Look up WM_COMMAND and BN_CLICKED, that should give what you want.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>GetDlgItem() only works for Dialogs<<

    Actually, it's good for any window:
    Quote Originally Posted by msdn
    (GetDlgItem)

    You can use the GetDlgItem function with any parent-child window pair, not just with dialog boxes.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    woot

    I thought I seen something like that on msdn =P. Well im going to go look up on BN_CLICKED and see what I can find, (I already tried putting something in case WM_COMMAND but haven't been to succesful)

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Remember that as you have declared the HWNDs for the conrols as non static in the callback that they will not retian their values after the WM_CREATE msg is processed.
    This means you will have to use the controls ID#, not HWND to find if that control has been used.

    You can use GET_WM_COMMAND_ID() from windowsx.h


    something like...

    Code:
    case WM_COMMAND:
         switch(GET_WM_COMMAND_ID(wParam,lParam))//switch based on the controls ID#
         {
                  case IDC_MAIN_BUTTON:
                      if( GET_WM_COMMAND_CMD( wParam, lParam)== BN_CLICKED)
    "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

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    You read my mind >.>

    That was one of my many questions I planned on asking in the future,thanks, When I was closing through my menu, I could hear my button making a message box and closing quickly... But everything is working perfectly since I changed my button to a static =D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. event handling is serialized in MS Visual Studio C++ 2005 ??
    By mynickmynick in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2008, 04:47 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Custom Control
    By X PaYnE X in forum Windows Programming
    Replies: 24
    Last Post: 03-03-2005, 11:47 AM
  4. tab control
    By tyouk in forum Windows Programming
    Replies: 6
    Last Post: 02-07-2005, 11:47 PM
  5. input/output
    By dogbert234 in forum Windows Programming
    Replies: 11
    Last Post: 01-26-2005, 06:57 AM