带有位图和文本的 MFC 按钮

发布于 2024-08-22 07:35:33 字数 459 浏览 2 评论 0原文

我有一个自定义 CButton,它使用 CButton::SetBitmap(bitmap); 加载位图。

同时,我想在同一个按钮上的位图上方显示文本。

我尝试实现 OnPaint(),但它不显示文本,只显示位图

void CBitmapToggleButton::OnPaint()
{
    CButton::OnPaint();
    CPaintDC dc(this); // device context for painting

    CString caption(_T("test message"));
    GetWindowText(caption);
    CRect rect;
    GetWindowRect(&rect);

    dc.DrawText(caption, &rect, DT_CENTER);
}

我该怎么做才能显示文本?

I have a Custom CButton which loads a bitmap, using CButton::SetBitmap(bitmap);

Meanwhile, I want to display a text above the bitmap, on the same button.

I tried implementing OnPaint(), but it does not display the text, just the bitmap

void CBitmapToggleButton::OnPaint()
{
    CButton::OnPaint();
    CPaintDC dc(this); // device context for painting

    CString caption(_T("test message"));
    GetWindowText(caption);
    CRect rect;
    GetWindowRect(&rect);

    dc.DrawText(caption, &rect, DT_CENTER);
}

What can I do to display the text too?

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

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

发布评论

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

评论(2

誰認得朕 2024-08-29 07:35:33

使用 GetClientRect()。传递给 DrawText() 的参数需要位于客户端坐标中。

也就是说,忘记这一点并使用 CMFCButton。它比您在合理时间内自己编写的任何内容都要好得多。

Use GetClientRect(). The argument passed to DrawText() needs to be in client coordinates.

That said, forget about this and use CMFCButton. It's way better than anything you can write yourself in a reasonable amount of time.

幽梦紫曦~ 2024-08-29 07:35:33

试试这个

CButton* pBtn=
(CButton*)GetDlgItem(ID_WIZBACK);

pBtn->ModifyStyle( 0, BS_ICON );

HICON hIcn= (HICON)LoadImage(
AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON3),
图像图标,
0,0, // 使用实际大小
LR_默认颜色
);

pBtn->SetIcon( hIcn );

Try this

CButton* pBtn=
(CButton*)GetDlgItem(ID_WIZBACK);

pBtn->ModifyStyle( 0, BS_ICON );

HICON hIcn= (HICON)LoadImage(
AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON3),
IMAGE_ICON,
0,0, // use actual size
LR_DEFAULTCOLOR
);

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