m_playButton.SetBitmap(m_hBmp);
用这两行代码已经可以帮一个按钮贴上位图,但总感觉效果不好,尤其按钮的边缘仍然不能为位图所覆盖
所以想请教用CBitmapButton替按钮贴位图的,是不是那样效果比较好?
一定请给出代码,谢谢了
7 个解决方案
#1
void CDatabaseLogin::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC buttomDC;
CBitmap bitmapTrans;
BITMAP bmp;
CDC mem;
CRect rc;
buttomDC.Attach(lpDrawItemStruct->hDC);
mem.CreateCompatibleDC(&buttomDC);
rc=lpDrawItemStruct->rcItem;
UINT state = lpDrawItemStruct->itemState;
if(state & ODS_FOCUS)
{
bitmapTrans.LoadBitmap(IDB_BTN_DOWN);
bitmapTrans.GetBitmap(&bmp);
CBitmap* old=mem.SelectObject(&bitmapTrans);
buttomDC.StretchBlt(rc.left,rc.top,rc.right,rc.bottom,&mem,
0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
mem.SelectObject(old);
bitmapTrans.DeleteObject();
buttomDC.SetBkMode(TRANSPARENT);
if(nIDCtl == IDC_ODBC_SQL_SAVE){
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
else
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
else
{
bitmapTrans.LoadBitmap(IDB_GETFOCUS);
bitmapTrans.GetBitmap(&bmp);
CBitmap* old2=mem.SelectObject(&bitmapTrans);
buttomDC.StretchBlt(rc.left,rc.top,rc.right,rc.bottom,&mem,
0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
mem.SelectObject(old2);
bitmapTrans.DeleteObject();
buttomDC.SetBkMode(TRANSPARENT);
if(nIDCtl == IDC_ODBC_SQL_SAVE){
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
else
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
CFormView::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
{
CDC buttomDC;
CBitmap bitmapTrans;
BITMAP bmp;
CDC mem;
CRect rc;
buttomDC.Attach(lpDrawItemStruct->hDC);
mem.CreateCompatibleDC(&buttomDC);
rc=lpDrawItemStruct->rcItem;
UINT state = lpDrawItemStruct->itemState;
if(state & ODS_FOCUS)
{
bitmapTrans.LoadBitmap(IDB_BTN_DOWN);
bitmapTrans.GetBitmap(&bmp);
CBitmap* old=mem.SelectObject(&bitmapTrans);
buttomDC.StretchBlt(rc.left,rc.top,rc.right,rc.bottom,&mem,
0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
mem.SelectObject(old);
bitmapTrans.DeleteObject();
buttomDC.SetBkMode(TRANSPARENT);
if(nIDCtl == IDC_ODBC_SQL_SAVE){
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
else
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
else
{
bitmapTrans.LoadBitmap(IDB_GETFOCUS);
bitmapTrans.GetBitmap(&bmp);
CBitmap* old2=mem.SelectObject(&bitmapTrans);
buttomDC.StretchBlt(rc.left,rc.top,rc.right,rc.bottom,&mem,
0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
mem.SelectObject(old2);
bitmapTrans.DeleteObject();
buttomDC.SetBkMode(TRANSPARENT);
if(nIDCtl == IDC_ODBC_SQL_SAVE){
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
else
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
CFormView::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
#2
蓝影写得很对哦,不过在创建按钮时不要忘记设定“Owner Draw”属性
#3
bphantom(蓝影) 写的非常好,我再提供一种方法:
1、如果在CDialog类型或其继承类
(1)、在对话框适当位置添加按钮,为其"Caption"设置一个值,假设为"MYBUTTON",为其指定ID号,假设为"IDC_MYBUTTON",并设定“Owner Draw”属性
(2)、做4个位图,分别用于当按钮处在“UP”、“DOWN”、“FOCUS”、“DISABLE”时显示,其ID分别为"MYBUTTONU"、"MYBUTTOND"、"MYBUTTONF"、"MYBUTTONX",注意,不要写成MYBUTTONU、MYBUTTOND、MYBUTTONF、MYBUTTONX;
(3)、在对话框类的结构中(.h)加入如下代码:
CBitmapButton m_wndBitmapButton;
(3)、在对话框的初始函数(InitDialog)中加入如下代码:
m_wndBitmapButton.AutoLoad(IDC_MYBUTTON,this);
1、如果在CDialog类型或其继承类
(1)、在对话框适当位置添加按钮,为其"Caption"设置一个值,假设为"MYBUTTON",为其指定ID号,假设为"IDC_MYBUTTON",并设定“Owner Draw”属性
(2)、做4个位图,分别用于当按钮处在“UP”、“DOWN”、“FOCUS”、“DISABLE”时显示,其ID分别为"MYBUTTONU"、"MYBUTTOND"、"MYBUTTONF"、"MYBUTTONX",注意,不要写成MYBUTTONU、MYBUTTOND、MYBUTTONF、MYBUTTONX;
(3)、在对话框类的结构中(.h)加入如下代码:
CBitmapButton m_wndBitmapButton;
(3)、在对话框的初始函数(InitDialog)中加入如下代码:
m_wndBitmapButton.AutoLoad(IDC_MYBUTTON,this);
#4
// MyButton.cpp : implementation file
//
#include "stdafx.h"
#include "DialogTest7.h"
#include "MyButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyButton
CMyButton::CMyButton()
{
m_bIsMouseMove = FALSE;
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
//{{AFX_MSG_MAP(CMyButton)
ON_WM_MOUSEMOVE()
ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyButton message handlers
void CMyButton::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
ModifyStyle(0, BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if( !m_bIsMouseMove )
{
m_bIsMouseMove = TRUE;
Invalidate();
TRACKMOUSEEVENT trackmouseevent;
trackmouseevent.cbSize = sizeof(trackmouseevent);
trackmouseevent.dwFlags = TME_LEAVE ;
trackmouseevent.hwndTrack = GetSafeHwnd();
trackmouseevent.dwHoverTime = HOVER_DEFAULT;
_TrackMouseEvent(&trackmouseevent);
}
CButton::OnMouseMove(nFlags, point);
}
#define WM_MOUSEOFFBUTTON WM_USER+101
LONG CMyButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
m_bIsMouseMove = FALSE;
::PostMessage(GetParent()->GetSafeHwnd(), WM_MOUSEOFFBUTTON, 0, 0);
Invalidate();
return 0;
}
#define WM_MOUSEONBUTTON WM_USER+100
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct )
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
DWORD nState = lpDrawItemStruct->itemState;
DWORD nAction = lpDrawItemStruct->itemAction;
CRect rc = lpDrawItemStruct->rcItem;
UINT uStyle = DFCS_BUTTONPUSH;
pDC->SetBkMode(TRANSPARENT);
CString strText;
GetWindowText(strText);
if (nState & ODS_SELECTED)//select
{
m_bIsMouseMove = FALSE;
DrawSelectedState(rc, pDC);
}
else //Normal
{
DrawNormalState(rc, pDC);
}
if (m_bIsMouseMove)//hover on the button
{
DrawFocusState(rc, pDC);
::PostMessage(GetParent()->GetSafeHwnd(), WM_MOUSEONBUTTON, 0, 0);
}
pDC->DrawText(strText, strText.GetLength(),
&lpDrawItemStruct->rcItem,
DT_SINGLELINE|DT_VCENTER|DT_CENTER);
}
void CMyButton::DrawNormalState(CRect rc, CDC *pDC)
{
//DrawFrame(rc, pDC);
// DrawFace(rc, RGB(223, 233, 255), RGB(0, 128, 255), pDC);
// DrawFrame(rc, RGB(230, 230, 230), RGB(128, 128, 128), pDC);
// pDC->SetTextColor(RGB(255, 255, 255));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmNormal, DSS_NORMAL);
}
void CMyButton::DrawSelectedState(CRect rc, CDC *pDC)
{
// DrawFace(rc, RGB(255, 255, 255), RGB(0, 255, 128), pDC);
// DrawFrame(rc, RGB(255, 255, 128), RGB(0, 255, 255), pDC);
// pDC->SetTextColor(RGB(0, 128, 128));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmSelect, DSS_NORMAL);
}
void CMyButton::DrawFocusState(CRect rc, CDC *pDC)
{
// DrawFace(rc, RGB(255, 255, 255), RGB(128, 255, 128), pDC);
// DrawFrame(rc, RGB(255, 255, 255), RGB(128, 255, 128), pDC);
// pDC->SetTextColor(RGB(0, 128, 255));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmFocuse, DSS_NORMAL);
}
void CMyButton::DrawFace(CRect &rc, COLORREF clrTopRight, COLORREF clrBottomLeft,
CDC* pDC)
{
CPen Pen;
CPen* pOldPen = pDC->SelectObject(&Pen);
int R,G, B;
R = (GetRValue(clrTopRight) - GetRValue(clrBottomLeft)) / rc.Height();
G = (GetGValue(clrTopRight) - GetGValue(clrBottomLeft)) / rc.Height();
B = (GetBValue(clrTopRight) - GetBValue(clrBottomLeft)) / rc.Height();
int nColR = GetRValue(clrTopRight);
int nColG = GetGValue(clrTopRight);
int nColB = GetBValue(clrTopRight);
COLORREF clrColMax = clrTopRight > clrBottomLeft ? clrTopRight : clrBottomLeft;
COLORREF clrColMin = clrTopRight > clrBottomLeft ? clrBottomLeft: clrTopRight;
for(int i=0; i<rc.Height(); i++)
{
nColR -= R;
nColG -= G;
nColB -= B;
Pen.CreatePen(PS_SOLID, 1, RGB(nColR, nColG, nColB));
pDC->SelectObject(&Pen);
pDC->MoveTo(rc.left, rc.top+i);
pDC->LineTo(rc.right, rc.top+i);
Pen.DeleteObject();
}
pDC->SelectObject(pOldPen);
}
void CMyButton::DrawFrame(CRect& rc, COLORREF clrTopRight, COLORREF clrBottomLeft,
CDC* pDC)
{
CBrush NullBrush;
NullBrush.CreateStockObject(NULL_BRUSH);
CBrush* pOldBrush = pDC->SelectObject(&NullBrush);
CPen Pen;
Pen.CreatePen(PS_SOLID, 1, RGB(0, 64, 128));
CPen* pOldPen = pDC->SelectObject(&Pen);
pDC->RoundRect(rc, CPoint(3, 3) );
rc.DeflateRect(1, 1, 1, 1);
pDC->Draw3dRect(rc, clrTopRight, clrBottomLeft);
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
}
void CMyButton::SetBitmaps(UINT nNormal, UINT nSelect, UINT nFocuse)
{
m_bmNormal.LoadBitmap(nNormal);
m_bmSelect.LoadBitmap(nSelect);
m_bmFocuse.LoadBitmap(nFocuse);
}
void CMyButton::OnClicked()
{
// TODO: Add your control notification handler code here
}
//
#include "stdafx.h"
#include "DialogTest7.h"
#include "MyButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyButton
CMyButton::CMyButton()
{
m_bIsMouseMove = FALSE;
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
//{{AFX_MSG_MAP(CMyButton)
ON_WM_MOUSEMOVE()
ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyButton message handlers
void CMyButton::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
ModifyStyle(0, BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if( !m_bIsMouseMove )
{
m_bIsMouseMove = TRUE;
Invalidate();
TRACKMOUSEEVENT trackmouseevent;
trackmouseevent.cbSize = sizeof(trackmouseevent);
trackmouseevent.dwFlags = TME_LEAVE ;
trackmouseevent.hwndTrack = GetSafeHwnd();
trackmouseevent.dwHoverTime = HOVER_DEFAULT;
_TrackMouseEvent(&trackmouseevent);
}
CButton::OnMouseMove(nFlags, point);
}
#define WM_MOUSEOFFBUTTON WM_USER+101
LONG CMyButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
m_bIsMouseMove = FALSE;
::PostMessage(GetParent()->GetSafeHwnd(), WM_MOUSEOFFBUTTON, 0, 0);
Invalidate();
return 0;
}
#define WM_MOUSEONBUTTON WM_USER+100
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct )
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
DWORD nState = lpDrawItemStruct->itemState;
DWORD nAction = lpDrawItemStruct->itemAction;
CRect rc = lpDrawItemStruct->rcItem;
UINT uStyle = DFCS_BUTTONPUSH;
pDC->SetBkMode(TRANSPARENT);
CString strText;
GetWindowText(strText);
if (nState & ODS_SELECTED)//select
{
m_bIsMouseMove = FALSE;
DrawSelectedState(rc, pDC);
}
else //Normal
{
DrawNormalState(rc, pDC);
}
if (m_bIsMouseMove)//hover on the button
{
DrawFocusState(rc, pDC);
::PostMessage(GetParent()->GetSafeHwnd(), WM_MOUSEONBUTTON, 0, 0);
}
pDC->DrawText(strText, strText.GetLength(),
&lpDrawItemStruct->rcItem,
DT_SINGLELINE|DT_VCENTER|DT_CENTER);
}
void CMyButton::DrawNormalState(CRect rc, CDC *pDC)
{
//DrawFrame(rc, pDC);
// DrawFace(rc, RGB(223, 233, 255), RGB(0, 128, 255), pDC);
// DrawFrame(rc, RGB(230, 230, 230), RGB(128, 128, 128), pDC);
// pDC->SetTextColor(RGB(255, 255, 255));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmNormal, DSS_NORMAL);
}
void CMyButton::DrawSelectedState(CRect rc, CDC *pDC)
{
// DrawFace(rc, RGB(255, 255, 255), RGB(0, 255, 128), pDC);
// DrawFrame(rc, RGB(255, 255, 128), RGB(0, 255, 255), pDC);
// pDC->SetTextColor(RGB(0, 128, 128));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmSelect, DSS_NORMAL);
}
void CMyButton::DrawFocusState(CRect rc, CDC *pDC)
{
// DrawFace(rc, RGB(255, 255, 255), RGB(128, 255, 128), pDC);
// DrawFrame(rc, RGB(255, 255, 255), RGB(128, 255, 128), pDC);
// pDC->SetTextColor(RGB(0, 128, 255));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmFocuse, DSS_NORMAL);
}
void CMyButton::DrawFace(CRect &rc, COLORREF clrTopRight, COLORREF clrBottomLeft,
CDC* pDC)
{
CPen Pen;
CPen* pOldPen = pDC->SelectObject(&Pen);
int R,G, B;
R = (GetRValue(clrTopRight) - GetRValue(clrBottomLeft)) / rc.Height();
G = (GetGValue(clrTopRight) - GetGValue(clrBottomLeft)) / rc.Height();
B = (GetBValue(clrTopRight) - GetBValue(clrBottomLeft)) / rc.Height();
int nColR = GetRValue(clrTopRight);
int nColG = GetGValue(clrTopRight);
int nColB = GetBValue(clrTopRight);
COLORREF clrColMax = clrTopRight > clrBottomLeft ? clrTopRight : clrBottomLeft;
COLORREF clrColMin = clrTopRight > clrBottomLeft ? clrBottomLeft: clrTopRight;
for(int i=0; i<rc.Height(); i++)
{
nColR -= R;
nColG -= G;
nColB -= B;
Pen.CreatePen(PS_SOLID, 1, RGB(nColR, nColG, nColB));
pDC->SelectObject(&Pen);
pDC->MoveTo(rc.left, rc.top+i);
pDC->LineTo(rc.right, rc.top+i);
Pen.DeleteObject();
}
pDC->SelectObject(pOldPen);
}
void CMyButton::DrawFrame(CRect& rc, COLORREF clrTopRight, COLORREF clrBottomLeft,
CDC* pDC)
{
CBrush NullBrush;
NullBrush.CreateStockObject(NULL_BRUSH);
CBrush* pOldBrush = pDC->SelectObject(&NullBrush);
CPen Pen;
Pen.CreatePen(PS_SOLID, 1, RGB(0, 64, 128));
CPen* pOldPen = pDC->SelectObject(&Pen);
pDC->RoundRect(rc, CPoint(3, 3) );
rc.DeflateRect(1, 1, 1, 1);
pDC->Draw3dRect(rc, clrTopRight, clrBottomLeft);
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
}
void CMyButton::SetBitmaps(UINT nNormal, UINT nSelect, UINT nFocuse)
{
m_bmNormal.LoadBitmap(nNormal);
m_bmSelect.LoadBitmap(nSelect);
m_bmFocuse.LoadBitmap(nFocuse);
}
void CMyButton::OnClicked()
{
// TODO: Add your control notification handler code here
}
#5
http://www.vckbase.com/document/viewdoc.asp?id=518
#6
用CBitmapButton的方法如下,如要例子代码请留下Email;
1、如果在CDialog类型或其继承类
(1)、在对话框适当位置添加按钮,为其"Caption"设置一个值,假设为"MYBUTTON",为其指定ID号,假设为"IDC_MYBUTTON",并设定“Owner Draw”属性
(2)、做4个位图,分别用于当按钮处在“UP”、“DOWN”、“FOCUS”、“DISABLE”时显示,其ID分别为"MYBUTTONU"、"MYBUTTOND"、"MYBUTTONF"、"MYBUTTONX",注意,不要写成MYBUTTONU、MYBUTTOND、MYBUTTONF、MYBUTTONX;
(3)、在对话框类的结构中(.h)加入如下代码:
CBitmapButton m_wndBitmapButton;
(4)、在对话框的初始函数(OnInitDialog)中加入如下代码:
m_wndBitmapButton.AutoLoad(IDC_MYBUTTON,this);
(5)、编译并运行
2、其它情况
(1)、做4个位图,分别用于当按钮处在“UP”、“DOWN”、“FOCUS”、“DISABLE”时显示,对ID无要求;
(2)、声明CBitmapButton对象如:CBitmapButton m_wndBitmapButton;
(3)、调用Create()函数创建按钮
(4)、调用LoadBitmaps()函数装入位图。
函数原形如下:
BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );
BOOL LoadBitmaps( UINT nIDBitmapResource, UINT nIDBitmapResourceSel = 0, UINT nIDBitmapResourceFocus = 0, UINT nIDBitmapResourceDisabled = 0 );
1、如果在CDialog类型或其继承类
(1)、在对话框适当位置添加按钮,为其"Caption"设置一个值,假设为"MYBUTTON",为其指定ID号,假设为"IDC_MYBUTTON",并设定“Owner Draw”属性
(2)、做4个位图,分别用于当按钮处在“UP”、“DOWN”、“FOCUS”、“DISABLE”时显示,其ID分别为"MYBUTTONU"、"MYBUTTOND"、"MYBUTTONF"、"MYBUTTONX",注意,不要写成MYBUTTONU、MYBUTTOND、MYBUTTONF、MYBUTTONX;
(3)、在对话框类的结构中(.h)加入如下代码:
CBitmapButton m_wndBitmapButton;
(4)、在对话框的初始函数(OnInitDialog)中加入如下代码:
m_wndBitmapButton.AutoLoad(IDC_MYBUTTON,this);
(5)、编译并运行
2、其它情况
(1)、做4个位图,分别用于当按钮处在“UP”、“DOWN”、“FOCUS”、“DISABLE”时显示,对ID无要求;
(2)、声明CBitmapButton对象如:CBitmapButton m_wndBitmapButton;
(3)、调用Create()函数创建按钮
(4)、调用LoadBitmaps()函数装入位图。
函数原形如下:
BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );
BOOL LoadBitmaps( UINT nIDBitmapResource, UINT nIDBitmapResourceSel = 0, UINT nIDBitmapResourceFocus = 0, UINT nIDBitmapResourceDisabled = 0 );
#7
http://www.vckbase.com/document/viewdoc.asp?id=518
有CButtonST的源码及例子
有CButtonST的源码及例子
#1
void CDatabaseLogin::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC buttomDC;
CBitmap bitmapTrans;
BITMAP bmp;
CDC mem;
CRect rc;
buttomDC.Attach(lpDrawItemStruct->hDC);
mem.CreateCompatibleDC(&buttomDC);
rc=lpDrawItemStruct->rcItem;
UINT state = lpDrawItemStruct->itemState;
if(state & ODS_FOCUS)
{
bitmapTrans.LoadBitmap(IDB_BTN_DOWN);
bitmapTrans.GetBitmap(&bmp);
CBitmap* old=mem.SelectObject(&bitmapTrans);
buttomDC.StretchBlt(rc.left,rc.top,rc.right,rc.bottom,&mem,
0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
mem.SelectObject(old);
bitmapTrans.DeleteObject();
buttomDC.SetBkMode(TRANSPARENT);
if(nIDCtl == IDC_ODBC_SQL_SAVE){
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
else
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
else
{
bitmapTrans.LoadBitmap(IDB_GETFOCUS);
bitmapTrans.GetBitmap(&bmp);
CBitmap* old2=mem.SelectObject(&bitmapTrans);
buttomDC.StretchBlt(rc.left,rc.top,rc.right,rc.bottom,&mem,
0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
mem.SelectObject(old2);
bitmapTrans.DeleteObject();
buttomDC.SetBkMode(TRANSPARENT);
if(nIDCtl == IDC_ODBC_SQL_SAVE){
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
else
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
CFormView::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
{
CDC buttomDC;
CBitmap bitmapTrans;
BITMAP bmp;
CDC mem;
CRect rc;
buttomDC.Attach(lpDrawItemStruct->hDC);
mem.CreateCompatibleDC(&buttomDC);
rc=lpDrawItemStruct->rcItem;
UINT state = lpDrawItemStruct->itemState;
if(state & ODS_FOCUS)
{
bitmapTrans.LoadBitmap(IDB_BTN_DOWN);
bitmapTrans.GetBitmap(&bmp);
CBitmap* old=mem.SelectObject(&bitmapTrans);
buttomDC.StretchBlt(rc.left,rc.top,rc.right,rc.bottom,&mem,
0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
mem.SelectObject(old);
bitmapTrans.DeleteObject();
buttomDC.SetBkMode(TRANSPARENT);
if(nIDCtl == IDC_ODBC_SQL_SAVE){
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
else
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
else
{
bitmapTrans.LoadBitmap(IDB_GETFOCUS);
bitmapTrans.GetBitmap(&bmp);
CBitmap* old2=mem.SelectObject(&bitmapTrans);
buttomDC.StretchBlt(rc.left,rc.top,rc.right,rc.bottom,&mem,
0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
mem.SelectObject(old2);
bitmapTrans.DeleteObject();
buttomDC.SetBkMode(TRANSPARENT);
if(nIDCtl == IDC_ODBC_SQL_SAVE){
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
else
buttomDC.DrawText("保存",&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
CFormView::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
#2
蓝影写得很对哦,不过在创建按钮时不要忘记设定“Owner Draw”属性
#3
bphantom(蓝影) 写的非常好,我再提供一种方法:
1、如果在CDialog类型或其继承类
(1)、在对话框适当位置添加按钮,为其"Caption"设置一个值,假设为"MYBUTTON",为其指定ID号,假设为"IDC_MYBUTTON",并设定“Owner Draw”属性
(2)、做4个位图,分别用于当按钮处在“UP”、“DOWN”、“FOCUS”、“DISABLE”时显示,其ID分别为"MYBUTTONU"、"MYBUTTOND"、"MYBUTTONF"、"MYBUTTONX",注意,不要写成MYBUTTONU、MYBUTTOND、MYBUTTONF、MYBUTTONX;
(3)、在对话框类的结构中(.h)加入如下代码:
CBitmapButton m_wndBitmapButton;
(3)、在对话框的初始函数(InitDialog)中加入如下代码:
m_wndBitmapButton.AutoLoad(IDC_MYBUTTON,this);
1、如果在CDialog类型或其继承类
(1)、在对话框适当位置添加按钮,为其"Caption"设置一个值,假设为"MYBUTTON",为其指定ID号,假设为"IDC_MYBUTTON",并设定“Owner Draw”属性
(2)、做4个位图,分别用于当按钮处在“UP”、“DOWN”、“FOCUS”、“DISABLE”时显示,其ID分别为"MYBUTTONU"、"MYBUTTOND"、"MYBUTTONF"、"MYBUTTONX",注意,不要写成MYBUTTONU、MYBUTTOND、MYBUTTONF、MYBUTTONX;
(3)、在对话框类的结构中(.h)加入如下代码:
CBitmapButton m_wndBitmapButton;
(3)、在对话框的初始函数(InitDialog)中加入如下代码:
m_wndBitmapButton.AutoLoad(IDC_MYBUTTON,this);
#4
// MyButton.cpp : implementation file
//
#include "stdafx.h"
#include "DialogTest7.h"
#include "MyButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyButton
CMyButton::CMyButton()
{
m_bIsMouseMove = FALSE;
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
//{{AFX_MSG_MAP(CMyButton)
ON_WM_MOUSEMOVE()
ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyButton message handlers
void CMyButton::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
ModifyStyle(0, BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if( !m_bIsMouseMove )
{
m_bIsMouseMove = TRUE;
Invalidate();
TRACKMOUSEEVENT trackmouseevent;
trackmouseevent.cbSize = sizeof(trackmouseevent);
trackmouseevent.dwFlags = TME_LEAVE ;
trackmouseevent.hwndTrack = GetSafeHwnd();
trackmouseevent.dwHoverTime = HOVER_DEFAULT;
_TrackMouseEvent(&trackmouseevent);
}
CButton::OnMouseMove(nFlags, point);
}
#define WM_MOUSEOFFBUTTON WM_USER+101
LONG CMyButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
m_bIsMouseMove = FALSE;
::PostMessage(GetParent()->GetSafeHwnd(), WM_MOUSEOFFBUTTON, 0, 0);
Invalidate();
return 0;
}
#define WM_MOUSEONBUTTON WM_USER+100
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct )
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
DWORD nState = lpDrawItemStruct->itemState;
DWORD nAction = lpDrawItemStruct->itemAction;
CRect rc = lpDrawItemStruct->rcItem;
UINT uStyle = DFCS_BUTTONPUSH;
pDC->SetBkMode(TRANSPARENT);
CString strText;
GetWindowText(strText);
if (nState & ODS_SELECTED)//select
{
m_bIsMouseMove = FALSE;
DrawSelectedState(rc, pDC);
}
else //Normal
{
DrawNormalState(rc, pDC);
}
if (m_bIsMouseMove)//hover on the button
{
DrawFocusState(rc, pDC);
::PostMessage(GetParent()->GetSafeHwnd(), WM_MOUSEONBUTTON, 0, 0);
}
pDC->DrawText(strText, strText.GetLength(),
&lpDrawItemStruct->rcItem,
DT_SINGLELINE|DT_VCENTER|DT_CENTER);
}
void CMyButton::DrawNormalState(CRect rc, CDC *pDC)
{
//DrawFrame(rc, pDC);
// DrawFace(rc, RGB(223, 233, 255), RGB(0, 128, 255), pDC);
// DrawFrame(rc, RGB(230, 230, 230), RGB(128, 128, 128), pDC);
// pDC->SetTextColor(RGB(255, 255, 255));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmNormal, DSS_NORMAL);
}
void CMyButton::DrawSelectedState(CRect rc, CDC *pDC)
{
// DrawFace(rc, RGB(255, 255, 255), RGB(0, 255, 128), pDC);
// DrawFrame(rc, RGB(255, 255, 128), RGB(0, 255, 255), pDC);
// pDC->SetTextColor(RGB(0, 128, 128));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmSelect, DSS_NORMAL);
}
void CMyButton::DrawFocusState(CRect rc, CDC *pDC)
{
// DrawFace(rc, RGB(255, 255, 255), RGB(128, 255, 128), pDC);
// DrawFrame(rc, RGB(255, 255, 255), RGB(128, 255, 128), pDC);
// pDC->SetTextColor(RGB(0, 128, 255));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmFocuse, DSS_NORMAL);
}
void CMyButton::DrawFace(CRect &rc, COLORREF clrTopRight, COLORREF clrBottomLeft,
CDC* pDC)
{
CPen Pen;
CPen* pOldPen = pDC->SelectObject(&Pen);
int R,G, B;
R = (GetRValue(clrTopRight) - GetRValue(clrBottomLeft)) / rc.Height();
G = (GetGValue(clrTopRight) - GetGValue(clrBottomLeft)) / rc.Height();
B = (GetBValue(clrTopRight) - GetBValue(clrBottomLeft)) / rc.Height();
int nColR = GetRValue(clrTopRight);
int nColG = GetGValue(clrTopRight);
int nColB = GetBValue(clrTopRight);
COLORREF clrColMax = clrTopRight > clrBottomLeft ? clrTopRight : clrBottomLeft;
COLORREF clrColMin = clrTopRight > clrBottomLeft ? clrBottomLeft: clrTopRight;
for(int i=0; i<rc.Height(); i++)
{
nColR -= R;
nColG -= G;
nColB -= B;
Pen.CreatePen(PS_SOLID, 1, RGB(nColR, nColG, nColB));
pDC->SelectObject(&Pen);
pDC->MoveTo(rc.left, rc.top+i);
pDC->LineTo(rc.right, rc.top+i);
Pen.DeleteObject();
}
pDC->SelectObject(pOldPen);
}
void CMyButton::DrawFrame(CRect& rc, COLORREF clrTopRight, COLORREF clrBottomLeft,
CDC* pDC)
{
CBrush NullBrush;
NullBrush.CreateStockObject(NULL_BRUSH);
CBrush* pOldBrush = pDC->SelectObject(&NullBrush);
CPen Pen;
Pen.CreatePen(PS_SOLID, 1, RGB(0, 64, 128));
CPen* pOldPen = pDC->SelectObject(&Pen);
pDC->RoundRect(rc, CPoint(3, 3) );
rc.DeflateRect(1, 1, 1, 1);
pDC->Draw3dRect(rc, clrTopRight, clrBottomLeft);
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
}
void CMyButton::SetBitmaps(UINT nNormal, UINT nSelect, UINT nFocuse)
{
m_bmNormal.LoadBitmap(nNormal);
m_bmSelect.LoadBitmap(nSelect);
m_bmFocuse.LoadBitmap(nFocuse);
}
void CMyButton::OnClicked()
{
// TODO: Add your control notification handler code here
}
//
#include "stdafx.h"
#include "DialogTest7.h"
#include "MyButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyButton
CMyButton::CMyButton()
{
m_bIsMouseMove = FALSE;
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
//{{AFX_MSG_MAP(CMyButton)
ON_WM_MOUSEMOVE()
ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyButton message handlers
void CMyButton::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
ModifyStyle(0, BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if( !m_bIsMouseMove )
{
m_bIsMouseMove = TRUE;
Invalidate();
TRACKMOUSEEVENT trackmouseevent;
trackmouseevent.cbSize = sizeof(trackmouseevent);
trackmouseevent.dwFlags = TME_LEAVE ;
trackmouseevent.hwndTrack = GetSafeHwnd();
trackmouseevent.dwHoverTime = HOVER_DEFAULT;
_TrackMouseEvent(&trackmouseevent);
}
CButton::OnMouseMove(nFlags, point);
}
#define WM_MOUSEOFFBUTTON WM_USER+101
LONG CMyButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
m_bIsMouseMove = FALSE;
::PostMessage(GetParent()->GetSafeHwnd(), WM_MOUSEOFFBUTTON, 0, 0);
Invalidate();
return 0;
}
#define WM_MOUSEONBUTTON WM_USER+100
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct )
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
DWORD nState = lpDrawItemStruct->itemState;
DWORD nAction = lpDrawItemStruct->itemAction;
CRect rc = lpDrawItemStruct->rcItem;
UINT uStyle = DFCS_BUTTONPUSH;
pDC->SetBkMode(TRANSPARENT);
CString strText;
GetWindowText(strText);
if (nState & ODS_SELECTED)//select
{
m_bIsMouseMove = FALSE;
DrawSelectedState(rc, pDC);
}
else //Normal
{
DrawNormalState(rc, pDC);
}
if (m_bIsMouseMove)//hover on the button
{
DrawFocusState(rc, pDC);
::PostMessage(GetParent()->GetSafeHwnd(), WM_MOUSEONBUTTON, 0, 0);
}
pDC->DrawText(strText, strText.GetLength(),
&lpDrawItemStruct->rcItem,
DT_SINGLELINE|DT_VCENTER|DT_CENTER);
}
void CMyButton::DrawNormalState(CRect rc, CDC *pDC)
{
//DrawFrame(rc, pDC);
// DrawFace(rc, RGB(223, 233, 255), RGB(0, 128, 255), pDC);
// DrawFrame(rc, RGB(230, 230, 230), RGB(128, 128, 128), pDC);
// pDC->SetTextColor(RGB(255, 255, 255));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmNormal, DSS_NORMAL);
}
void CMyButton::DrawSelectedState(CRect rc, CDC *pDC)
{
// DrawFace(rc, RGB(255, 255, 255), RGB(0, 255, 128), pDC);
// DrawFrame(rc, RGB(255, 255, 128), RGB(0, 255, 255), pDC);
// pDC->SetTextColor(RGB(0, 128, 128));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmSelect, DSS_NORMAL);
}
void CMyButton::DrawFocusState(CRect rc, CDC *pDC)
{
// DrawFace(rc, RGB(255, 255, 255), RGB(128, 255, 128), pDC);
// DrawFrame(rc, RGB(255, 255, 255), RGB(128, 255, 128), pDC);
// pDC->SetTextColor(RGB(0, 128, 255));
pDC->DrawState(CPoint(0, 0), CSize(rc.Width(), rc.Height()),
m_bmFocuse, DSS_NORMAL);
}
void CMyButton::DrawFace(CRect &rc, COLORREF clrTopRight, COLORREF clrBottomLeft,
CDC* pDC)
{
CPen Pen;
CPen* pOldPen = pDC->SelectObject(&Pen);
int R,G, B;
R = (GetRValue(clrTopRight) - GetRValue(clrBottomLeft)) / rc.Height();
G = (GetGValue(clrTopRight) - GetGValue(clrBottomLeft)) / rc.Height();
B = (GetBValue(clrTopRight) - GetBValue(clrBottomLeft)) / rc.Height();
int nColR = GetRValue(clrTopRight);
int nColG = GetGValue(clrTopRight);
int nColB = GetBValue(clrTopRight);
COLORREF clrColMax = clrTopRight > clrBottomLeft ? clrTopRight : clrBottomLeft;
COLORREF clrColMin = clrTopRight > clrBottomLeft ? clrBottomLeft: clrTopRight;
for(int i=0; i<rc.Height(); i++)
{
nColR -= R;
nColG -= G;
nColB -= B;
Pen.CreatePen(PS_SOLID, 1, RGB(nColR, nColG, nColB));
pDC->SelectObject(&Pen);
pDC->MoveTo(rc.left, rc.top+i);
pDC->LineTo(rc.right, rc.top+i);
Pen.DeleteObject();
}
pDC->SelectObject(pOldPen);
}
void CMyButton::DrawFrame(CRect& rc, COLORREF clrTopRight, COLORREF clrBottomLeft,
CDC* pDC)
{
CBrush NullBrush;
NullBrush.CreateStockObject(NULL_BRUSH);
CBrush* pOldBrush = pDC->SelectObject(&NullBrush);
CPen Pen;
Pen.CreatePen(PS_SOLID, 1, RGB(0, 64, 128));
CPen* pOldPen = pDC->SelectObject(&Pen);
pDC->RoundRect(rc, CPoint(3, 3) );
rc.DeflateRect(1, 1, 1, 1);
pDC->Draw3dRect(rc, clrTopRight, clrBottomLeft);
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
}
void CMyButton::SetBitmaps(UINT nNormal, UINT nSelect, UINT nFocuse)
{
m_bmNormal.LoadBitmap(nNormal);
m_bmSelect.LoadBitmap(nSelect);
m_bmFocuse.LoadBitmap(nFocuse);
}
void CMyButton::OnClicked()
{
// TODO: Add your control notification handler code here
}
#5
http://www.vckbase.com/document/viewdoc.asp?id=518
#6
用CBitmapButton的方法如下,如要例子代码请留下Email;
1、如果在CDialog类型或其继承类
(1)、在对话框适当位置添加按钮,为其"Caption"设置一个值,假设为"MYBUTTON",为其指定ID号,假设为"IDC_MYBUTTON",并设定“Owner Draw”属性
(2)、做4个位图,分别用于当按钮处在“UP”、“DOWN”、“FOCUS”、“DISABLE”时显示,其ID分别为"MYBUTTONU"、"MYBUTTOND"、"MYBUTTONF"、"MYBUTTONX",注意,不要写成MYBUTTONU、MYBUTTOND、MYBUTTONF、MYBUTTONX;
(3)、在对话框类的结构中(.h)加入如下代码:
CBitmapButton m_wndBitmapButton;
(4)、在对话框的初始函数(OnInitDialog)中加入如下代码:
m_wndBitmapButton.AutoLoad(IDC_MYBUTTON,this);
(5)、编译并运行
2、其它情况
(1)、做4个位图,分别用于当按钮处在“UP”、“DOWN”、“FOCUS”、“DISABLE”时显示,对ID无要求;
(2)、声明CBitmapButton对象如:CBitmapButton m_wndBitmapButton;
(3)、调用Create()函数创建按钮
(4)、调用LoadBitmaps()函数装入位图。
函数原形如下:
BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );
BOOL LoadBitmaps( UINT nIDBitmapResource, UINT nIDBitmapResourceSel = 0, UINT nIDBitmapResourceFocus = 0, UINT nIDBitmapResourceDisabled = 0 );
1、如果在CDialog类型或其继承类
(1)、在对话框适当位置添加按钮,为其"Caption"设置一个值,假设为"MYBUTTON",为其指定ID号,假设为"IDC_MYBUTTON",并设定“Owner Draw”属性
(2)、做4个位图,分别用于当按钮处在“UP”、“DOWN”、“FOCUS”、“DISABLE”时显示,其ID分别为"MYBUTTONU"、"MYBUTTOND"、"MYBUTTONF"、"MYBUTTONX",注意,不要写成MYBUTTONU、MYBUTTOND、MYBUTTONF、MYBUTTONX;
(3)、在对话框类的结构中(.h)加入如下代码:
CBitmapButton m_wndBitmapButton;
(4)、在对话框的初始函数(OnInitDialog)中加入如下代码:
m_wndBitmapButton.AutoLoad(IDC_MYBUTTON,this);
(5)、编译并运行
2、其它情况
(1)、做4个位图,分别用于当按钮处在“UP”、“DOWN”、“FOCUS”、“DISABLE”时显示,对ID无要求;
(2)、声明CBitmapButton对象如:CBitmapButton m_wndBitmapButton;
(3)、调用Create()函数创建按钮
(4)、调用LoadBitmaps()函数装入位图。
函数原形如下:
BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );
BOOL LoadBitmaps( UINT nIDBitmapResource, UINT nIDBitmapResourceSel = 0, UINT nIDBitmapResourceFocus = 0, UINT nIDBitmapResourceDisabled = 0 );
#7
http://www.vckbase.com/document/viewdoc.asp?id=518
有CButtonST的源码及例子
有CButtonST的源码及例子