C++ 如何在特定窗口中设置控件的初始焦点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5114473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:25:25  来源:igfitidea点击:

How to Set the Initial Focus of the control in the particular window?

c++windowswinapimfcsetfocus

提问by karthik

I created an application in which I use window procedure to keep track of all the controls in the window.

我创建了一个应用程序,在其中使用窗口过程来跟踪窗口中的所有控件。

My question is, how do I initially set the focus to the first created control in the window?

我的问题是,我最初如何将焦点设置为窗口中第一个创建的控件?

回答by Cody Gray

There are two ways to set the initial focus to a particular control in MFC.

有两种方法可以将初始焦点设置到 MFC 中的特定控件。

  1. The first, and simplest, method is to take advantage of your controls' tab order. When you use the Resource Editor in Visual Studio to lay out a dialog, you can assign each control a tab index. The control with the lowesttab index will automatically receive the initial focus. To set the tab order of your controls, select "Tab Order" from the "Format" menu, or press Ctrl+D.

  2. The second, slightly more complicated, method is to override the OnInitDialogfunctionin the class that represents your dialog. In that function, you can set the input focus to any control you wish, and then return FALSEto indicate that you have explicitly set the input focus to one of the controls in the dialog box. If you return TRUE, the framework automatically sets the focus to the default location, described above as the first control in the dialog box. To set the focus to a particular control, call the GotoDlgCtrlmethodand specify your control. For example:

    BOOL CMyDialog::OnInitDialog()
    {
        CDialog::OnInitDialog();
    
        // Add your initialization code here
        // ...
    
        // Set the input focus to your control
        GotoDlgCtrl(GetDlgItem(IDC_EDIT)); 
    
        // Return FALSE because you manually set the focus to a control
        return FALSE;
    }
    

    Note that you should notset focus in a dialog box by simply calling the SetFocusmethod of a particular control. Raymond Chen explains here on his blogwhy it's more complicated than that, and why the GotoDlgCtrlfunction (or its equivalent, the WM_NEXTDLGCTRLmessage) is preferred.

  1. 第一种也是最简单的方法是利用控件的 Tab 键顺序。当您使用 Visual Studio 中的资源编辑器来布置对话框时,您可以为每个控件分配一个选项卡索引。标签索引最低的控件将自动获得初始焦点。要设置控件的 Tab 顺序,请从“格式”菜单中选择“Tab 顺序”,或按Ctrl+ D

  2. 第二种稍微复杂一点的方法是覆盖表示对话框的类中的OnInitDialog函数。在该函数中,您可以将输入焦点设置为您希望的任何控件,然后返回FALSE以指示您已将输入焦点显式设置为对话框中的控件之一。如果您返回TRUE,框架会自动将焦点设置到默认位置,如上述对话框中的第一个控件。要将焦点设置到特定控件,请调用该GotoDlgCtrl方法并指定您的控件。例如:

    BOOL CMyDialog::OnInitDialog()
    {
        CDialog::OnInitDialog();
    
        // Add your initialization code here
        // ...
    
        // Set the input focus to your control
        GotoDlgCtrl(GetDlgItem(IDC_EDIT)); 
    
        // Return FALSE because you manually set the focus to a control
        return FALSE;
    }
    

    请注意,您应该通过简单的调用将焦点设置在对话框中SetFocus的特定控制的方法。Raymond Chen在他的博客中解释了为什么它比这更复杂,以及为什么首选GotoDlgCtrl函数(或等效的WM_NEXTDLGCTRL消息)。