📄 coolb.cpp
字号:
AfxGetResourceHandle (),
MAKEINTRESOURCE (uiBmpResId),
IMAGE_BITMAP,
0, 0,
LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS);
ASSERT (hbmp != NULL);
HBITMAP hbmpHot = NULL;
if (uiBmpHotResId != 0)
{
hbmpHot = (HBITMAP) ::LoadImage (
AfxGetResourceHandle (),
MAKEINTRESOURCE (uiBmpHotResId),
IMAGE_BITMAP,
0, 0,
LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS);
ASSERT (hbmp != NULL);
}
SetImage (hbmp, TRUE /* AutoDestroy */, hbmpHot);
}
//****************************************************************************
void CCoolButton::OnCancelMode()
{
CButton::OnCancelMode();
if (m_bCaptured)
{
ReleaseCapture ();
m_bCaptured = FALSE;
Invalidate ();
UpdateWindow ();
}
}
//****************************************************************************
void CCoolButton::OnMouseMove(UINT nFlags, CPoint point)
{
m_bHover = FALSE;
if ((nFlags & MK_LBUTTON) || m_nFlatStyle != BUTTONSTYLE_3D)
{
BOOL bRedraw = FALSE;
CRect rectClient;
GetClientRect (rectClient);
if (rectClient.PtInRect (point))
{
m_bHover = TRUE;
if (!m_bHighlighted)
{
m_bHighlighted = TRUE;
bRedraw = TRUE;
}
if ((nFlags & MK_LBUTTON) && !m_bPushed)
{
m_bPushed = TRUE;
bRedraw = TRUE;
}
if (!m_bCaptured)
{
SetCapture ();
m_bCaptured = TRUE;
bRedraw = TRUE;
}
}
else
{
if (nFlags & MK_LBUTTON)
{
if (m_bPushed)
{
m_bPushed = FALSE;
bRedraw = TRUE;
}
}
else if (m_bHighlighted)
{
m_bHighlighted = FALSE;
bRedraw = TRUE;
}
if (m_bCaptured && (!nFlags & MK_LBUTTON))
{
ReleaseCapture ();
m_bCaptured = FALSE;
bRedraw = TRUE;
}
}
if (bRedraw)
{
Invalidate ();
UpdateWindow ();
}
}
CButton::OnMouseMove(nFlags, point);
}
//****************************************************************************
void CCoolButton::OnLButtonDown(UINT nFlags, CPoint point)
{
m_bPushed = TRUE;
m_bHighlighted = TRUE;
if (!m_bCaptured)
{
SetCapture ();
m_bCaptured = TRUE;
}
Invalidate ();
UpdateWindow ();
CButton::OnLButtonDown(nFlags, point);
}
//****************************************************************************
void CCoolButton::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bPushed = FALSE;
m_bHighlighted = FALSE;
Invalidate ();
UpdateWindow ();
CButton::OnLButtonUp(nFlags, point);
if (m_bCaptured)
{
ReleaseCapture ();
m_bCaptured = FALSE;
}
}
//****************************************************************************
CSize CCoolButton::SizeToContent (BOOL bCalcOnly)
{
ASSERT (GetSafeHwnd () != NULL);
CClientDC dc (this);
CString strText;
GetWindowText (strText);
CSize sizeText = dc.GetTextExtent (strText);
int cx,cy;
switch (m_nDisplayStyle)
{
case BUTTONSTYLE_IMAGE:
cx = m_sizeImage.cx + nImageHorzMargin;
cy = m_sizeImage.cy + nVertMargin * 2;
break;
case BUTTONSTYLE_TEXT:
cx = sizeText.cx + nImageHorzMargin;
cy = sizeText.cy + nVertMargin * 2;
break;
case BUTTONSTYLE_BOTH:
cx = sizeText.cx + m_sizeImage.cx + nImageHorzMargin;
if (sizeText.cx > 0)
{
cx += nImageHorzMargin;
}
cy = max (sizeText.cy, m_sizeImage.cy) + nVertMargin * 2;
break;
}
if (!bCalcOnly)
{
SetWindowPos (NULL, -1, -1, cx, cy,
SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
}
return CSize (cx, cy);
}
//****************************************************************************
BOOL CCoolButton::PreTranslateMessage(MSG* pMsg)
{
if (m_wndToolTip.GetSafeHwnd () != NULL)
{
if (pMsg->message == WM_LBUTTONDOWN ||
pMsg->message == WM_LBUTTONUP ||
pMsg->message == WM_MOUSEMOVE)
{
m_wndToolTip.RelayEvent(pMsg);
}
}
return CButton::PreTranslateMessage(pMsg);
}
//****************************************************************************
void CCoolButton::SetTooltip (LPCTSTR lpszToolTipText)
{
ASSERT (GetSafeHwnd () != NULL);
if (lpszToolTipText == NULL)
{
if (m_wndToolTip.GetSafeHwnd () != NULL)
{
m_wndToolTip.Activate (FALSE);
}
}
else
{
if (m_wndToolTip.GetSafeHwnd () != NULL)
{
m_wndToolTip.UpdateTipText (lpszToolTipText, this);
}
else
{
m_wndToolTip.Create (this, TTS_ALWAYSTIP);
m_wndToolTip.AddTool (this, lpszToolTipText);
}
m_wndToolTip.Activate (TRUE);
}
}
//*****************************************************************************
void CCoolButton::OnDrawFocusRect (CDC* pDC, const CRect& rectClient)
{
ASSERT_VALID (pDC);
CRect rectFocus = rectClient;
if (m_nFlatStyle == BUTTONSTYLE_FLAT)
{
rectFocus.DeflateRect (2, 2);
}
else
{
rectFocus.DeflateRect (3, 3);
}
pDC->DrawFocusRect (rectFocus);
}
//******************************************************************************
BOOL CCoolButton::CreateDisabledImage ()
{
if (m_hBitmapDisabled != NULL)
{
::DeleteObject (m_hBitmapDisabled);
m_hBitmapDisabled = NULL;
}
if (m_hBitmap == NULL)
{
ASSERT (FALSE);
return FALSE;
}
//-------------------------------------------------------
// Create memory source DC and select an original bitmap:
//-------------------------------------------------------
CDC memDCSrc;
memDCSrc.CreateCompatibleDC (NULL);
HBITMAP hOldBitmapSrc = NULL;
int iBitmapWidth;
int iBitmapHeight;
//-------------------------------
// Get original bitmap attrbutes:
//-------------------------------
BITMAP bmp;
if (::GetObject (m_hBitmap, sizeof (BITMAP), &bmp) == 0)
{
return FALSE;
}
hOldBitmapSrc = (HBITMAP) memDCSrc.SelectObject (m_hBitmap);
if (hOldBitmapSrc == NULL)
{
return FALSE;
}
iBitmapWidth = bmp.bmWidth;
iBitmapHeight = bmp.bmHeight;
//----------------------------------------------------------
// Create a new bitmap compatibel with the source memory DC:
// (original bitmap SHOULD BE ALREADY SELECTED!):
//----------------------------------------------------------
m_hBitmapDisabled = (HBITMAP) ::CreateCompatibleBitmap (memDCSrc,
iBitmapWidth,
iBitmapHeight);
if (m_hBitmapDisabled == NULL)
{
memDCSrc.SelectObject (hOldBitmapSrc);
return FALSE;
}
//------------------------------
// Create memory destination DC:
//------------------------------
CDC memDCDst;
memDCDst.CreateCompatibleDC (&memDCSrc);
HBITMAP hOldBitmapDst = (HBITMAP) memDCDst.SelectObject (m_hBitmapDisabled);
if (hOldBitmapDst == NULL)
{
memDCSrc.SelectObject (hOldBitmapSrc);
::DeleteObject (m_hBitmapDisabled);
m_hBitmapDisabled = NULL;
return FALSE;
}
//-----------------------------
// Copy original bitmap to new:
//-----------------------------
memDCDst.BitBlt (0, 0, iBitmapWidth, iBitmapHeight,
&memDCSrc, 0, 0, SRCCOPY);
//------------------------------------
// Change "3dface" colors to White:
//------------------------------------
for (int x = 0; x < iBitmapWidth; x ++)
{
for (int y = 0; y < iBitmapHeight; y ++)
{
COLORREF clrOrig = ::GetPixel (memDCDst, x, y);
COLORREF clrBtnFace;
clrBtnFace = ::GetSysColor(COLOR_BTNFACE);
COLORREF clrNew = (clrOrig == clrBtnFace) ?
RGB (255, 255, 255) : clrOrig;
if (clrOrig != clrNew)
{
::SetPixel (memDCDst, x, y, clrNew);
}
}
}
memDCDst.SelectObject (hOldBitmapDst);
memDCSrc.SelectObject (hOldBitmapSrc);
return TRUE;
}
//******************************************************************************
void CCoolButton::OnEnable(BOOL bEnable)
{
if (!bEnable)
{
// control disabled
m_bPushed = FALSE;
m_bHighlighted = FALSE;
if (m_bCaptured)
{
ReleaseCapture ();
m_bCaptured = FALSE;
}
}
CButton::OnEnable(bEnable);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -