Solved

DialogBox

Posted on 2001-08-03
20
791 Views
Last Modified: 2013-12-03
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++
0
Comment
Question by:dafoki
  • 9
  • 8
  • 2
  • +1
20 Comments
 

Expert Comment

by:sdjefferies
Comment Utility
Have you tried unticking the "default button" property? Thus preventing the IDOK event triggering on return.

Simon
0
 

Author Comment

by:dafoki
Comment Utility
i tried that, it still triger the IDOK
0
 
LVL 2

Expert Comment

by:Lockias
Comment Utility
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
 

Author Comment

by:dafoki
Comment Utility
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
 
LVL 2

Expert Comment

by:Lockias
Comment Utility
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
 
LVL 5

Expert Comment

by:robpitt
Comment Utility
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
 

Author Comment

by:dafoki
Comment Utility
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
 
LVL 5

Expert Comment

by:robpitt
Comment Utility
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
 

Author Comment

by:dafoki
Comment Utility
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
 
LVL 5

Expert Comment

by:robpitt
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:dafoki
Comment Utility
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
 
LVL 5

Expert Comment

by:robpitt
Comment Utility
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
 
LVL 5

Expert Comment

by:robpitt
Comment Utility
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
 

Author Comment

by:dafoki
Comment Utility
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
 
LVL 5

Expert Comment

by:robpitt
Comment Utility
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
 

Author Comment

by:dafoki
Comment Utility
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
 
LVL 5

Expert Comment

by:robpitt
Comment Utility
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
 

Author Comment

by:dafoki
Comment Utility
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
 
LVL 5

Accepted Solution

by:
robpitt earned 300 total points
Comment Utility
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
 

Author Comment

by:dafoki
Comment Utility
thanks for all the help
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now