c ++-mfc/要将位图添加到cbutton. CButton没有成员setBitmap,并且BM_SETIMAGE也不适用于sendMessage [英] c++ - mfc / want to add bitmap to cbutton. CButton has no member setBitmap and BM_SETIMAGE is also not available for sendMessage

查看:491
本文介绍了c ++-mfc/要将位图添加到cbutton. CButton没有成员setBitmap,并且BM_SETIMAGE也不适用于sendMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我关于stackoverflow的第一个问题,我希望我做对了所有事情:S

this is my first question on stackoverflow and i hope i do everything right:S

正如我的职称中所述,我正在与mfc一起从事Visual Studio(2012)项目. 我尝试向我的cbutton添加一个位图,该位图已插入到设计视图的对话框中.

As described in my titel i am working on a visual studio(2012) project with mfc. I try to add a bitmap to my cbutton, which was inserted in the design view to my dialog.

我已经阅读过的所有文章,都描述了使用setBitmap或sendMessage来做到这一点. 我总是尝试在对话框的onInit()函数中执行此操作. 当我(尝试)使用setBitmap()时:

All post i've read about this, describe to use setBitmap or sendMessage to do so. I always try to do this in the onInit()-function of my dialog. When i (try to) use setBitmap() like this:

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错误:

It results in an IntelliSense-Error:

IntelliSense:类"CButton"没有成员"setBitmap"

IntelliSense: class "CButton" has no member "setBitmap"

另一种尝试是使用sendMessage:

Another try was to use 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错误:

First i got another IntelliSense-Error:

IntelliSense:标识符"BM_SETIMAGE"未定义

IntelliSense: identifier "BM_SETIMAGE" is undefined

就像我读过另一篇文章一样,我自己定义了"BM_SETIMAGE":

Like i've read in another post, i defined "BM_SETIMAGE" by my own:

#define BM_SETIMAGE 0x00F7

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

Now the code is able to compile, but the button still shows no bitmap... Since every post in the internet uses one of this two solutions i'm helpless. Anybody an idea whats wrong? And if not, also thank you for reading:)

推荐答案

所以我想我找到了一个解决方案: 我以为我将其发布在这里,以便其他人也可以使用它.我也不能不提这个问题:S

So i think i found a solution:) I thought i post it here, that other may have use of it too. Also i cannot let this question be unansweared:S

我的解决方案来自 http://www.flounder.com/bitmapbutton.htm 和适应我的需求.现在,它可以与Microsoft Embedded Compact 2013一起使用.谢谢指导!

My solution is from http://www.flounder.com/bitmapbutton.htm and adapted to fit my needs. It now can be used with Microsoft Embedded Compact 2013. Thx to the autor!

我的简短版本如下:

ImageButton.h

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

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. 就是这样:)

Than you can add a normal CButton with ressource editor or dynamically(i think, not tested), cast it to ImageButton and load the bitmap with loadBitmapForButton. The property owner-draw of the CButton must be set to true. Thats all:)

PS,直到现在我才检查代码是否进行了正确的内存释放.如果我发现某事不见了,我会尽快这样做,我会在我的帖子中添加它.如果其他人可以在这方面提供帮助,请随时教我;)

PS, i did not checked the code for correct memory deallocation until now...I will do so soon, if i found sth missing i will add this in my post. If someone else can help in this point, feel free to teach me;)

这篇关于c ++-mfc/要将位图添加到cbutton. CButton没有成员setBitmap,并且BM_SETIMAGE也不适用于sendMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆