DialogBox

I Have a DialogBox with a ComboBox in it, I want to call to a function when the user hits <enter> in the ComboBox, I subclassed the window procedure of the ComboBox so I could check the WN_CHAR messages, the problem is, when the user hits <enter> in the ComboBox the DialogBox doesnt send an WM_CHAR message to it's window procedure, it sends WM_COMMAND with IDOK to the Dialog Procedure which ends the dialog...

How do I fix that?

no MFC
VC++
dafokiAsked:
Who is Participating?
 
robpittConnect With a Mentor Commented:
Almost. I think you'll actually need a mixture of the two samples I've refered you to thus far.

You should subclass the edit control inside the combo box and then in your new edit procedure you can handle WM_GETDLGCODE and WM_KEYDOWN.

0
 
sdjefferiesCommented:
Have you tried unticking the "default button" property? Thus preventing the IDOK event triggering on return.

Simon
0
 
dafokiAuthor Commented:
i tried that, it still triger the IDOK
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
LockiasCommented:
You need to handle the WM_KEYDOWN message and look for the wParam to be VK_RETURN.  You will only find alpha numerics in come through the WM_CHAR message.

~Lockias
0
 
dafokiAuthor Commented:
This is my ComboBox procedure:
HRESULT CALLBACK myComboProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
     if(Msg == WM_KEYDOWN && wParam == VK_RETURN)
     {
          MessageBox(0,"Enter","",0);
          return 0;
     }
     return CallWindowProc(OldComboProc,hWnd,Msg,wParam,lParam);
}

it's not working......
0
 
LockiasCommented:
Test to see if your combo is getting keydown messages other than for the return.  I suspect that return (and I think the escape key as well) may be special cases that the main dialog get first crack at and your combo box never sees.  In this case you will need to handle these events in your dialog procedure, or mess around with the message translation stuff.  

I have seen this before.  If what I stated above does not get anywhere, I will look more deeply and thoroughly at this.

~Lockias
0
 
robpittCommented:
There is a mechanism by which a control can signal what keypresses it is interested in.

This mechanism is the WM_GETDLGCODE message which is sent by windows to the control.

Add code to your myComboProc() so that the control returns WANTALLKEYS when windows sends the WM_GETDLGCODE message t the control.

Rob
0
 
dafokiAuthor Commented:
Rob, I'm not sure I understand how to use the WM_GETDLGCODE,
all I need to do is to return DLGC_WANTALLKEYS when I get the message?

This how my Combo procedure should look like?

switch(Msg)
{
     case WM_GETDLGCODE:
          return DLGC_WANTALLKEYS;
     case WM_KEYDOWN:
          if(wParam == VK_RETURN)
               MessageBox(0,"Enter","",0);
          return 0;
}
0
 
robpittCommented:
I think that should work, though you might want to do a CallWindowProc first.
E.g.

    case WM_GETDLGCODE:
        CallWindowProc(OldComboProc,...
        return DLGC_WANTALLKEYS;

Let us know how it goes.
0
 
dafokiAuthor Commented:
it's not working........

i an expamle for WM_GETDLGCODE, i tried to combine in with my code, and it's still not working...
why?????

HRESULT CALLBACK myComboProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
     LRESULT lRet;
     LPMSG lpmsg;

     switch(Msg)
     {
     case WM_GETDLGCODE:
          lRet = CallWindowProc(OldComboProc,hWnd,Msg,wParam,lParam);
         if (lParam)
          {
               lpmsg= (LPMSG) lParam;
               if (lpmsg->message == WM_KEYDOWN)
               {
                    if (lpmsg->wParam == VK_RETURN)
                              lRet |= DLGC_WANTALLKEYS;
               }
          }
        return (lRet);
     case WM_KEYDOWN:
          if(wParam == VK_RETURN)
               MessageBox(hWnd,"ENTER","",0);
          return 0;
     }
     return CallWindowProc(OldComboProc,hWnd,Msg,wParam,lParam);
}
0
 
robpittCommented:
That's odd, I would have expected that to work!

You might want to checkout http://support.microsoft.com/support/kb/articles/Q102/5/89.asp
This artcile talks about the very same issue in the context of EDIT boxes - it should be applicable.

Rob
0
 
dafokiAuthor Commented:
I know what's wrong, the dialog procedure never gets the WM_DLGCODE message,

case WM_GETDLGCODE:
     MessageBox(hWnd,"DLG","",0); // it never pops
          return(DLGC_WANTALLKEYS | CallWindowProc(OldComboProc,hWnd,Msg

The MessagewBox doesnt pop, any idea why?
0
 
robpittCommented:
Just to clarify, all of the above WM_GETDLGODE stuff should be put in the replacement combo box wndproc.
I imagine you installed this by doing something like:
OldComboProc=SetWindowLong(hCombo,GWL_WNDPROC,NewComboProc);


Another question... is your dialog box modal or modeless (i.e. was it created with DialogBox() or CreateDialog()). If it is modeless do call IsDialogMessage() correctly in your main message loop?
0
 
robpittCommented:
Just to clarify, all of the above WM_GETDLGODE stuff should be put in the replacement combo box wndproc.
I imagine you installed this by doing something like:
OldComboProc=SetWindowLong(hCombo,GWL_WNDPROC,NewComboProc);


Another question... is your dialog box modal or modeless (i.e. was it created with DialogBox() or CreateDialog()). If it is modeless do call IsDialogMessage() correctly in your main message loop?
0
 
dafokiAuthor Commented:
it's ok i fixed it.......there was somthing wrong with my code, now i get the WM_GETDLGCODE, but i still cant make my dialog proc to send me a WM_CHAR/WM_KEYDOWN when the user hits <enter>....
0
 
robpittCommented:
Ahhhh, I bet its because its the EDIT control inside the combobox that actually has the keyboard focus!

ANYWAY, I just found this which should do exactly what you want...

http://msdn.microsoft.com/library/en-us/winui/hh/winui/combobox_2hbm.asp
0
 
dafokiAuthor Commented:
is that how they do it?
hwndEdit1 = ChildWindowFromPoint(hwndCombo1, pt);
hwndEdit2 = ChildWindowFromPoint(hwndCombo2, pt);

what does the ChildWindowFromPoint() do? (i didnt understand from the MSDN)
0
 
robpittCommented:
If you hadn't gathered, an editable combo box control contains an edit control as a child window.

All the ChildWindowFromPoint thing does is get the child window (the edit control) that is at position (1,1) inside the combo box.

Since there is only likely to be one child window within the combo box, I would have just done
    GetWindow(hCombo,GW_CHILD);
to get the same result.
0
 
dafokiAuthor Commented:
i think i got it,
After i get the child edit window,  inside the combo box than i use the WM_GETDLGCODE to monitor which keys was pressed
0
 
dafokiAuthor Commented:
thanks for all the help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.