📄 atlflatbutton.h
字号:
//
// Class: CButtonST
//
// Compiler: Visual C++
// Tested on: Visual C++ 5.0
//
// Version: See GetVersionC() or GetVersionI()
//
// Created: xx/xxxx/1998
// Updated: 19/September/1999
//
// Author: Davide Calabro' davide_calabro@yahoo.com
//
// ===================================================================================
//
// Ported to WTL by Rashid Thadha (05/04/2001)
// added functionality to give a outlook style button
//
// ===================================================================================
#if !defined(ATL_FLAT_BUTTON_H__)
#define ATL_FLAT_BUTTON_H__
class CButtonST : public CWindowImpl < CButtonST, CButton>
{
public:
DECLARE_WND_SUPERCLASS(_T("CATLFlatButton"), CButton::GetWndClassName())
CButtonST() : m_MouseOnButton(FALSE), m_hIconIn(NULL), m_hIconOut(NULL), m_cxIcon(0),
m_cyIcon(0), m_hCursor(NULL)
{
// Default type is "flat" button
m_bIsFlat = TRUE;
// By default draw border in "flat" button
m_bDrawBorder = TRUE;
// By default icon is aligned horizontally
m_nAlign = ST_ALIGN_HORIZ;
// By default show the text button
m_bShowText = TRUE;
// By default, for "flat" button, don't draw the focus rect
m_bDrawFlatFocus = FALSE;
// By default the button is not the default button
m_bIsDefault = FALSE;
SetDefaultInactiveBgColor();
SetDefaultInactiveFgColor();
SetDefaultActiveBgColor();
SetDefaultActiveFgColor();
SetDefaultTextColor();
SetDefaultHighLightColor();
SetDefaultShadowColor();
// No tooltip created
m_ToolTip.m_hWnd = NULL;
// Do not draw as a transparent button
m_bDrawTransparent = FALSE;
m_pbmpOldBk = NULL;
// Do not draw the Arrow
m_bAddArrow = FALSE;
}
~CButtonST()
{
// Restore old bitmap (if any)
if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL)
{
m_dcBk.SelectBitmap(m_pbmpOldBk);
}
// Destroy the icons (if any)
// Note: the following two lines MUST be here! even if
// BoundChecker says they are unnecessary!
if (m_hIconIn != NULL)
::DestroyIcon(m_hIconIn);
if (m_hIconOut != NULL)
::DestroyIcon(m_hIconOut);
// Destroy the cursor (if any)
if (m_hCursor != NULL)
::DestroyCursor(m_hCursor);
}
enum {ST_ALIGN_HORIZ, ST_ALIGN_VERT, ST_ALIGN_HORIZ_RIGHT};
BEGIN_MSG_MAP(CButtonST)
MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
MESSAGE_HANDLER(WM_KILLFOCUS, OnKillFocus)
MESSAGE_HANDLER(WM_CAPTURECHANGED, OnCaptureChanged)
MESSAGE_HANDLER(OCM_DRAWITEM, OnDrawItem)
MESSAGE_HANDLER(WM_SETCURSOR, OnSetCursor)
MESSAGE_HANDLER(WM_SYSCOLORCHANGE, OnSysColorChange)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
REFLECT_NOTIFICATIONS()
END_MSG_MAP()
BOOL PreTranslateMessage(MSG* pMsg)
{
if (::IsWindow(m_ToolTip.m_hWnd))
{
InitToolTip();
m_ToolTip.RelayEvent(pMsg);
}
return (BOOL)SendMessage(WM_FORWARDMSG, 0, (LPARAM)pMsg);
}
void SetIcon(int nIconInId, int nIconOutId = NULL)
{
HICON hIconIn;
HICON hIconOut;
// Set icon when the mouse is IN the button
hIconIn = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(nIconInId), IMAGE_ICON, 0, 0, 0);
// Set icon when the mouse is OUT the button
hIconOut = (nIconOutId == NULL) ? NULL :(HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(nIconOutId), IMAGE_ICON, 0, 0, 0);
SetIcon(hIconIn, hIconOut);
} // End of SetIcon
void SetIcon(HICON hIconIn, HICON hIconOut = NULL)
{
// Note: the following two lines MUST be here! even if
// BoundChecker says they are unnecessary!
if (m_hIconIn != NULL)
::DestroyIcon(m_hIconIn);
if (m_hIconOut != NULL)
::DestroyIcon(m_hIconOut);
// Set icon when the mouse is IN the button
m_hIconIn = hIconIn;
// Set icon when the mouse is OUT the button
m_hIconOut = (hIconOut == NULL) ? m_hIconIn : hIconOut;
ICONINFO ii;
// Get icon dimension
ZeroMemory(&ii, sizeof(ICONINFO));
::GetIconInfo(m_hIconIn, &ii);
m_cxIcon = (BYTE)(ii.xHotspot * 2);
m_cyIcon = (BYTE)(ii.yHotspot * 2);
::DeleteObject(ii.hbmMask);
::DeleteObject(ii.hbmColor);
RedrawWindow();
} // End of SetIcon
BOOL SetBtnCursor(int nCursorId)
{
// Destroy any previous cursor
if (m_hCursor != NULL)
::DestroyCursor(m_hCursor);
m_hCursor = NULL;
// If we want a cursor
if (nCursorId != -1)
{
// Load icon resource
m_hCursor = (HCURSOR)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(nCursorId), IMAGE_CURSOR, 0, 0, 0);
// If something wrong then return FALSE
if (m_hCursor == NULL)
return FALSE;
}
return TRUE;
} // End of SetBtnCursor
void SetFlat(BOOL bState)
{
m_bIsFlat = bState;
Invalidate();
} // End of SetFlat
BOOL GetFlat()
{
return m_bIsFlat;
} // End of GetFlat
void SetAlign(int nAlign)
{
switch (nAlign)
{
case ST_ALIGN_HORIZ:
m_nAlign = ST_ALIGN_HORIZ;
break;
case ST_ALIGN_HORIZ_RIGHT:
m_nAlign = ST_ALIGN_HORIZ_RIGHT;
break;
case ST_ALIGN_VERT:
m_nAlign = ST_ALIGN_VERT;
break;
}
Invalidate();
} // End of SetAlign
int GetAlign()
{
return m_nAlign;
} // End of GetAlign
void DrawArrow(BOOL bDraw)
{
m_bAddArrow = bDraw;
}
void DrawBorder(BOOL bEnable)
{
m_bDrawBorder = bEnable;
} // End of DrawBorder
static const char* GetVersionC()
{
return "2.6";
} // End of GetVersionC
static const short GetVersionI()
{
return 26; // Divide by 10 to get actual version
} // End of GetVersionI
void SetShowText(BOOL bShow)
{
m_bShowText = bShow;
Invalidate();
} // End of SetShowText
BOOL GetShowText()
{
return m_bShowText;
} // End of GetShowText
void Init()
{
ATLASSERT(::IsWindow(m_hWnd));
UINT nBS;
nBS = GetButtonStyle();
// Check if this is the default button
if (nBS & BS_DEFPUSHBUTTON)
m_bIsDefault = TRUE;
// Add BS_OWNERDRAW style
SetButtonStyle(nBS | BS_OWNERDRAW);
}
HWND Create(HWND hWndParent, RECT& rcPos, LPCTSTR szWindowName = NULL,
DWORD dwStyle = 0, DWORD dwExStyle = 0,
UINT nID = 0, LPVOID lpCreateParam = NULL)
{
HWND hWnd = CWindowImpl < CButtonST, CButton>::Create(hWndParent,
rcPos, szWindowName, dwStyle, dwExStyle, nID, lpCreateParam);
if (hWnd)
Init();
return hWnd;
}
BOOL SubclassWindow(HWND hWnd)
{
BOOL bRet = CWindowImpl < CButtonST, CButton>::SubclassWindow(hWnd);
if (bRet)
Init();
return bRet;
}
void SetDefaultInactiveBgColor(BOOL bRepaint = FALSE)
{
m_crInactiveBg = ::GetSysColor(COLOR_BTNFACE);
if (bRepaint)
Invalidate();
} // End of SetDefaultInactiveBgColor
void SetInactiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE)
{
m_crInactiveBg = crNew;
if (bRepaint)
Invalidate();
} // End of SetInactiveBgColor
const COLORREF GetInactiveBgColor()
{
return m_crInactiveBg;
} // End of GetInactiveBgColor
void SetDefaultInactiveFgColor(BOOL bRepaint = FALSE)
{
m_crInactiveFg = ::GetSysColor(COLOR_BTNTEXT);
if (bRepaint)
Invalidate();
} // End of SetDefaultInactiveFgColor
void SetInactiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE)
{
m_crInactiveFg = crNew;
if (bRepaint)
Invalidate();
} // End of SetInactiveFgColor
const COLORREF GetInactiveFgColor()
{
return m_crInactiveFg;
} // End of GetInactiveFgColor
void SetDefaultActiveBgColor(BOOL bRepaint = FALSE)
{
m_crActiveBg = ::GetSysColor(COLOR_BTNFACE);
if (bRepaint)
Invalidate();
} // End of SetDefaultActiveBgColor
void SetActiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE)
{
m_crActiveBg = crNew;
if (bRepaint)
Invalidate();
} // End of SetActiveBgColor
const COLORREF GetActiveBgColor()
{
return m_crActiveBg;
} // End of GetActiveBgColor
void SetDefaultActiveFgColor(BOOL bRepaint = FALSE)
{
m_crActiveFg = ::GetSysColor(COLOR_BTNTEXT);
if (bRepaint)
Invalidate();
} // End of SetDefaultActiveFgColor
void SetDefaultTextColor(BOOL bRepaint = FALSE)
{
m_crTextColor = ::GetSysColor(COLOR_BTNTEXT);
if (bRepaint)
Invalidate();
} // End of SetDefaultTextColor
void SetDefaultHighLightColor(BOOL bRepaint = FALSE)
{
m_crHighLight = ::GetSysColor(COLOR_BTNHILIGHT);
if (bRepaint)
Invalidate();
} // End of SetDefaultHighLightColor
void SetDefaultShadowColor(BOOL bRepaint = FALSE)
{
m_crShadow = ::GetSysColor(COLOR_BTNSHADOW);
if (bRepaint)
Invalidate();
} // End of SetDefaultShadowColor
void SetActiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE)
{
m_crActiveFg = crNew;
if (bRepaint)
Invalidate();
} // End of SetActiveFgColor
const COLORREF GetActiveFgColor()
{
return m_crActiveFg;
} // End of GetActiveFgColor
void SetTextColor(COLORREF crNew, BOOL bRepaint = FALSE)
{
m_crTextColor = crNew;
if (bRepaint)
Invalidate();
} // End of SetTextColor
const COLORREF GetTextColor()
{
return m_crTextColor;
} // End of GetTextColor
void SetHighLightColor(COLORREF crNew, BOOL bRepaint = FALSE)
{
m_crHighLight = crNew;
if (bRepaint)
Invalidate();
} // End of SetHighLightColor
const COLORREF GetHighLightColor()
{
return m_crHighLight;
} // End of GetHighLightColor
void SetShadowColor(COLORREF crNew, BOOL bRepaint = FALSE)
{
m_crShadow = crNew;
if (bRepaint)
Invalidate();
} // End of SetHighLightColor
const COLORREF GetShadowColor()
{
return m_crShadow;
} // End of GetHighLightColor
void SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint = FALSE)
{
m_bDrawFlatFocus = bDrawFlatFocus;
// Repaint the button
if (bRepaint)
Invalidate();
} // End of SetFlatFocus
BOOL GetFlatFocus()
{
return m_bDrawFlatFocus;
} // End of GetFlatFocus
void SetTooltipText(CString* spText, BOOL bActivate = TRUE)
{
// We cannot accept NULL pointer
if (spText == 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(m_hWnd, (LPCTSTR)*spText, rectBtn, 1);
}
// Set text for tooltip
m_ToolTip.UpdateTipText((LPCTSTR)*spText, m_hWnd, 1);
m_ToolTip.Activate(bActivate);
} // End of SetTooltipText
void SetTooltipText(int nId, BOOL bActivate = TRUE)
{
CString sText;
// load string resource
sText.LoadString(nId);
// If string resource is not empty
if (sText.IsEmpty() == FALSE)
SetTooltipText(&sText, bActivate);
} // End of SetTooltipText
void ActivateTooltip(BOOL bActivate)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -