如何实现位图按钮

Elysium 2006-09-30 03:15:42
U/D/F/X是可以的,如果标题是中文的呢,或者存在两个相同的标题?又或者位图超过256色?
...全文
639 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
liwei84516 2006-10-24
  • 打赏
  • 举报
回复
Too easy !!
oicqhf 2006-10-24
  • 打赏
  • 举报
回复
可以自己从CButton中派生一个类,具体实现方法类似于CBitmapButton,然后在DrawItem中用pDC->TextOut()来显示标题,标题相同无所谓,只要ID不同就可以。超过256色的位图可以用工具(ACDSee or Photoshop)先转成gif格式,然后再由gif格式的图片转成bmp格式的就可以了。
下面是我写的CButton派生出来的类,仅供参考
void CBitmapBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct != NULL);
// must have at least the first bitmap loaded before calling DrawItem
ASSERT(m_bitmap.m_hObject != NULL); // required

BOOL bIsPressed = (lpDrawItemStruct->itemState & ODS_SELECTED);

// use the main bitmap for up, the selected bitmap for down
CBitmap* pBitmap = &m_bitmap;
UINT state = lpDrawItemStruct->itemState;
if ((state & ODS_SELECTED) && m_bitmapSel.m_hObject != NULL)
pBitmap = &m_bitmapSel;
else if ((state & ODS_FOCUS) && m_bitmapFocus.m_hObject != NULL)
pBitmap = &m_bitmapFocus; // third image for focused
else if ((state & ODS_DISABLED) && m_bitmapDisabled.m_hObject != NULL)
pBitmap = &m_bitmapDisabled; // last image for disabled

// draw the whole button
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap* pOld = memDC.SelectObject(pBitmap);
if (pOld == NULL)
return; // destructors will clean up

CRect rect;
rect.CopyRect(&lpDrawItemStruct->rcItem);
pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height()-15,
&memDC, 0, 0, SRCCOPY);
CString szCaption;
GetWindowText(szCaption);
CRect txtRect(rect.left,rect.Height()-15,rect.right,rect.Height());

if (bIsPressed)
{
pDC->DrawText(szCaption,&txtRect,DT_CENTER | DT_TOP | DT_SINGLELINE);
pDC->Draw3dRect(rect,RGB(0,0,0),RGB(255,255,255));
}
else
{
pDC->DrawText(szCaption,&txtRect,DT_CENTER | DT_TOP | DT_SINGLELINE);
pDC->Draw3dRect(rect,RGB(255,255,255),RGB(0,0,0));
}

memDC.SelectObject(pOld);

ReleaseDC(pDC);
}

// LoadBitmaps will load in one, two, three or all four bitmaps
// returns TRUE if all specified images are loaded
BOOL CBitmapBtn::LoadBitmaps(LPCTSTR lpszBitmapResource,
LPCTSTR lpszBitmapResourceSel, LPCTSTR lpszBitmapResourceFocus,
LPCTSTR lpszBitmapResourceDisabled)
{
// delete old bitmaps (if present)
m_bitmap.DeleteObject();
m_bitmapSel.DeleteObject();
m_bitmapFocus.DeleteObject();
m_bitmapDisabled.DeleteObject();

if (!m_bitmap.LoadBitmap(lpszBitmapResource))
{
TRACE0("Failed to load bitmap for normal image.\n");
return FALSE; // need this one image
}
BOOL bAllLoaded = TRUE;
if (lpszBitmapResourceSel != NULL)
{
if (!m_bitmapSel.LoadBitmap(lpszBitmapResourceSel))
{
TRACE0("Failed to load bitmap for selected image.\n");
bAllLoaded = FALSE;
}
}
if (lpszBitmapResourceFocus != NULL)
{
if (!m_bitmapFocus.LoadBitmap(lpszBitmapResourceFocus))
bAllLoaded = FALSE;
}
if (lpszBitmapResourceDisabled != NULL)
{
if (!m_bitmapDisabled.LoadBitmap(lpszBitmapResourceDisabled))
bAllLoaded = FALSE;
}
return bAllLoaded;
}

// SizeToContent will resize the button to the size of the bitmap
void CBitmapBtn::SizeToContent()
{
ASSERT(m_bitmap.m_hObject != NULL);
CSize bitmapSize;
BITMAP bmInfo;
VERIFY(m_bitmap.GetObject(sizeof(bmInfo), &bmInfo) == sizeof(bmInfo));
VERIFY(SetWindowPos(NULL, -1, -1, bmInfo.bmWidth, bmInfo.bmHeight+15,
SWP_NOMOVE|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE));
}

// Autoload will load the bitmap resources based on the text of
// the button
// Using suffices "U", "D", "F" and "X" for up/down/focus/disabled
BOOL CBitmapBtn::AutoLoad(UINT nID, CWnd* pParent)
{
// first attach the CBitmapButton to the dialog control
// if (!SubclassDlgItem(nID, pParent))
// return FALSE;

CString buttonName;
GetWindowText(buttonName);
ASSERT(!buttonName.IsEmpty()); // must provide a title

LoadBitmaps(buttonName + _T("U"), buttonName + _T("D"),
buttonName + _T("F"), buttonName + _T("X"));

// we need at least the primary
if (m_bitmap.m_hObject == NULL)
return FALSE;

// size to content
SizeToContent();
return TRUE;
}

IMPLEMENT_DYNAMIC(CBitmapBtn, CButton)
Elysium 2006-10-24
  • 打赏
  • 举报
回复
有用过BCGPButton的么?我想知道BCG与CButtonST孰优孰劣
烨炜带火 2006-10-24
  • 打赏
  • 举报
回复
UP
sanbao439 2006-10-23
  • 打赏
  • 举报
回复
CBitmapButton类的LoadBitmaps
具体可看msdn
「已注销」 2006-10-19
  • 打赏
  • 举报
回复
不是据说不错,是确实不错,推荐
MSDN里的CBITMAPBUTTON例子
Example

CBitmapButton myButton;

// Create the bitmap button (must include the BS_OWNERDRAW style).
myButton.Create(NULL, WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,
CRect(10,10,100,100), pParentWnd, 1);

// Load the bitmaps for this button.
myButton.LoadBitmaps(IDB_UP, IDB_DOWN, IDB_FOCUS, IDB_DISABLE);


Elysium 2006-10-19
  • 打赏
  • 举报
回复
效果还行,另外据说CButtonST也不错
DentistryDoctor 2006-09-30
  • 打赏
  • 举报
回复
http://www.codeproject.com/buttonctrl/
DentistryDoctor 2006-09-30
  • 打赏
  • 举报
回复
自绘,在DrawItem中显示位图即可。

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧