//窗口透明和图片按钮使用方法
/*ModifyStyleEx(0,0x80000);
HMODULE hModule = LoadLibrary("User32.DLL");
if(NULL != hModule)
{
typedef BOOL (WINAPI *FUNC)(HWND,COLORREF,BYTE,DWORD);
FUNC func = (FUNC)GetProcAddress(hModule,"SetLayeredWindowAttributes");
if(func != NULL)
{
func(m_hWnd,0x00,240,2);
}
FreeLibrary(hModule);
}
//图片透明按钮使用方法
//m_BtnBng.SetButtonStyle(m_BtnBng.GetButtonStyle()|BS_OWNERDRAW);
RECT rt;
m_BtnBng.GetClientRect(&rt);
Bitmap Bmp((HINSTANCE)0x400000,MAKEINTRESOURCEW(IDB_BITMAP_ME));
Bitmap BmpRr((HINSTANCE)0x400000,MAKEINTRESOURCEW(IDB_BITMAP_RR));
m_BtnBng.Init(CreateRoundRectRgn(0,0,rt.right,rt.bottom,80,30),&Bmp);//theApp.m_hInstance == 0x400000);
m_BtnBng.SetToolTip("Click BNG to config/view AUTO-VPN");
m_BtnPe.GetClientRect(&rt);
m_BtnPe.Init(CreateRoundRectRgn(0,0,rt.right,rt.bottom,80,30),&BmpRr);
m_BtnPe.SetToolTip("Click PE to config/view VPN's information");
m_BtnRr.GetClientRect(&rt);
m_BtnRr.Init(CreateRoundRectRgn(0,0,rt.right,rt.bottom,60,30),&BmpRr);
m_BtnRr.SetToolTip("Click RR to view related configuration");
RECT rt;
m_Btn1.GetClientRect(&rt);
Bitmap bmp1(L"button01.png");
Graphics gh(&bmp);
gh.DrawImage(&bmp1, 0, 0, rt.right, rt.bottom);
HRGN rgn = CreateRectRgn(rt.left, rt.top, rt.right, rt.bottom);
Color clr;
for(int j=0; j < rt.bottom; j++)
for(int i=0; i< rt.right;i++)
bmp.GetPixel(i,j,&clr);
if(0x00 == clr.GetA())
{
HRGN rgnx = CreateRectRgn(i,j,i+1,j+1);
ComBineRgn(rgn,rgn,rgnx, RGN_DIFF);
DeleteObject(rgnx);
}
}
}
m_Btn1.Init(rgn,&bmp);
*/
//CSButton.h
class CSButton: public CButton
{
// Construction
public:
CSButton();
// 初始化值
void Init(HRGN hrgn,Image* pImageButton);
// 设置按钮外框的颜色,参数为空时无按钮外框,如SetFrameCOLORREF();
void SetFrameCOLORREF(COLORREF ColorrefMouseDown = 0,COLORREF ColorrefMouseUp = 0,COLORREF ColorrefMouseMove = 0);
// Attributes
protected:
// 必须指定的值
HRGN m_hrgn; // 按钮的区域
int m_iResourceID; // 按钮的资源ID
// Operations
public:
CToolTipCtrl m_ToolTipCtrl;
int m_iMove;
Image *m_pImageButton;
bool m_bSetBtnStyle;
COLORREF m_ColorrefMouseDown;
COLORREF m_ColorrefMouseUp;
COLORREF m_ColorrefMouseMove;
// 按键的字体大小 ,默认是系统的大小,可以设置
float m_fFontSize;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CSButton)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
//}}AFX_VIRTUAL
// Implementation
public:
/// 设置提示信息
void SetToolTip(CString str);
virtual ~CSButton();
// Generated message map functions
protected:
//{{AFX_MSG(CSButton)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//CSButton.c
CSButton::CSButton()
{
m_iMove = 0;
m_bSetBtnStyle = true;
m_hrgn = NULL;
m_iResourceID = 0;
m_pImageButton = NULL;
m_fFontSize = 0;
m_ColorrefMouseDown = 0;
m_ColorrefMouseUp = 0;
m_ColorrefMouseMove = 0;
m_ToolTipCtrl.Create(this);
}
CSButton::~CSButton()
{
if(NULL != m_hrgn)
{
DeleteObject(m_hrgn);
m_hrgn = NULL;
}
if(NULL != m_pImageButton)
{
delete m_pImageButton;
m_pImageButton = NULL;
}
}
BEGIN_MESSAGE_MAP(CSButton, CButton)
//{{AFX_MSG_MAP(CSButton)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CSButton message handlers
void CSButton::Init(HRGN hrgn,Image* pImageButton)
{
if((NULL != hrgn) && (NULL != pImageButton))
{
m_pImageButton = pImageButton->Clone();
m_hrgn = CreateRectRgn(0,0,0,0);
HRGN hrgnDest = CreateRectRgn(0,0,0,0);
CombineRgn(m_hrgn,hrgn,hrgnDest,RGN_OR);
SetWindowRgn(hrgn,true);
DeleteObject(hrgnDest);
}
if(NULL != hrgn)
{
DeleteObject(hrgn);
hrgn = NULL;
}
}
void CSButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_iMove = 2;
//Invalidate();
CButton::OnLButtonDown(nFlags, point);
}
void CSButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_iMove = 0;
//Invalidate();
CButton::OnLButtonUp(nFlags, point);
}
void CSButton::OnMouseMove(UINT nFlags, CPoint point)
{
if(m_iMove == 0)// && (nFlags != MK_LBUTTON))
{
if(::GetCapture() != m_hWnd)
{
::SetCapture(m_hWnd);
}
m_iMove = 1;
Invalidate();
//GetParent()->Invalidate();
}
if(!PtInRegion(m_hrgn,point.x,point.y))
{
ReleaseCapture();
m_iMove = 0;
//::SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,0);//刷新所有桌面窗口
Invalidate();
//GetParent()->Invalidate();
}
CButton::OnMouseMove(nFlags, point);
}
void CSButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
//return;
UINT uStyle = DFCS_BUTTONPUSH;
// This code only works with buttons.
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
// If drawing selected, add the pushed style to DrawFrameControl.
if (lpDrawItemStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED;
if(NULL != m_pImageButton)
{
SetBkMode(lpDrawItemStruct->hDC,0);
Graphics graphics(lpDrawItemStruct->hDC);
graphics.SetClip(m_hrgn,CombineModeReplace);
Rect rt;
GetClientRect((RECT*)&rt);
COLORREF Colorref = 0;
switch(m_iMove)
{
case 0:
{
Colorref = m_ColorrefMouseUp;
graphics.DrawImage(m_pImageButton,rt);
break;
}
case 1:
{
Colorref = m_ColorrefMouseMove;
graphics.DrawImage(m_pImageButton,
rt,// dest rect
0,0,m_pImageButton->GetWidth(), m_pImageButton->GetHeight(),// source rect
UnitPixel,NULL,NULL,NULL);
SolidBrush blackBrush(Color(80, 255, 255, 0));
graphics.FillRectangle(&blackBrush, rt);
break;
}
case 2:
{
Colorref = m_ColorrefMouseDown;
ImageAttributes remapAttributes;
graphics.DrawImage(m_pImageButton,
rt,// dest rect
0,0,m_pImageButton->GetWidth()-2, m_pImageButton->GetHeight()-2,// source rect
UnitPixel,&remapAttributes,NULL,NULL);
break;
}
default:break;
}
if(0 != Colorref)
{
CBrush brush(Colorref);
FrameRgn(lpDrawItemStruct->hDC,m_hrgn,(HBRUSH)brush,2,1);
}
CString str;
GetWindowText(str);
if(!str.IsEmpty())
{
WCHAR wstr[100] = {0};
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,str,-1,wstr,100);
Font myFont(lpDrawItemStruct->hDC);
float size = myFont.GetSize();
FontFamily fontFamily;
myFont.GetFamily(&fontFamily);
if(m_fFontSize != 0)size = m_fFontSize;
Font myFontUse(&fontFamily,size,0,UnitPixel);
RectF layoutRect(0.0f + (m_iMove == 2 ? 1 : 0), lpDrawItemStruct->rcItem.bottom/2 - size/2 + (m_iMove == 2 ? 1 : 0),lpDrawItemStruct->rcItem.right, lpDrawItemStruct->rcItem.bottom);
StringFormat format;
format.SetAlignment(StringAlignmentCenter);
SolidBrush blackBrush(Color(255, 0, 0, 0));
graphics.DrawString(wstr,wcslen(wstr),&myFontUse,layoutRect,&format,&blackBrush);
}
}
return ;
}
LRESULT CSButton::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if(m_bSetBtnStyle && (NULL != m_pImageButton))
{
m_bSetBtnStyle = false;
SetButtonStyle(GetButtonStyle()|BS_OWNERDRAW);
}
return CButton::WindowProc(message, wParam, lParam);
}
BOOL CSButton::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return false;
//return CButton::OnEraseBkgnd(pDC);
}
void CSButton::SetFrameCOLORREF(COLORREF ColorrefMouseDown,COLORREF ColorrefMouseUp,COLORREF ColorrefMouseMove)
{
m_ColorrefMouseDown = ColorrefMouseDown;
m_ColorrefMouseUp = ColorrefMouseUp;
m_ColorrefMouseMove = ColorrefMouseMove;
}
BOOL CSButton::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
m_ToolTipCtrl.RelayEvent(pMsg);
return CButton::PreTranslateMessage(pMsg);
}
void CSButton::SetToolTip(CString str)
{
m_ToolTipCtrl.AddTool(this,str);
m_ToolTipCtrl.Activate(TRUE);
}