.h:
/**/
/********************************************************************
copyright: 2006-2007,Zhejiang Tianhuang Technology Industrial Co., Ltd.
created: 2007/03/08 8:3:2007 19:59
modified: 2007/03/08 8:3:2007 19:59
file base: MyBt
file ext: h
author: Fu Lingwei
description:header file
*********************************************************************/
#if !defined(AFX_LOGINBST_H__B270ED20_CA73_4180_99E3_CC95A0C21CCF__INCLUDED_)
#define AFX_LOGINBST_H__B270ED20_CA73_4180_99E3_CC95A0C21CCF__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyBt.h : header file
//
/**/ /////
// CMyBt window
class CMyBt : public CButton
... {
// Construction
public:
CMyBt();
virtual ~CMyBt();
// Attributes
public:
CToolTipCtrl m_tooltip;
HBITMAP hbmp;
CBitmap m_Bmp;
BITMAP bmp;
BOOL m_bDrawTransparent;
BOOL m_bSetBitmap;
// Operations
public:
void InitToolTip();
void SetToolTipText(LPCTSTR lpstr,BOOL bActivate = TRUE);
void DrawTransparent(BOOL bRepaint = false);
void SetBitmap(UINT nID,BOOL bset = FALSE);
void SetBitmap(LPCTSTR lpname,BOOL bset = FALSE);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyBt)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
// Implementation
// Generated message map functions
protected:
//{{AFX_MSG(CMyBt)
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
//afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
} ;
/**/ /////
// {{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_LOGINBST_H__B270ED20_CA73_4180_99E3_CC95A0C21CCF__INCLUDED_)
copyright: 2006-2007,Zhejiang Tianhuang Technology Industrial Co., Ltd.
created: 2007/03/08 8:3:2007 19:59
modified: 2007/03/08 8:3:2007 19:59
file base: MyBt
file ext: h
author: Fu Lingwei
description:header file
*********************************************************************/
#if !defined(AFX_LOGINBST_H__B270ED20_CA73_4180_99E3_CC95A0C21CCF__INCLUDED_)
#define AFX_LOGINBST_H__B270ED20_CA73_4180_99E3_CC95A0C21CCF__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyBt.h : header file
//
/**/ /////
// CMyBt window
class CMyBt : public CButton
... {
// Construction
public:
CMyBt();
virtual ~CMyBt();
// Attributes
public:
CToolTipCtrl m_tooltip;
HBITMAP hbmp;
CBitmap m_Bmp;
BITMAP bmp;
BOOL m_bDrawTransparent;
BOOL m_bSetBitmap;
// Operations
public:
void InitToolTip();
void SetToolTipText(LPCTSTR lpstr,BOOL bActivate = TRUE);
void DrawTransparent(BOOL bRepaint = false);
void SetBitmap(UINT nID,BOOL bset = FALSE);
void SetBitmap(LPCTSTR lpname,BOOL bset = FALSE);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyBt)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
// Implementation
// Generated message map functions
protected:
//{{AFX_MSG(CMyBt)
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
//afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
} ;
/**/ /////
// {{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_LOGINBST_H__B270ED20_CA73_4180_99E3_CC95A0C21CCF__INCLUDED_)
.cpp:
//
MyBt.cpp : implementation file
//
/**/ /********************************************************************
copyright: 2006-2007,Zhejiang Tianhuang Technology Industrial Co., Ltd.
created: 2007/03/08 8:3:2007 19:59
modified: 2007/03/08 8:3:2007 19:59
file base: MyBt
file ext: cpp
author: Fu Lingwei
description:implementation file
*********************************************************************/
#include " stdafx.h "
#include " MyBt.h "
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/**/ /////
// CMyBt
CMyBt::CMyBt()
... {
m_bDrawTransparent = FALSE;
m_tooltip.m_hWnd = NULL;
m_bSetBitmap = FALSE;
}
CMyBt:: ~ CMyBt()
... {
}
BEGIN_MESSAGE_MAP(CMyBt, CButton)
// {{AFX_MSG_MAP(CMyBt)
// ON_WM_CTLCOLOR_REFLECT()
ON_WM_ERASEBKGND()
// }}AFX_MSG_MAP
END_MESSAGE_MAP()
/**/ /////
// CMyBt message handlers
void CMyBt::PreSubclassWindow()
... {
// TODO: Add your specialized code here and/or call the base class
//ModifyStyle(0, BS_OWNERDRAW | BS_PUSHBUTTON);
ModifyStyle(0, BS_OWNERDRAW, SWP_FRAMECHANGED);
CButton::PreSubclassWindow();
}
void CMyBt::InitToolTip()
... {
if (m_tooltip.m_hWnd == NULL)
...{
// Create ToolTip control
m_tooltip.Create(this);
// Create inactive
m_tooltip.Activate(FALSE);
// Enable multiline
m_tooltip.SendMessage(TTM_SETMAXTIPWIDTH, 0, 400);
} // if
}
BOOL CMyBt::PreTranslateMessage(MSG * pMsg)
... {
// TODO: Add your specialized code here and/or call the base class
InitToolTip();
m_tooltip.RelayEvent(pMsg);
return CButton::PreTranslateMessage(pMsg);
}
void CMyBt::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
... {
// TODO: Add your code to draw the specified item
// Draw the button text using the text color red.
CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
pDC->SetBkMode(TRANSPARENT);
CString strText /**//*= "登录"*/;
GetWindowText( strText );
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(0,0,255));
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}
/**/ /*HBRUSH CMyBt::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: Change any attributes of the DC here
ASSERT(nCtlColor == CTLCOLOR_BTN);
DWORD dwStyle = GetStyle();
CFont m_font;
COLORREF m_color;
HBRUSH hbr = NULL;
if ((dwStyle & 0xFF) <= SS_RIGHT)
{
// this is a text control: set up font and colors
if (!(HFONT)m_font)
{
// first time init: create font
LOGFONT lf;
GetFont()->GetObject(sizeof(lf), &lf);
//lf.lfUnderline = TRUE;
m_font.CreateFontIndirect(&lf);
}
// use underline font and visited/unvisited colors
pDC->SelectObject(&m_font);
pDC->SetTextColor(m_color);
pDC->SetBkMode(TRANSPARENT);
// TODO: Return a non-NULL brush if the parent's handler should not be called
hbr = (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
return hbr;
}*/
BOOL CMyBt::OnEraseBkgnd(CDC * pDC)
... {
// TODO: Add your message handler code here and/or call default
if (m_bSetBitmap == TRUE)
...{
CDC MemDC;
int x = 0, y = 0;
CRect rect;
GetClientRect(rect);
MemDC.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = MemDC.SelectObject(&m_Bmp);
BOOL ret = pDC->StretchBlt(x,y,rect.Width(),rect.Height(),&MemDC,0,0,
bmp.bmWidth,bmp.bmHeight,SRCCOPY);
return ret;
}
if (m_bDrawTransparent == TRUE)
...{
return TRUE;
}
else
...{
return CButton::OnEraseBkgnd(pDC);
}
}
void CMyBt::DrawTransparent(BOOL bRepaint)
... {
m_bDrawTransparent = bRepaint;
}
void CMyBt::SetToolTipText(LPCTSTR lpstr,BOOL bActivate)
... {
// We cannot accept NULL pointer
if (lpstr == NULL) return;
// Initialize ToolTip
InitToolTip();
// If there is no tooltip defined then add it
if (m_tooltip.GetToolCount() == 0)
...{
CRect rectBtn;
GetClientRect(rectBtn);
m_tooltip.AddTool(this, lpstr, rectBtn, 1);
} // if
// Set text for tooltip
m_tooltip.UpdateTipText(lpstr, this, 1);
m_tooltip.Activate(bActivate);
}
void CMyBt::SetBitmap(UINT nID,BOOL bset)
... {
hbmp = ::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(nID));
m_Bmp.Attach(hbmp);
m_Bmp.GetBitmap(&bmp);
m_bSetBitmap = bset;
}
void CMyBt::SetBitmap(LPCTSTR lpname,BOOL bset)
... {
hbmp = (HBITMAP)LoadImage(NULL,lpname,IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
m_Bmp.Attach(hbmp);
m_Bmp.GetBitmap(&bmp);
m_bSetBitmap = bset;
}
//
/**/ /********************************************************************
copyright: 2006-2007,Zhejiang Tianhuang Technology Industrial Co., Ltd.
created: 2007/03/08 8:3:2007 19:59
modified: 2007/03/08 8:3:2007 19:59
file base: MyBt
file ext: cpp
author: Fu Lingwei
description:implementation file
*********************************************************************/
#include " stdafx.h "
#include " MyBt.h "
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/**/ /////
// CMyBt
CMyBt::CMyBt()
... {
m_bDrawTransparent = FALSE;
m_tooltip.m_hWnd = NULL;
m_bSetBitmap = FALSE;
}
CMyBt:: ~ CMyBt()
... {
}
BEGIN_MESSAGE_MAP(CMyBt, CButton)
// {{AFX_MSG_MAP(CMyBt)
// ON_WM_CTLCOLOR_REFLECT()
ON_WM_ERASEBKGND()
// }}AFX_MSG_MAP
END_MESSAGE_MAP()
/**/ /////
// CMyBt message handlers
void CMyBt::PreSubclassWindow()
... {
// TODO: Add your specialized code here and/or call the base class
//ModifyStyle(0, BS_OWNERDRAW | BS_PUSHBUTTON);
ModifyStyle(0, BS_OWNERDRAW, SWP_FRAMECHANGED);
CButton::PreSubclassWindow();
}
void CMyBt::InitToolTip()
... {
if (m_tooltip.m_hWnd == NULL)
...{
// Create ToolTip control
m_tooltip.Create(this);
// Create inactive
m_tooltip.Activate(FALSE);
// Enable multiline
m_tooltip.SendMessage(TTM_SETMAXTIPWIDTH, 0, 400);
} // if
}
BOOL CMyBt::PreTranslateMessage(MSG * pMsg)
... {
// TODO: Add your specialized code here and/or call the base class
InitToolTip();
m_tooltip.RelayEvent(pMsg);
return CButton::PreTranslateMessage(pMsg);
}
void CMyBt::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
... {
// TODO: Add your code to draw the specified item
// Draw the button text using the text color red.
CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
pDC->SetBkMode(TRANSPARENT);
CString strText /**//*= "登录"*/;
GetWindowText( strText );
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(0,0,255));
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}
/**/ /*HBRUSH CMyBt::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: Change any attributes of the DC here
ASSERT(nCtlColor == CTLCOLOR_BTN);
DWORD dwStyle = GetStyle();
CFont m_font;
COLORREF m_color;
HBRUSH hbr = NULL;
if ((dwStyle & 0xFF) <= SS_RIGHT)
{
// this is a text control: set up font and colors
if (!(HFONT)m_font)
{
// first time init: create font
LOGFONT lf;
GetFont()->GetObject(sizeof(lf), &lf);
//lf.lfUnderline = TRUE;
m_font.CreateFontIndirect(&lf);
}
// use underline font and visited/unvisited colors
pDC->SelectObject(&m_font);
pDC->SetTextColor(m_color);
pDC->SetBkMode(TRANSPARENT);
// TODO: Return a non-NULL brush if the parent's handler should not be called
hbr = (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
return hbr;
}*/
BOOL CMyBt::OnEraseBkgnd(CDC * pDC)
... {
// TODO: Add your message handler code here and/or call default
if (m_bSetBitmap == TRUE)
...{
CDC MemDC;
int x = 0, y = 0;
CRect rect;
GetClientRect(rect);
MemDC.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = MemDC.SelectObject(&m_Bmp);
BOOL ret = pDC->StretchBlt(x,y,rect.Width(),rect.Height(),&MemDC,0,0,
bmp.bmWidth,bmp.bmHeight,SRCCOPY);
return ret;
}
if (m_bDrawTransparent == TRUE)
...{
return TRUE;
}
else
...{
return CButton::OnEraseBkgnd(pDC);
}
}
void CMyBt::DrawTransparent(BOOL bRepaint)
... {
m_bDrawTransparent = bRepaint;
}
void CMyBt::SetToolTipText(LPCTSTR lpstr,BOOL bActivate)
... {
// We cannot accept NULL pointer
if (lpstr == NULL) return;
// Initialize ToolTip
InitToolTip();
// If there is no tooltip defined then add it
if (m_tooltip.GetToolCount() == 0)
...{
CRect rectBtn;
GetClientRect(rectBtn);
m_tooltip.AddTool(this, lpstr, rectBtn, 1);
} // if
// Set text for tooltip
m_tooltip.UpdateTipText(lpstr, this, 1);
m_tooltip.Activate(bActivate);
}
void CMyBt::SetBitmap(UINT nID,BOOL bset)
... {
hbmp = ::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(nID));
m_Bmp.Attach(hbmp);
m_Bmp.GetBitmap(&bmp);
m_bSetBitmap = bset;
}
void CMyBt::SetBitmap(LPCTSTR lpname,BOOL bset)
... {
hbmp = (HBITMAP)LoadImage(NULL,lpname,IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
m_Bmp.Attach(hbmp);
m_Bmp.GetBitmap(&bmp);
m_bSetBitmap = bset;
}
使用范例:
CMyBt m_login;
m_login.SubclassDlgItem(IDOK,this);
1,透明
m_login.DrawTransparent(TRUE);
2,提示
m_login.SetToolTipText("sdgdhgf",TRUE);
3,位图
m_login.SetBitmap(IDB_XXX,TRUE);
m_login.SetBitmap("res//bk1.bmp",TRUE);
CMyBt 只实现按钮透明、加载位图和提示,要使用其他功能,在 DrawItem ,CtlColor 中扩展即可。