关于Button贴图的问题,重分感谢!!! 急急急!!! 我在界面中需要给按钮贴图片,但是但按钮EnableWindow变成DISABLED后,按钮上的图片不见了,请问我怎么设DISABLED时按钮的图片。我是使用消息贴图的: BM_SETIMAGE,请大家帮忙解决一下。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 class CCoolButton : public CButton{// Constructionpublic: CCoolButton();// Attributespublic:// Operationspublic:// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CCoolButton) public: virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); virtual BOOL SubclassDlgItem(UINT nID,CWnd *pParent); //}}AFX_VIRTUAL 从CButton派生,重载DrawItem,按钮设为OwnerDraw. void CCoolButton::DrawItem(LPDRAWITEMSTRUCT lpDIS) { CDC memDC; CBitmap bitmap; BITMAP bm; UINT bmpID; CRect itemRect = lpDIS->rcItem; UINT IsPressed = lpDIS->itemState & ODS_SELECTED; UINT IsDisabled = lpDIS->itemState & ODS_DISABLED; bmpID = m_nNormalBmp; if(IsPressed) bmpID = m_nPressedBmp; if(IsDisabled) bmpID = m_nDisabledBmp; if(m_bMouseOnButton) { if(IsPressed) bmpID = m_nPressedBmp; else bmpID = m_nFocusedBmp; } // 显示按钮图案。 CDC* pDC = CDC::FromHandle(lpDIS->hDC); memDC.CreateCompatibleDC(pDC); if(bitmap.LoadBitmap(bmpID)) { bitmap.GetBitmap(&bm); memDC.SelectObject(&bitmap); pDC->StretchBlt(itemRect.left,itemRect.top,itemRect.Width(),itemRect.Height(),&memDC,0,0, bm.bmWidth,bm.bmHeight,SRCCOPY); }} 不重写一个组件不行吗?我没有使用MFC啊,能不能说一下具体办法啊? m_BitMapBN.EnableWindow(FALSE); m_BitMapBN.SetBitmap(LoadBitmap(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDB_BITMAP5)));这是我在MFC中做的一个测试,后面的图片设置上去后显示出来是一团啊,现在我不太清楚的是EnableWindow(FALSE)后设怎么设置按钮的图片? 也就是显示不出,设置的图片是一幅图,只是后来EnableWindow(FALSE)要设置按钮为DISABLED,之后设置同一幅图片也显示不出了 http://www.codeguru.com/Cpp/controls/buttonctrl/article.php/c2057 不用MFC,不重写一个组件,能不能做到?怎么做呢??谢谢大家啦!!! disable状态的判断DRAWITEMSTRUCT的itemState == ODS_DISABLED http://www.codeguru.com/Cpp/controls/buttonctrl/advancedbuttons/article.php/c5161/这是不用MFC的自绘Button的例子 goodboyws(深夜不眠者) :不重写继承一个类做不到吗?当按钮Disabled后,直接设一幅Disabled时的图片,这样怎么做呢?谢谢 CBitmapButton::( UINT nIDBitmapResource, UINT nIDBitmapResourceSel = 0, UINT nIDBitmapResourceFocus = 0, UINT nIDBitmapResourceDisabled = 0 ); 你能不能写一个MFC的测试程序(不用另外写一个类的),然后发给我,我的邮件是[email protected],只要设一下正常时的图片和Disabled后的图片,谢谢啦!! 直接用CBitmapButton应该就可以BOOL CBitmapButton::LoadBitmaps( UINT nIDBitmapResource, UINT nIDBitmapResourceSel = 0, UINT nIDBitmapResourceFocus = 0, UINT nIDBitmapResourceDisabled = 0 );我估计是你第四个参数没有给 MFC的方式我知道了,但是不用MFC,怎么实现这种效果呢?(ATL中) 根据MSDN的代码ATLButton修改... MESSAGE_HANDLER(WM_ENABLE, OnEnable)...LRESULT OnEnable(UINT, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { if (wParam == 0) m_nEntry = 3; FireViewChange(); return 0; } HRESULT OnDraw(ATL_DRAWINFO& di) { USES_CONVERSION; //根据nEntry选择不同的位图 if (m_hDIB[m_nEntry]) { HPALETTE hPal = SelectPalette(di.hdcDraw, m_hPal[m_nEntry], TRUE); RealizePalette(di.hdcDraw); PaintDIB(di.hdcDraw, (LPRECT) di.prcBounds, m_hDIB[m_nEntry], &m_rcDIB[m_nEntry], m_hPal[m_nEntry]); SelectPalette(di.hdcDraw, hPal, TRUE); } return 0; } 我是不是需要重新写一个BUTTON类啊,否则MESSAGE_HANDLER(WM_ENABLE, OnEnable)代码放到哪去呢? 是啊,你可以看一下MSDN附带的代码Samples\VC98\atl\atlbutton 我现在不知道在ATL中新写的类怎么跟对话框使用的组件建立联系,在MFC中可以修改变量的类型啊,在ATL中怎么用自己写的类来替换原有的类呢?并且事件也挂上去啊。 直接用CBitmapButton,提供想要的各种状态的位图就行了。 说过在ATL中,哪有CBitmapButton用啊 socket编程怎么把输入的ipv6地址进行赋值连接? C#的多线程问题 急问VC ADO 无客户端连接ORACLE 手工创建的CEdit,打算在他失去焦点时把里面输入的文本读出来 按钮ID不显示 CRichEditCtrl问题 关于磁盘的问题。 请问如何让窗口最小化时,标题栏隐藏?谢谢 C++写的LIB文件,能否在C语言里使用? 用pNewPen->CreatePen(PS_NULL, 1, RGB(255, 255, 255))创建空画笔为什么不对,应如何创建? 关于代码页、字符集、编码之间联系的问题? [DLL]初学者的疑惑
{
// Construction
public:
CCoolButton();// Attributes
public:// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCoolButton)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual BOOL SubclassDlgItem(UINT nID,CWnd *pParent);
//}}AFX_VIRTUAL
{
CDC memDC;
CBitmap bitmap;
BITMAP bm;
UINT bmpID; CRect itemRect = lpDIS->rcItem;
UINT IsPressed = lpDIS->itemState & ODS_SELECTED;
UINT IsDisabled = lpDIS->itemState & ODS_DISABLED;
bmpID = m_nNormalBmp;
if(IsPressed)
bmpID = m_nPressedBmp;
if(IsDisabled)
bmpID = m_nDisabledBmp;
if(m_bMouseOnButton)
{
if(IsPressed)
bmpID = m_nPressedBmp;
else
bmpID = m_nFocusedBmp;
}
// 显示按钮图案。
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
memDC.CreateCompatibleDC(pDC);
if(bitmap.LoadBitmap(bmpID))
{
bitmap.GetBitmap(&bm);
memDC.SelectObject(&bitmap);
pDC->StretchBlt(itemRect.left,itemRect.top,itemRect.Width(),itemRect.Height(),&memDC,0,0,
bm.bmWidth,bm.bmHeight,SRCCOPY);
}
}
m_BitMapBN.SetBitmap(LoadBitmap(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDB_BITMAP5)));
这是我在MFC中做的一个测试,后面的图片设置上去后显示出来是一团啊,现在我不太清楚的是EnableWindow(FALSE)后设怎么设置按钮的图片?
DRAWITEMSTRUCT的itemState == ODS_DISABLED
这是不用MFC的自绘Button的例子
BOOL CBitmapButton::LoadBitmaps( UINT nIDBitmapResource, UINT nIDBitmapResourceSel = 0, UINT nIDBitmapResourceFocus = 0, UINT nIDBitmapResourceDisabled = 0 );我估计是你第四个参数没有给
...
MESSAGE_HANDLER(WM_ENABLE, OnEnable)
...
LRESULT OnEnable(UINT, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
if (wParam == 0)
m_nEntry = 3; FireViewChange();
return 0;
}
HRESULT OnDraw(ATL_DRAWINFO& di)
{
USES_CONVERSION;
//根据nEntry选择不同的位图
if (m_hDIB[m_nEntry])
{
HPALETTE hPal = SelectPalette(di.hdcDraw, m_hPal[m_nEntry], TRUE);
RealizePalette(di.hdcDraw);
PaintDIB(di.hdcDraw, (LPRECT) di.prcBounds, m_hDIB[m_nEntry], &m_rcDIB[m_nEntry], m_hPal[m_nEntry]);
SelectPalette(di.hdcDraw, hPal, TRUE);
}
return 0;
}
Samples\VC98\atl\atlbutton