MFC 专注于我的输入框的第一个字段

发布于 2025-01-12 02:11:15 字数 872 浏览 7 评论 0原文

我是MFC的初学者,我只想默认选择我的输入框,准备在输入框弹出时用键盘输入文本。

这是我的类的 C++ .cpp 文件:

testInputBox::testInputBox(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_INPUT_BOX, pParent)
    , warning(_T(""))
{

}

testInputBox::~testInputBox()
{
}

void testInputBox::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDIT_INPUT_BOX, editcedit);

    DDX_Text(pDX, IDC_STATIC_MESSAGE, warning);
    DDV_MaxChars(pDX, warning, 100);
}

BEGIN_MESSAGE_MAP(testInputBox, CDialogEx)
    ON_EN_CHANGE(IDC_EDIT_INPUT_BOX, &testInputBox::OnEnChangeEdit1)
    ON_BN_CLICKED(IDOK, &testInputBox::OnBnClickedOk)
    ON_BN_CLICKED(IDCANCEL, &testInputBox::OnBnClickedCancel)
    ON_STN_CLICKED(IDC_STATIC_MESSAGE, &testInputBox::OnStnClickedStaticMessage)
END_MESSAGE_MAP()

I'm a beginner in MFC and I just want my input box selected by default, ready to input text with the keyboard when the input box pops up.

Here is my C++ .cpp file of the class:

testInputBox::testInputBox(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_INPUT_BOX, pParent)
    , warning(_T(""))
{

}

testInputBox::~testInputBox()
{
}

void testInputBox::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDIT_INPUT_BOX, editcedit);

    DDX_Text(pDX, IDC_STATIC_MESSAGE, warning);
    DDV_MaxChars(pDX, warning, 100);
}

BEGIN_MESSAGE_MAP(testInputBox, CDialogEx)
    ON_EN_CHANGE(IDC_EDIT_INPUT_BOX, &testInputBox::OnEnChangeEdit1)
    ON_BN_CLICKED(IDOK, &testInputBox::OnBnClickedOk)
    ON_BN_CLICKED(IDCANCEL, &testInputBox::OnBnClickedCancel)
    ON_STN_CLICKED(IDC_STATIC_MESSAGE, &testInputBox::OnStnClickedStaticMessage)
END_MESSAGE_MAP()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燃情 2025-01-19 02:11:15

您需要覆盖 < code>OnInitDialog()CDialogEx 类的成员,并在该重写中将焦点设置到所需的控件。然后,因为您已经显式设置了焦点,所以您的覆盖函数应该返回零(或FALSE)。

假设您的 editcedit 是指向您希望获得焦点的控件的指针,那么您的重写将如下所示:

BOOL testInputBox::OnInitDialog()
{
    CDialogEx::OnInitDialog(); // Always call base class function
    // ... anything else you want to do here
    editcedit->SetFocus(); // Or: GetDlgItem(IDC_EDIT_INPUT_BOX)->SetFocus();
    return FALSE;
}

您的重写应在类中声明,如下所示:

class testInputBox : public CDialogEx
{
//...
protected:
    BOOL OnInitDialog() override;
    //...
};

You need to override the OnInitDialog() member of the CDialogEx class and, in that override, set the focus to the desired control. Then, because you have explicitly set the focus, your override function should return zero (or FALSE).

Assuming your editcedit is a pointer to the control you wish to have the focus, then your override will look something like this:

BOOL testInputBox::OnInitDialog()
{
    CDialogEx::OnInitDialog(); // Always call base class function
    // ... anything else you want to do here
    editcedit->SetFocus(); // Or: GetDlgItem(IDC_EDIT_INPUT_BOX)->SetFocus();
    return FALSE;
}

Your override should be declared in the class like this:

class testInputBox : public CDialogEx
{
//...
protected:
    BOOL OnInitDialog() override;
    //...
};
幻想少年梦 2025-01-19 02:11:15

CDialog:: GotoDlgCtrl 是您搜索的内容。

BOOL testInputBox::OnInitDialog()
{
    CDialogEx::OnInitDialog(); 
    // ... 

    GotoDlgCtrl(&editcedit);  

    // alternativ
    CWnd* pWnd = GetDlgItem(IDC_EDIT_INPUT_BOX)
    if(pWnd)
       GotoDlgCtrl(pWnd)
    // ...

}

CDialog::GotoDlgCtrl is what you search.

BOOL testInputBox::OnInitDialog()
{
    CDialogEx::OnInitDialog(); 
    // ... 

    GotoDlgCtrl(&editcedit);  

    // alternativ
    CWnd* pWnd = GetDlgItem(IDC_EDIT_INPUT_BOX)
    if(pWnd)
       GotoDlgCtrl(pWnd)
    // ...

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文