将光标置于 visual studio 中的编辑控制框中

Place cursor in edit control box in visual studio

我正在尝试在 visual studio 软件中制作一个密码对话框。一旦我的密码对话框打开,我希望将光标放在编辑控件框中,以便我可以在上面输入密码。

如何在 visual studio MFC 的文本编辑框中放置或放置光标而无需单击鼠标?

请建议我如何执行此操作。

static CEdit *ptrCurrentEditWindow;

BOOL GET_PASSWORD::OnInitDialog()
{
    CDialog::OnInitDialog();

    SetDlgItemText(IDC_PASSWORD, L"");
    ptrCurrentEditWindow = &m_wnd_password;

    return TRUE;  
}

CDialog::OnInitDialog 的文档解释了如何执行此操作。 return 值部分包含以下信息:

Specifies whether the application has set the input focus to one of the controls in the dialog box. If OnInitDialog returns nonzero, Windows sets the input focus to the default location, the first control in the dialog box. The application can return 0 only if it has explicitly set the input focus to one of the controls in the dialog box.

剩下 2 个选项:

  • 将您的编辑控件设置为第一个控件,并通过 returning TRUE.
  • 让 Windows 为您处理所有事情
  • 手动将输入移至您的编辑控件 (CDialog::GotoDlgCtrl),并且 return FALSE.

解决更新后的问题:只要您将 IDC_PASSWORD 设为对话框模板中的第一个控件,该实现就已经满足您的需求。如果您不想或不能为此安排,则必须手动移动输入焦点,如下所示:

BOOL GET_PASSWORD::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Not needed; an edit control is initially empty
    SetDlgItemText(IDC_PASSWORD, L"");

    // Set input focus to the desired control
    GotoDlgCtrl(GetDlgItem(IDC_PASSWORD));
    // Let the framework know, that you already set input focus
    return FALSE;
}