【问题标题】:c++ - mfc / want to add bitmap to cbutton. CButton has no member setBitmap and BM_SETIMAGE is also not available for sendMessagec++ - mfc / 想要将位图添加到 cbutton。 CButton 没有成员 setBitmap 并且 BM_SETIMAGE 也不能用于 sendMessage
【发布时间】:2014-12-12 20:03:04
【问题描述】:

这是我关于 stackoverflow 的第一个问题,我希望我做的一切都是正确的:S

正如我的标题中所述,我正在与 mfc 合作进行视觉工作室(2012)项目。 我尝试向我的 cbutton 添加一个位图,该位图被插入到我的对话框的设计视图中。

我读过的所有帖子都描述了使用 setBitmap 或 sendMessage 来做到这一点。 我总是尝试在对话框的 onInit() 函数中执行此操作。 当我(尝试)像这样使用 setBitmap() 时:

m_backButton.Attach (LoadBitmap (AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BACK_BUTTON))); //m_backButton is a private CBitmap member of my dialog
CButton* pButton = (CButton* )GetDlgItem(IDC_BUTTON1);
pButton->SetBitmap(m_backButton);

这会导致 IntelliSense 错误:

IntelliSense:类“CButton”没有成员“setBitmap”

另一个尝试是使用 sendMessage:

m_backButton.Attach (LoadBitmap (AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BACK_BUTTON)));
CButton* pButton = (CButton* )GetDlgItem(IDC_BUTTON1);  
HBITMAP hBitmap = (HBITMAP)m_backButton;
pButton->SendMessage(BM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)hBitmap); 

首先我得到另一个 IntelliSense 错误:

IntelliSense:标识符“BM_SETIMAGE”未定义

就像我在另一篇文章中读到的那样,我自己定义了“BM_SETIMAGE”:

#define BM_SETIMAGE 0x00F7

现在代码可以编译了,但是按钮仍然没有显示位图... 由于互联网上的每个帖子都使用这两种解决方案之一,我很无奈。 有人知道有什么问题吗? 如果没有,也感谢您的阅读:)

【问题讨论】:

  • 哪个操作系统?根据BM_SETIMAGE documentation 上的 cmets,XP 和 7 之间的某个地方发生了变化。
  • 最终代码在紧凑的嵌入式设备上运行。它使用 Microsoft Compact Embedded 2013。
  • 我查看了ce sdk的winuser.h,找不到标签BM_SETIMAGE。这种情况下是不是不能用位图来做按钮?
  • 是的,CE 本身很可能不支持位图按钮。您始终可以自己使用所有者绘制来完成,但工作量要大得多。
  • CButton函数是SetBitmap,不是setBitmap。另外,请确保按钮属性在资源编辑器属性中设置了位图样式。

标签: visual-c++ mfc cbitmap cbutton


【解决方案1】:

所以我想我找到了解决方案:) 我想我把它贴在这里,其他人也可能使用它。我也不能不回答这个问题:S

我的解决方案来自http://www.flounder.com/bitmapbutton.htm,并根据我的需要进行了调整。它现在可以与 Microsoft Embedded Compact 2013 一起使用。感谢作者!

我的简短版本如下所示:

ImageButton.h

#include "myApp.h" //check the original article if you are missing dependencies
class CImageButton : public CButton
{

public:
    CImageButton(UINT bitmap);
    // Default constructor (required for MFC compatibility)
    CImageButton() {bitmap = 0; }
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    virtual ~CImageButton();
    void LoadBitmapForButton(UINT bitmap)
          { this->bitmap = bitmap; } 
    void GetBitmaps(UINT &bitmap)
          { bitmap = this->bitmap; }

protected:
    UINT bitmap;

    DECLARE_MESSAGE_MAP()
};

ImageButton.cpp

#include "stdafx.h"
#include "ImageButton.h"

CImageButton::CImageButton(UINT bitmap)
{
 this->bitmap = bitmap;
}

CImageButton::~CImageButton()
{
}


BEGIN_MESSAGE_MAP(CImageButton, CButton)
END_MESSAGE_MAP()

void CImageButton::DrawItem(LPDRAWITEMSTRUCT dis) 
{
     CDC * dc = CDC::FromHandle(dis->hDC);  // Get a CDC we can use
     CRect r(dis->rcItem);                  // Copy the button rectangle
     CBitmap bitmap;                        // Handle to the bitmap we are drawing
     BITMAP bmpval;                         // Parameters of the bitmap

     int saved = dc->SaveDC();              // Save the DC for later restoration

     bitmap.Attach (LoadBitmap (AfxGetInstanceHandle(), MAKEINTRESOURCE(this->bitmap)));

     // Get the bitmap parameters, because we will need width and height
     ::GetObject(bitmap, sizeof(BITMAP), &bmpval);

     // Select the bitmap into a DC
     CDC memDC;
     memDC.CreateCompatibleDC(dc);
     int savemem = memDC.SaveDC();
     memDC.SelectObject(bitmap);

    dc->BitBlt(0, 0,                            // target x, y
           min(bmpval.bmWidth, r.Width() ),     // target width
           min(bmpval.bmHeight, r.Height() ),   // target height
           &memDC,                              // source DC
           0, 0,                                // source x, y
           SRCCOPY);

     memDC.RestoreDC(savemem);

     dc->RestoreDC(saved);
     ::DeleteObject(bitmap);
}

您可以使用资源编辑器或动态添加普通 CButton(我认为,未测试),将其转换为 ImageButton 并使用 loadBitmapForButton 加载位图。 CButton 的属性 owner-draw 必须设置为 true。 就是这样:)

PS,直到现在我还没有检查过正确的内存释放代码...我会尽快检查,如果我发现某些东西丢失了,我会在我的帖子中添加这个。如果其他人可以在这一点上提供帮助,请随时教我;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 2010-11-03
    相关资源
    最近更新 更多