?? btnst.cpp
字號:
?
+
// Draw pressed button
if (bIsPressed)
{
if (m_bIsFlat == TRUE)
{
if (m_bDrawBorder == TRUE)
{
pDC->Draw3dRect(itemRect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_BTNHILIGHT));
}
}
else
{
CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
pDC->FrameRect(&itemRect, &brBtnShadow);
}
}
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_bIsFlat == TRUE)
{
if (m_bMouseOnButton == TRUE && m_bDrawBorder == TRUE)
{
pDC->Draw3dRect(itemRect, ::GetSysColor(COLOR_BTNHILIGHT), ::GetSysColor(COLOR_BTNSHADOW));
}
}
else
{
// Disegno i bordi a sinistra e in alto
// White line
pOldPen = pDC->SelectObject(&penBtnHiLight);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.left, itemRect.top);
pDC->LineTo(itemRect.right, itemRect.top);
// Light gray line
pDC->SelectObject(pen3DLight);
pDC->MoveTo(itemRect.left+1, itemRect.bottom-1);
pDC->LineTo(itemRect.left+1, itemRect.top+1);
pDC->LineTo(itemRect.right, itemRect.top+1);
// Disegno i bordi a destra e in basso
// Black line
pDC->SelectObject(pen3DDKShadow);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.top-1);
// Dark gray line
pDC->SelectObject(penBtnShadow);
pDC->MoveTo(itemRect.left+1, itemRect.bottom-2);
pDC->LineTo(itemRect.right-2, itemRect.bottom-2);
pDC->LineTo(itemRect.right-2, itemRect.top);
//
pDC->SelectObject(pOldPen);
}
}
// 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_bIsFlat == FALSE || (m_bIsFlat == TRUE && 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()
{
return m_bDrawFlatFocus;
} // End of GetFlatFocus
BOOL CButtonST::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// If a cursor was specified then use it!
if (m_hCursor != NULL)
{
::SetCursor(m_hCursor);
return TRUE;
}
return CButton::OnSetCursor(pWnd, nHitTest, message);
} // End of OnSetCursor
void CButtonST::SetTooltipText(LPCTSTR lpszText, BOOL bActivate)
{
// We cannot accept NULL pointer
if (lpszText == 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, lpszText, rectBtn, 1);
}
// Set text for tooltip
m_ToolTip.UpdateTipText(lpszText, this, 1);
m_ToolTip.Activate(bActivate);
} // End of SetTooltipText
void CButtonST::SetTooltipText(int nId, BOOL bActivate)
{
CString sText;
// load string resource
sText.LoadString(nId);
// If string resource is not empty
if (sText.IsEmpty() == FALSE) SetTooltipText((LPCTSTR)sText, bActivate);
} // End of SetTooltipText
void CButtonST::ActivateTooltip(BOOL bActivate)
{
// If there is no tooltip then do nothing
if (m_ToolTip.GetToolCount() == 0) return;
// Activate tooltip
m_ToolTip.Activate(bActivate);
} // End of EnableTooltip
BOOL CButtonST::GetDefault()
{
return m_bIsDefault;
} // End of GetDefault
void CButtonST::DrawTransparent(BOOL bRepaint)
{
m_bDrawTransparent = TRUE;
// Restore old bitmap (if any)
if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL)
{
m_dcBk.SelectObject(m_pbmpOldBk);
} // if
m_bmpBk.DeleteObject();
m_dcBk.DeleteDC();
// Repaint the button
if (bRepaint == TRUE) Invalidate();
} // End of DrawTransparent
void CButtonST::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
} // End of InitToolTip
void CButtonST::PaintBk(CDC* pDC)
{
CClientDC clDC(GetParent());
CRect rect;
CRect rect1;
GetClientRect(rect);
GetWindowRect(rect1);
GetParent()->ScreenToClient(rect1);
if (m_dcBk.m_hDC == NULL)
{
m_dcBk.CreateCompatibleDC(&clDC);
m_bmpBk.CreateCompatibleBitmap(&clDC, rect.Width(), rect.Height());
m_pbmpOldBk = m_dcBk.SelectObject(&m_bmpBk);
m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), &clDC, rect1.left, rect1.top, SRCCOPY);
} // if
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &m_dcBk, 0, 0, SRCCOPY);
} // End of PaintBk
HBRUSH CButtonST::CtlColor(CDC* pDC, UINT nCtlColor)
{
return (HBRUSH)::GetStockObject(NULL_BRUSH);
} // End of CtlColor
void CButtonST::OnSysColorChange()
{
CButton::OnSysColorChange();
m_dcBk.DeleteDC();
m_bmpBk.DeleteObject();
} // End of OnSysColorChange
BOOL CButtonST::OnClicked()
{
if (m_bIsCheckBox == TRUE)
{
m_nCheck = !m_nCheck;
Invalidate();
} // if
else
{
// Handle the URL (if any)
if (::lstrlen(m_szURL) > 0)
::ShellExecute(NULL, _T("open"), m_szURL, NULL,NULL, SW_SHOWMAXIMIZED);
} // else
return FALSE;
} // End of OnClicked
void CButtonST::SetCheck(int nCheck, BOOL bRepaint)
{
if (m_bIsCheckBox == TRUE)
{
if (nCheck == 0) m_nCheck = 0;
else m_nCheck = 1;
if (bRepaint == TRUE) Invalidate();
} // if
} // End of SetCheck
int CButtonST::GetCheck()
{
return m_nCheck;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -