CWnd * pWnd = GetDlgItem(IDC_BUTTON1); CDC * pDC=pWnd->GetDC(); //Button now is respected to be a window with pDC is its view pointer ! //Draw with the pDC as you do in CView Class!ReleaseDC(pDC);
先定义一个CIEButton类 class CIEButton : public CButton { // Construction public: CIEButton();// Attributes public:// Operations public:// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CIEButton) public: virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); //}}AFX_VIRTUAL// Implementation public: //设置状态,查看鼠标是否位于按钮之上 BOOL IsMouseOn;
可以看看这个帖子
http://expert.csdn.net/Expert/topic/2397/2397383.xml?temp=.2466242
CDC * pDC=pWnd->GetDC();
//Button now is respected to be a window with pDC is its view pointer !
//Draw with the pDC as you do in CView Class!ReleaseDC(pDC);
class CIEButton : public CButton
{
// Construction
public:
CIEButton();// Attributes
public:// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CIEButton)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
//}}AFX_VIRTUAL// Implementation
public:
//设置状态,查看鼠标是否位于按钮之上
BOOL IsMouseOn;
virtual ~CIEButton(); // Generated message map functions
protected:
//{{AFX_MSG(CIEButton)
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnClicked();
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};按钮上绘图的主要函数:
void CIEButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CDC ButtonDC;
CBitmap bitmapTrans;
BITMAP bmp;
CDC mem;
CRect rc,rc2;
//得到用于绘制按钮的DC
ButtonDC.Attach( lpDrawItemStruct->hDC );
//准备用于向按钮区域传输位图
mem.CreateCompatibleDC(&ButtonDC);
//获取按钮所占的矩形大小
rc=lpDrawItemStruct->rcItem;
rc2=rc;
//在任何状态下,图片是首先要绘绘制的
bitmapTrans.LoadBitmap(IDB_IE);//IDB_IE为你的资源位图标识符
bitmapTrans.GetBitmap(&bmp);
CBitmap *old=mem.SelectObject(&bitmapTrans);
int x,y;
x=rc.Width()/2-bmp.bmWidth/2;
y=rc.Height()/2-bmp.bmHeight/2;
//向按钮中心点传输位图
ButtonDC.BitBlt(x,y,rc.right,rc.bottom,&mem, 0, 0, SRCCOPY);
mem.SelectObject(old);
//获取按钮目前所处的状态,根据不同的状态绘制不同的按钮
//如果鼠标移入按钮,绘制浮起状态
if (IsMouseOn )
{
//为按钮绘制立体效果
rc.top=rc.top+1;rc.bottom=rc.bottom-1;
rc.left=rc.left+1;rc.right=rc.right-1;
//绘制立体边框
ButtonDC.Draw3dRect(&rc,RGB(255,255,255),RGB(0,0,0));
}
//如果按钮被按下,绘制下沉立体效果
if(lpDrawItemStruct->itemAction &ODA_SELECT)
{
rc2.top=rc2.top+1;rc2.bottom=rc2.bottom-1;
rc2.left=rc2.left+1;rc2.right=rc2.right-1;
//绘制立体边框
ButtonDC.Draw3dRect(&rc2,RGB(0,0,0),RGB(255,255,255));
}
}void CIEButton::OnMouseMove(UINT nFlags, CPoint point)
{
//如果鼠标移入按钮
if(!IsMouseOn)
{
this->IsMouseOn=TRUE;
//重绘整个控件
this->Invalidate();
}
CButton::OnMouseMove(nFlags, point);
}void CIEButton::OnClicked()
{
AfxMessageBox("测试信息");
}
其他的就不用再讲了吧!^_^^_^