📄 btnst.cpp
字号:
#include "stdafx.h"
#include "BtnST.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CButtonST
CButtonST::CButtonST()
{
m_bMouseOnButton = FALSE;
FreeResources(FALSE);
// 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;
// By default the button is not a checkbox
m_bIsCheckBox = FALSE;
m_nCheck = 0;
// Set default colors
SetDefaultColors(FALSE);
// No tooltip created
m_ToolTip.m_hWnd = NULL;
// Do not draw as a transparent button
m_bDrawTransparent = FALSE;
m_pbmpOldBk = NULL;
// No URL defined
::ZeroMemory(&m_szURL, sizeof(m_szURL));
// No cursor defined
m_hCursor = NULL; // Fix by kawasaki@us.dnttm.ro
// No autorepeat
m_bAutoRepeat = FALSE;
m_hWndAutoRepeat = NULL;
m_nMsgAutoRepeat = WM_APP;
m_dwPeriodAutoRepeat = 100;
} // End of CButtonST
CButtonST::~CButtonST()
{
// Restore old bitmap (if any)
if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL)
{
m_dcBk.SelectObject(m_pbmpOldBk);
}
FreeResources();
// Destroy the cursor (if any)
if (m_hCursor != NULL) ::DestroyCursor(m_hCursor);
} // End of ~CButtonST
BEGIN_MESSAGE_MAP(CButtonST, CButton)
//{{AFX_MSG_MAP(CButtonST)
ON_WM_CAPTURECHANGED()
ON_WM_SETCURSOR()
ON_WM_KILLFOCUS()
ON_WM_MOUSEMOVE()
ON_WM_SYSCOLORCHANGE()
ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
ON_WM_ACTIVATE()
ON_WM_ENABLE()
ON_WM_CANCELMODE()
ON_WM_CTLCOLOR_REFLECT()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
DWORD CButtonST::SetIcon(int nIconInId, int nIconOutId)
{
HICON hIconIn;
HICON hIconOut;
HINSTANCE hInstResource;
// Find correct resource handle
hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconInId), RT_GROUP_ICON);
// Set icon when the mouse is IN the button
hIconIn = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconInId), IMAGE_ICON, 0, 0, 0);
// Set icon when the mouse is OUT the button
hIconOut = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconOutId), IMAGE_ICON, 0, 0, 0);
return SetIcon(hIconIn, hIconOut);
} // End of SetIcon
DWORD CButtonST::SetIcon(HICON hIconIn, HICON hIconOut)
{
BOOL bRetValue;
ICONINFO ii;
// Free any loaded resource
FreeResources();
if (hIconIn != NULL)
{
m_csIcons[0].hIcon = hIconIn;
// Get icon dimension
ZeroMemory(&ii, sizeof(ICONINFO));
bRetValue = ::GetIconInfo(hIconIn, &ii);
if (bRetValue == FALSE)
{
FreeResources();
return BTNST_INVALIDRESOURCE;
} // if
m_csIcons[0].dwWidth = (DWORD)(ii.xHotspot * 2);
m_csIcons[0].dwHeight = (DWORD)(ii.yHotspot * 2);
::DeleteObject(ii.hbmMask);
::DeleteObject(ii.hbmColor);
if (hIconOut != NULL)
{
m_csIcons[1].hIcon = hIconOut;
// Get icon dimension
ZeroMemory(&ii, sizeof(ICONINFO));
bRetValue = ::GetIconInfo(hIconOut, &ii);
if (bRetValue == FALSE)
{
FreeResources();
return BTNST_INVALIDRESOURCE;
} // if
m_csIcons[1].dwWidth = (DWORD)(ii.xHotspot * 2);
m_csIcons[1].dwHeight = (DWORD)(ii.yHotspot * 2);
::DeleteObject(ii.hbmMask);
::DeleteObject(ii.hbmColor);
} // if
} // if
RedrawWindow();
return BTNST_OK;
} // End of SetIcon
BOOL CButtonST::SetBtnCursor(int nCursorId)
{
HINSTANCE hInstResource;
// Destroy any previous cursor
if (m_hCursor != NULL) ::DestroyCursor(m_hCursor);
m_hCursor = NULL;
// If we want a cursor
if (nCursorId != NULL)
{
hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nCursorId),
RT_GROUP_CURSOR);
// Load icon resource
m_hCursor = (HCURSOR)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, 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 CButtonST::SetAlign(int nAlign)
{
switch (nAlign)
{
case ST_ALIGN_HORIZ:
case ST_ALIGN_HORIZ_RIGHT:
case ST_ALIGN_VERT:
m_nAlign = nAlign;
break;
}
Invalidate();
} // End of SetAlign
int CButtonST::GetAlign()
{
return m_nAlign;
} // End of GetAlign
void CButtonST::SetShowText(BOOL bShow)
{
m_bShowText = bShow;
Invalidate();
} // End of SetShowText
BOOL CButtonST::GetShowText()
{
return m_bShowText;
} // End of GetShowText
void CButtonST::OnMouseMove(UINT nFlags, CPoint point)
{
CWnd* pWnd; // Finestra attiva
CWnd* pParent; // Finestra che contiene il bottone
CButton::OnMouseMove(nFlags, point);
// If the mouse enter the button with the left button pressed
// then do nothing
if (nFlags & MK_LBUTTON && m_bMouseOnButton == FALSE) return;
// If our button is not flat then do nothing
pWnd = GetActiveWindow();
pParent = GetOwner();
if ((GetCapture() != this) &&
(
#ifndef ST_LIKEIE
pWnd != NULL &&
#endif
pParent != NULL))
{
m_bMouseOnButton = TRUE;
//SetFocus(); // Thanks Ralph!
SetCapture();
Invalidate();
}
else
{
/*
CRect rc;
GetClientRect(&rc);
if (!rc.PtInRect(point))
{
*/
POINT p2 = point;
ClientToScreen(&p2);
CWnd* wndUnderMouse = WindowFromPoint(p2);
// if (wndUnderMouse != this)
if (wndUnderMouse && wndUnderMouse->m_hWnd != this->m_hWnd)
{
// Redraw only if mouse goes out
if (m_bMouseOnButton == TRUE)
{
m_bMouseOnButton = FALSE;
Invalidate();
}
// If user is NOT pressing left button then release capture!
if (!(nFlags & MK_LBUTTON)) ReleaseCapture();
}
}
} // End of OnMouseMove
void CButtonST::OnKillFocus(CWnd * pNewWnd)
{
CButton::OnKillFocus(pNewWnd);
CancelHover();
} // End of OnKillFocus
void CButtonST::OnLButtonDown(UINT nFlags, CPoint point)
{
CButton::OnLButtonDown(nFlags, point);
if (m_bAutoRepeat == TRUE)
{
MSG csMsg;
int nButtonID;
HWND hWndParent;
nButtonID = GetDlgCtrlID();
hWndParent = GetParent()->GetSafeHwnd();
while(PeekMessage(&csMsg, m_hWnd, WM_LBUTTONUP, WM_LBUTTONUP, PM_REMOVE) == FALSE)
{
::SendMessage(hWndParent, WM_COMMAND, MAKEWPARAM((WORD)nButtonID, BN_CLICKED), (LPARAM)m_hWnd);
::Sleep(m_dwPeriodAutoRepeat);
}
SendMessage(WM_LBUTTONUP);
} // if
} // End of OnLButtonDown
void CButtonST::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CButton::OnActivate(nState, pWndOther, bMinimized);
if (nState == WA_INACTIVE) CancelHover();
} // End of OnActivate
void CButtonST::OnEnable(BOOL bEnable)
{
CButton::OnEnable(bEnable);
if (bEnable == FALSE) CancelHover();
} // End of OnEnable
void CButtonST::OnCancelMode()
{
CButton::OnCancelMode();
CancelHover();
} // End of OnCancelMode
void CButtonST::OnCaptureChanged(CWnd *pWnd)
{
if (m_bMouseOnButton == TRUE)
{
ReleaseCapture();
Invalidate();
}
// Call base message handler
CButton::OnCaptureChanged(pWnd);
} // End of OnCaptureChanged
void CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
BOOL bIsPressed;
// Checkbox or Radiobutton style ?
if (m_bIsCheckBox == TRUE)
{
bIsPressed = (lpDIS->itemState & ODS_SELECTED)
|| (m_nCheck != 0);
//bIsPressed = TRUE;
}
// Normal button OR other button style ...
else
{
bIsPressed = (lpDIS->itemState & ODS_SELECTED);
}
// BOOL bIsPressed = (lpDIS->itemState & ODS_SELECTED);
BOOL bIsFocused = (lpDIS->itemState & ODS_FOCUS);
BOOL bIsDisabled = (lpDIS->itemState & ODS_DISABLED);
CRect itemRect = lpDIS->rcItem;
pDC->SetBkMode(TRANSPARENT);
// Prepare draw... paint button's area with background color
COLORREF bgColor;
if ((m_bMouseOnButton == TRUE) || (bIsPressed))
bgColor = m_crColors[BTNST_COLOR_BK_IN]; //GetActiveBgColor();
else
bgColor = m_crColors[BTNST_COLOR_BK_OUT]; //GetInactiveBgColor();
CBrush br(bgColor);
// Draw transparent?
if (m_bDrawTransparent == TRUE)
{
PaintBk(pDC);
}
else
{
pDC->FillRect(&itemRect, &br);
}
// Disegno lo sfondo del bottone
//CBrush br(GetSysColor(COLOR_BTNFACE));
//pDC->FillRect(&itemRect, &br);
// Draw pressed button
if (bIsPressed)
{
pDC->Draw3dRect(itemRect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_BTNHILIGHT));
}
else // ...else draw non pressed button
{
CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); // Light gray
CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Dark gray
CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black
//注释掉下句以总是画方框
//if (m_bMouseOnButton == TRUE )
{
pDC->Draw3dRect(itemRect, ::GetSysColor(COLOR_BTNHILIGHT), ::GetSysColor(COLOR_BTNSHADOW));
}
}
// Read the button's title
CString sTitle;
GetWindowText(sTitle);
// If we don't want the title displayed
if (m_bShowText == FALSE) sTitle.Empty();
CRect captionRect = lpDIS->rcItem;
// Draw the icon
if (m_csIcons[0].hIcon != NULL)
{
DrawTheIcon(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, bIsPressed, bIsDisabled);
}
if (m_csBitmaps[0].hBitmap != NULL)
{
pDC->SetBkColor(RGB(255,255,255));
DrawTheBitmap(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, bIsPressed, bIsDisabled);
} // if
// Write the button title (if any)
if (sTitle.IsEmpty() == FALSE)
{
// Draw the button's title
// If button is pressed then "press" title also
if (bIsPressed && m_bIsCheckBox == FALSE)
captionRect.OffsetRect(1, 1);
// ONLY FOR DEBUG
// Evidenzia il rettangolo in cui verra' centrata la caption
//CBrush brBtnShadow(RGB(255, 0, 0));
//pDC->FrameRect(&captionRect, &brBtnShadow);
/*
if ((m_bMouseOnButton == TRUE) || (bIsPressed))
{
pDC->SetTextColor(GetActiveFgColor());
pDC->SetBkColor(GetActiveBgColor());
}
else
{
pDC->SetTextColor(GetInactiveFgColor());
pDC->SetBkColor(GetInactiveBgColor());
}
*/
// Center text
CRect centerRect = captionRect;
pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER | DT_CALCRECT);
captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())/2);
/* RFU
captionRect.OffsetRect(0, (centerRect.Height() - captionRect.Height())/2);
captionRect.OffsetRect((centerRect.Width() - captionRect.Width())-4, (centerRect.Height() - captionRect.Height())/2);
*/
pDC->SetBkMode(TRANSPARENT);
/*
pDC->DrawState(captionRect.TopLeft(), captionRect.Size(), (LPCTSTR)sTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
TRUE, 0, (CBrush*)NULL);
*/
if (bIsDisabled)
{
captionRect.OffsetRect(1, 1);
pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER);
captionRect.OffsetRect(-1, -1);
pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER);
}
else
{
if ((m_bMouseOnButton == TRUE) ||(bIsPressed))
{
pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_IN]);
pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_IN]);
}
else
{
pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_OUT]);
pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_OUT]);
}
pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER);
}
}
if (m_bDrawFlatFocus == TRUE)
{
// Draw the focus rect
if (bIsFocused)
{
CRect focusRect = itemRect;
focusRect.DeflateRect(3, 3);
pDC->DrawFocusRect(&focusRect);
}
}
} // End of DrawItem
void CButtonST::DrawTheIcon(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, BOOL bIsDisabled)
{
BYTE byIndex = 0;
// Select the icon to use
if (m_bIsCheckBox == TRUE)
{
if (bIsPressed == TRUE)
{
byIndex = 0;
} // if
else
{
if (m_csIcons[1].hIcon != NULL)
byIndex = 1;
else
byIndex = 0; // No icon Out available
} // else
} // if
else
{
if (m_bMouseOnButton == TRUE || bIsPressed == TRUE)
{
byIndex = 0;
} // if
else
{
if (m_csIcons[1].hIcon != NULL)
byIndex = 1;
else
byIndex = 0; // No icon Out available
} // else
} // else
CRect rImage;
PrepareImageRect(bHasTitle, rpItem, rpTitle, bIsPressed, m_csIcons[byIndex].dwWidth, m_csIcons[byIndex].dwHeight, &rImage);
// Ole'!
pDC->DrawState( rImage.TopLeft(),
rImage.Size(),
m_csIcons[byIndex].hIcon,
(bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
(CBrush*)NULL);
} // End of DrawTheIcon
void CButtonST::PreSubclassWindow()
{
UINT nBS;
nBS = GetButtonStyle();
// Check if this is the default button
if (nBS & BS_DEFPUSHBUTTON) m_bIsDefault = TRUE;
// Check if this is a checkbox
if (nBS & BS_CHECKBOX) m_bIsCheckBox = TRUE;
// Add BS_OWNERDRAW style
SetButtonStyle(nBS | BS_OWNERDRAW);
CButton::PreSubclassWindow();
} // End of PreSubclassWindow
BOOL CButtonST::PreTranslateMessage(MSG* pMsg)
{
InitToolTip();
m_ToolTip.RelayEvent(pMsg);
return CButton::PreTranslateMessage(pMsg);
} // End of PreTranslateMessage
LRESULT CButtonST::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_LBUTTONDBLCLK)
{
message = WM_LBUTTONDOWN;
}
return CButton::DefWindowProc(message, wParam, lParam);
} // End of DefWindowProc
void CButtonST::SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint)
{
m_bDrawFlatFocus = bDrawFlatFocus;
// Repaint the button
if (bRepaint == TRUE) Invalidate();
} // End of SetFlatFocus
BOOL CButtonST::GetFlatFocus()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -