如何杀死“编辑”的焦点控制“输入”按键 [英] How to kill focus of "edit" control on "Enter" key press

查看:135
本文介绍了如何杀死“编辑”的焦点控制“输入”按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主函数中创建了一个主窗口。在WM_CREATE消息的主窗口的过程中,创建一个编辑控件,作为父窗口的子窗口使用系统编辑窗口类。我想要在编辑控制中按下回车键时将焦点转移到主窗口。由于我使用系统类我没有访问其过程。
我在Visual Studio 10中使用C ++
由于我是新的win32应用程序,我想要一个简单的解决方案,无论代码多长。

I have a main window created in main function. In procedure for the main window on WM_CREATE message I create a edit control which as a child of parent window using system "edit" window class. I want the focus to be transferred to main window when enter key is pressed in edit control. Since I used system class I don't have access to its procedure. I am using C++ for this in Visual Studio 10 Since I'm new to win32 apps I want a simple solution no matter how long the code is

推荐答案

如果您的窗口只有一个可控制的控件(如编辑控件),那么该控件将总是有焦点。你不能有一个没有聚焦控件的窗口,并且父窗口本身是不可聚焦的(即不能有焦点)。

If your window has only one focusable control (such as an edit control), then that control will always have the focus. You cannot have a window with no focused controls, and the parent window itself is not focusable (i.e. cannot have the focus).

所以你首先需要添加另一个可聚焦的控制到你的窗口,如果你还没有一个已经(我不能从这个问题告诉)。例如,您可以添加确定或取消按钮。这样,每当你对编辑控件进行聚焦时,按钮就可以接收焦点。

So you will first need to add another focusable control to your window, if you don't have one already (I couldn't tell from the question). For example, you might add an "OK" or "Cancel" button. That way, whenever you unfocus the edit control, the button can receive the focus.

然后,你需要对编辑控件进行子类化,事件(例如 WM_KEYDOWN WM_KEYUP )。要子类化单个窗口,请调用 SetWindowLongPtr 函数并传递窗口句柄以及 GWLP_WNDPROC 标志和指向自定义窗口过程的指针。这有效地用您的自定义窗口过程替换该控制类的默认窗口过程。例如:

Then, you will need to subclass the edit control so that you can process its key press events (e.g. WM_KEYDOWN and WM_KEYUP). To subclass a single window, call the SetWindowLongPtr function and pass the window handle along with the GWLP_WNDPROC flag and a pointer to your custom window procedure. This effectively replaces the default window procedure for that class of control with your custom window procedure. For example:

// Stores the old original window procedure for the edit control.
WNDPROC wpOldEditProc;

// The new custom window procedure for the edit control.
LRESULT CALLBACK CustomEditProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_KEYDOWN:
        {
            if (wParam == VK_RETURN)
            {
                // The user pressed Enter, so set the focus to the other control
                // on the window (where 'hwndOther' is a handle to that window).
                SetFocus(hwndOther);

                // Indicate that we processed the message.
                return 0;
            }
        }
    }

    // Pass the messages we don't process here on to the
    // original window procedure for default handling.
    CallWindowProc(wpOldEditProc, hWnd, msg, wParam, lParam);
}





// ----- Add to the parent window's WM_CREATE: -----

// Create the edit control
HWND hwndEdit = CreateWindowEx(...);

// Subclass it.
wpOldEditProc = (WNDPROC)SetWindowLongPtr(hwndEdit,
                                          GWLP_WNDPROC,
                                          (LONG_PTR)CustomEditProc);

// Show it.
ShowWindow(hwndEdit, SW_SHOW);

// ... your other code (e.g. creating and showing the other control)


$ b b

// ----- Add to the parent window's WM_DESTROY: -----

// Unsubclass the edit control.
SetWindowLongPtr(hwndEdit, GWLP_WNDPROC, (LONG_PTR)wpOldEditProc);

// ... your other code (e.g. calling PostQuitMessage(...) to close your app)

有关子类化窗口的更多信息,请访问这里在MSDN 。示例代码(以及网络上的许多其他地方)假设您是在对话框窗口中对一个编辑控件进行子类化。因为对话框是自动处理大量键盘处理的特殊类型的父窗口,所以您需要采取额外的步骤来克服对话框执行的默认处理。如果您使用的是使用 CreateWindowEx 创建的常规窗口,则不需要。

Further reading about subclassing windows is here on MSDN. The sample code there (and lots of other places on the web) assumes that you're subclassing an edit control in a dialog window. Because dialogs are special types of parent windows that handle a lot of keyboard processing automatically, you need to take extra steps to overcome this default processing done by the dialog. If you're using a regular window created with CreateWindowEx, that is not necessary.

>多个编辑控件,所有这些控件在响应某些按键操作时都会以相同的方式运行,因此注册自定义窗口子类更加干净,更好的设计。上面的代码只是子类化了一个编辑控制对象,这种方法会创建一个新类型的自定义编辑控制类。您可以根据需要创建这种新类型的编辑控件的实例,并且它们的行为都是一样的。

If you want multiple edit controls that all behave the same way in response to certain key presses, it is much cleaner and better design to register a custom window subclass. Whereas the above code subclassed only a single edit control object, this approach would create a new type of custom edit control class. You could create as many instances of this new type of edit control as you wanted, and they would all behave the same way.

但是我不会介绍这里做。 如果您有兴趣,可以在线查找代码,您的特定用例有点复杂。为了改变焦点,控制必须知道应该将焦点设置到哪个其他控制。这在全球很难处理。使用对话窗口作为父对象是可取的。它管理Z顺序并自动设置焦点。

But I won't go into how to do that here. You can find the code online if you're interested, and your particular use case makes it a bit more complicated. In order to change the focus, the control has to know which other control it should set the focus to. This is difficult to handle globally. Using a dialog window as the parent might be advisable. It manages the Z order and setting the focus for you automatically.

这篇关于如何杀死“编辑”的焦点控制“输入”按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆