如何杀死的&QUOT重点;编辑"在&QUOT控制; Enter]键。关键preSS [英] How to kill focus of "edit" control on "Enter" key press

查看:230
本文介绍了如何杀死的&QUOT重点;编辑"在&QUOT控制; Enter]键。关键preSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主函数创建一个主窗口。在对WM_CREATE消息的主窗口过程我创建一个作为父窗口的子窗口中使用编辑窗口类一个编辑控件。我想,当进入关键是在编辑控件pssed $ P $的焦点将转移到主窗口。由于我使用的系统级的我没有访问它的程序。
我使用C ++这个在Visual Studio 10
由于我是新来的Win32应用程序我希望有一个简单的解决方案,无论是code是多久

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).

所以,你首先需要另一个聚焦的控件添加到你的窗口,如果你不已经有一个(我不能从这个问题告诉)。例如,您可以添加一个OK或取消按钮。这样一来,只要你在无焦点的编辑控件,该按钮可接收焦点。

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.

然后,你将需要子类的编辑控件,以便您可以处理它的关键preSS事件(如 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)

// ----- 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 。样品code有(和大量的在网络上其他地方)假定你继承了的对话框的窗口中编辑控件。因为对话框是特殊类型的自动处理大量的键盘处理的父窗口,你需要采取额外措施来克服由对话框完成此默认处理。如果您使用的是与函数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.

如果你想的多个的修改,所有的行为响应某些关键presses以同样的方式控制,这是更清洁,更好的设计来注册自定义窗口的子类。而上述code子类只有一个单一的编辑控制对象,这种方法会创建一个新的类型的自定义编辑控件类。您可以创建这个新类型的编辑控件,因为你想要的尽可能多的情况下,他们会表现得都以同样的方式。

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.

但我不会进入如何做到这一点在这里。 您可以找到code在线如果你有兴趣,你的特殊用途情况下,使得它更复杂一些。为了改变重点,控制有知道哪些其他控制应该将焦点设置。这是难以在全球处理。使用对话窗口作为父可能是可取的。它管理的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.

这篇关于如何杀死的&QUOT重点;编辑"在&QUOT控制; Enter]键。关键preSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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