开发者

WinApi equivalent of .NET KeyPreview

In .Net there is a feature called KeyPreview. How can I do this in WinApi. Maybe I don't necessarily need this but my hWnd has WM_KEYDOWN, but it does n开发者_如何转开发ot receive it when my Text Box has focus. How can I achieve this? Thanks

*Using pure Win32API...

is there an alternative, how could I handle the Text Box's WM_KEYDOWN?


You can try subclassing the edit control. Either "instance subclassing", to trap messages for only one window, or "global subclassing" to trap messages for all windows of that class (in your application, not system-wide).

The example here (http://msdn.microsoft.com/en-us/library/ms997565.aspx) shows how to subclass an edit control and trap the WM_GETDLGCODE message -it wouldn't be that difficult to change it to WM_KEYDOWN.

You may have problems previewing the keys used for dialog commands, e.g. TAB or RETURN, as they may be trapped higher up the food chain. You may need to look at changing WM_GETDLGCODE as well.


  1. If you use MFC you can look at PreTranslateMessage (I am not sure regarding to PreTranslateMessage, but you can easily to verify whether to enough to override the function).
  2. You can set keyboard hook.


There is no such a possibility in WinAPI to catch the messages belonging to controls in window procedure of parent window (although some exceptions exist). What .NET basically does is that it routes all messages of all controls including their parent window to a single window procedure. Then it's easy to catch all the messages in one place. This is your way to go - to set window procedures of all the controls into single procedure and there to catch all the messages As window procedure also receives hwnd parameter, it's then also easy to determine the control to which the message belongs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜