所以我想我找到了解决方案:)
我想我将其发布在这里,其他人也可能会使用它。我也不能不回答这个问题:S
我的解决方案来自http://www.flounder.com/bitmapbutton.htm并适应我的需要。它现在可以与 Microsoft Embedded Compact 2013 一起使用。感谢作者!
我的简短版本如下所示:
图像按钮.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()
};
图像按钮.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,直到现在我才检查代码是否正确的内存释放...我很快就会这样做,如果我发现缺少某些内容,我会将其添加到我的帖子中。如果其他人可以在这一点上提供帮助,请随时教我;)