首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MFC C++无法在动态创建的CButton继承对象上设置图像

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

Stack Overflow用户
提问于 2015-03-15 23:22:52
回答 2查看 244关注 0票数 0

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

代码语言:javascript
复制
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

代码语言:javascript
复制
#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

代码语言:javascript
复制
// 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;
}
EN

回答 2

Stack Overflow用户

发布于 2015-03-15 23:58:12

将ButtonBitmap;定义为gobal或number变量!

票数 0
EN

Stack Overflow用户

发布于 2015-03-15 23:59:05

在我的例子中,解决方案是:

代码语言:javascript
复制
HBITMAP ButtonBitmap = (HBITMAP)LoadImage(NULL, "c:/arrow.bmp", IMAGE_BITMAP, 30, 30, LR_LOADFROMFILE);
if (ButtonBitmap)
{
    SetBitmap(ButtonBitmap);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29062327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档