MFC C ++无法在动态创建的CButton继承对象上设置图像

时间:2015-03-15 15:22:52

标签: c++ bitmap background mfc custom-controls

我正在尝试使用以下代码在动态创建的CButton继承(CustomButton)对象上设置图像:

int CustomButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    ResizeButton();

    ModifyStyle(0, BS_BITMAP);

    CImage ButtonImage;
    BOOL LoadResult = ButtonImage.Load(_T("c:/arrow.bmp"));
    if (FAILED(LoadResult))
    {
        return false;
    }

    CBitmap ButtonBitmap;
    if (!ButtonBitmap.Attach(ButtonImage.Detach()))
        return false;

    SetBitmap(ButtonBitmap);//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    return 0;
}

我已经通过调试器和SetBitmap(ButtonBitmap)进行了检查;被调用但没有任何效果,我不知道为什么以及如何在按钮上设置图像。

以下是完整的代码:

CustomToolBar.h

#pragma once

class CustomButton : public CButton
{
public:
    CustomButton(const CString& ButtonText);

protected:
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    DECLARE_MESSAGE_MAP()

private:
    void ResizeButton();

private:
    CString m_ButtonText;
};

// CustomToolBar
class CustomToolBar : public CWnd
{
    DECLARE_DYNAMIC(CustomToolBar)
    static const CString CLASS_NAME;
public:
    CustomToolBar();
    virtual ~CustomToolBar();

protected:
    //{{AFX_MSG(CustomToolBar)
    //}}AFX_MSG
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    void OnButton(UINT nButtonID);
    DECLARE_MESSAGE_MAP()

private:
    bool CreateControlDynamically() const;

private:
    CustomButton m_Button;
};

CustomToolBar.cpp

// CustomToolBar.cpp : implementation file
//

#include "stdafx.h"
#include "CustomControl.h"
#include "CustomToolBar.h"

BEGIN_MESSAGE_MAP(CustomButton, CButton)
    ON_WM_CREATE()
END_MESSAGE_MAP()

CustomButton::CustomButton(const CString& ButtonText)
    : m_ButtonText(ButtonText)
{
}

int CustomButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    ResizeButton();

    ModifyStyle(0, BS_BITMAP);

    CImage ButtonImage;
    BOOL LoadResult = ButtonImage.Load(_T("c:/arrow.bmp"));
    if (FAILED(LoadResult))
    {
        return false;
    }

    CBitmap ButtonBitmap;
    if (!ButtonBitmap.Attach(ButtonImage.Detach()))
        return false;

    SetBitmap(ButtonBitmap);

    return 0;
}

void CustomButton::ResizeButton()
{
    CDC *pDC = GetDC();
    CString Text;
    GetWindowText(Text);
    CRect Rect;
    GetWindowRect(&Rect);
    CSize Size = pDC->GetTextExtent(Text);
    SetWindowPos(NULL, 0, 0, Size.cx + 10, Rect.Height(), SWP_NOMOVE | SWP_NOZORDER);
    ReleaseDC(pDC);
}

// CustomToolBar
#define BUTTON_RANGE_START  1000
#define BUTTON_RANGE_END    1010

const CString CustomToolBar::CLASS_NAME = "CustomToolBar";

IMPLEMENT_DYNAMIC(CustomToolBar, CWnd)


CustomToolBar::CustomToolBar()
: m_Button("Test1")
{
    CreateControlDynamically();
}

CustomToolBar::~CustomToolBar()
{
}

BEGIN_MESSAGE_MAP(CustomToolBar, CWnd)
    ON_WM_CREATE()
    ON_CONTROL_RANGE(BN_CLICKED, BUTTON_RANGE_START, BUTTON_RANGE_END, OnButton)
END_MESSAGE_MAP()

int CustomToolBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    CRect ClientRect;
    GetClientRect(ClientRect);
    CRect WindowRect;
    GetWindowRect(WindowRect);

    VERIFY(SetWindowPos(NULL, 0, 0, WindowRect.Width(), WindowRect.Height(), SWP_NOZORDER | SWP_NOMOVE));
    ShowWindow(SW_SHOW);

    if (!m_Button.Create("Dynamically created button1111111", WS_CHILD | WS_VISIBLE, CRect(0, 0, 30, 30), this, BUTTON_RANGE_START + 1))
    {
        return 1;
    }


    return 0;
}

void CustomToolBar::OnButton(UINT nButtonID)
{
    // Add button action here
}

bool CustomToolBar::CreateControlDynamically() const
{
    WNDCLASS windowclass;
    HINSTANCE hInst = AfxGetInstanceHandle();

    //Check weather the class is registered already
    if (!(::GetClassInfo(hInst, CLASS_NAME, &windowclass)))
    {
        //If not then we have to register the new class
        windowclass.style = CS_DBLCLKS;// | CS_HREDRAW | CS_VREDRAW;
        windowclass.lpfnWndProc = ::DefWindowProc;
        windowclass.cbClsExtra = windowclass.cbWndExtra = 0;
        windowclass.hInstance = hInst;
        windowclass.hIcon = NULL;
        windowclass.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
        windowclass.hbrBackground = ::GetSysColorBrush(COLOR_WINDOW);
        windowclass.lpszMenuName = NULL;
        windowclass.lpszClassName = CLASS_NAME;

        if (!AfxRegisterClass(&windowclass))
        {
            AfxThrowResourceException();
            return false;
        }
    }

    return true;
}

2 个答案:

答案 0 :(得分:0)

定义按钮位图;作为全局或数字var!

答案 1 :(得分:0)

我的案例中的解决方案是:

HBITMAP ButtonBitmap = (HBITMAP)LoadImage(NULL, "c:/arrow.bmp", IMAGE_BITMAP, 30, 30, LR_LOADFROMFILE);
if (ButtonBitmap)
{
    SetBitmap(ButtonBitmap);
}