如何将焦点设置为 CButton 以使边框和焦点虚线可见?

发布于 2024-07-10 03:53:16 字数 890 浏览 7 评论 0原文

我创建了一个简单的基于对话框的应用程序,并使用 Visual Studio 编辑器在默认的 CDialog 中添加了三个按钮(通过拖放它们)。

默认的“确定”和“取消”按钮也在那里。

我想在单击按钮 3 时将焦点设置到按钮 1。

我在多个按钮的属性中将属性 Flat 设置为 true。

我编码如下:

void CbuttonfocusDlg::OnBnClickedButton3()
{
    // TODO: Add your control notification handler code here
    GetDlgItem(IDC_BUTTON1)->SetFocus();

    Invalidate();

}

但是button1中的边框永远不会被绘制。 仅当我在单击按钮 3 之前随时按 TAB 键时,才会绘制插入符号(表示焦点的虚线)。

我希望按钮看起来与单击按钮后的外观完全相同。 以编程方式显示按钮内的虚线将是一个优点。

我想要什么:

http://i33.tinypic.com/11t8pkl.png

我得到的:

http://i37.tinypic.com/160q5hw.png

I created a simple dialog-based application, and in the default CDialog added three buttons (by drag-and-dropping them) using the Visual Studio editor.

The default OK and Cancel buttons are there too.

I want to set the focus to button 1 when I click button 3.

I set the property Flat to true in the properties for muy buttons.

I coded this:

void CbuttonfocusDlg::OnBnClickedButton3()
{
    // TODO: Add your control notification handler code here
    GetDlgItem(IDC_BUTTON1)->SetFocus();

    Invalidate();

}

But the boder in button1 is never drawn. The caret (the dotted line indicating focus) is only drawn if I pressed TAB any time before clicking button 3.

I want the button to look exactly as it looks after I click it. Showing the dotted line inside the button programatically, would be a plus.

What I want:

http://i33.tinypic.com/11t8pkl.png

What I get:

http://i37.tinypic.com/160q5hw.png

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

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

发布评论

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

评论(4

∞觅青森が 2024-07-17 03:53:16

使用WM_NEXTDLGCTL

请参阅 Reymond Chen 的“如何在对话框中设置焦点”< /a>:

void SetDialogFocus(HWND hdlg, HWND hwndControl)
{
    SendMessage(hdlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, TRUE);
}

Use WM_NEXTDLGCTL.

See Reymond Chen's "How to set focus in a dialog box":

void SetDialogFocus(HWND hdlg, HWND hwndControl)
{
    SendMessage(hdlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, TRUE);
}
草莓酥 2024-07-17 03:53:16

通过调用 UpdateWindow,在焦点更改生效之前重新绘制按钮。 Invalidate 本身就足够了,当一切稳定下来后,窗口将被重新绘制。

By calling UpdateWindow, the button is being redrawn before the focus change can take effect. The Invalidate should be sufficient by itself, the window will get repainted when everything settles down.

梦与时光遇 2024-07-17 03:53:16

这会在按钮周围绘制粗边框:

static_cast<CButton*>(GetDlgItem(IDC_BUTTON1))->SetButtonStyle(BS_DEFPUSHBUTTON);

一种更优雅的方法是在 CbuttonfocusDlg 中定义一个 CButton 成员变量,并将其关联到 IDC_BUTTON1 控件,然后调用

this->m_myButton.SetButtonStyle(BS_DEFPUSHBUTTON);

这使得我将焦点设置到的按钮成为默认按钮,但请注意,当焦点转到非按钮的控件(对话框内)时,默认按钮再次成为对话框资源中设置的原始默认按钮,在本例中为“确定”按钮。

This draws the thick border around the button:

static_cast<CButton*>(GetDlgItem(IDC_BUTTON1))->SetButtonStyle(BS_DEFPUSHBUTTON);

A more elegant way to do this would be to define a CButton member variable in CbuttonfocusDlg and associate it to the IDC_BUTTON1 control, and then calling

this->m_myButton.SetButtonStyle(BS_DEFPUSHBUTTON);

This makes the button to which I'm setting the focus the default button, but note that when the focus goes to a control (inside the dialog) that is not a button, the default button is once more the original default button set in the dialog resource, in this case the "Ok" button.

下壹個目標 2024-07-17 03:53:16

我正在听从乔尔的建议。 但与该链接中使用的 API 略有不同,我的 API 是:

PostMessage(WM_NEXTDLGCTL, (WPARAM)(pwnd->GetSafeHwnd()), TRUE);

I am following Joel's suggestion. But slightly different with the API used in that link, my one is :

PostMessage(WM_NEXTDLGCTL, (WPARAM)(pwnd->GetSafeHwnd()), TRUE);

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