我应该如何设置图像(最简单的位图是一个简单的位图)来动态创建CButton?我试过这个(CustomButton继承自CButton),但似乎无效:
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;
}
问题出在哪里?
这是完整的代码:
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;
}
答案 0 :(得分:0)
我的案例中的解决方案是:
HBITMAP ButtonBitmap = (HBITMAP)LoadImage(NULL, "c:/arrow.bmp", IMAGE_BITMAP, 30, 30, LR_LOADFROMFILE);
if (ButtonBitmap)
{
SetBitmap(ButtonBitmap);
}