📄 macbuttons.cpp
字号:
// Return Value: None.
//
// Parameters : None.
//
// Remarks : Invalidates the check rectangle of a radio button or check box.
//
{
CRect rect;
GetClientRect(rect);
InvalidateRect(GetCheckRect(rect, GetStyle()));
} // RedrawCheck
//-------------------------------------------------------------------
//
void CMacButton::DrawButtonText(CDC *pDC, CRect &rect, const CString &sText, UINT nStyle, UINT nState)
//
// Return Value: None.
//
// Parameters : pDC - A pointer to the DC being drawn on.
// rect - The button's rectangle.
// sText - The button's text.
// nStyle - The button's style.
// nState - The button's state.
//
// Remarks : Draws the text on the button.
//
{
CFont *pFont = GetFont();
CFont *pOldFont = (CFont *)pDC->SelectObject(pFont);
CFont fontBold;
if (m_bBold)
{
LOGFONT lf;
pFont->GetLogFont(&lf);
lf.lfWeight = FW_BOLD;
fontBold.CreateFontIndirect(&lf);
pDC->SelectObject(&fontBold);
}
CSize sizeText = pDC->GetTextExtent(sText);
// Determine the rect for the text.
if ((m_nType == TYPE_STANDARD) || (nStyle & BS_PUSHLIKE))
{
if (nStyle & BS_LEFT)
rect.left += 5;
else if (nStyle & BS_RIGHT)
rect.left = rect.right - sizeText.cx - 5;
else
rect.left = (rect.Width() - sizeText.cx) >> 1;
rect.right = rect.left + sizeText.cx;
if (nStyle & BS_TOP)
rect.top += (m_nType == TYPE_RADIO? 4 : 5);
else if (nStyle & BS_BOTTOM)
rect.top = rect.bottom - sizeText.cy - 5;
else
rect.top = (rect.Height() - sizeText.cy) >> 1;
rect.bottom = rect.top + sizeText.cy;
}
else
{
if (nStyle & BS_LEFT)
rect.left = (nStyle & BS_LEFTTEXT ? 2 : CHECKBOX_HEIGHT + 5);
else if (nStyle & BS_RIGHT)
rect.left = rect.right - sizeText.cx - (nStyle & BS_LEFTTEXT ? CHECKBOX_HEIGHT + 10 : 5);
else
{
if (m_nType == TYPE_CHECKBOX || m_nType == TYPE_RADIO)
rect.left = (nStyle & BS_LEFTTEXT ? 2 : CHECKBOX_HEIGHT + 5);
else
rect.left = (rect.Width() - sizeText.cx) >> 1;
}
rect.right = rect.left + sizeText.cx + (nStyle & BS_LEFTTEXT ? 2 : 0);
if (nStyle & BS_TOP)
rect.top = rect.top;
else if (nStyle & BS_BOTTOM)
rect.top = rect.bottom - sizeText.cy - 3;
else
rect.top = ((rect.Height() - sizeText.cy) >> 1) - 1;
rect.bottom = rect.top + sizeText.cy;
}
if (((m_nType == TYPE_STANDARD) && (nState & ODS_SELECTED)) ||
(nStyle & BS_PUSHLIKE) && (m_bMouseDown || ((nStyle & BS_FLAT) && m_nCheck)))
pDC->SetTextColor(m_crHilight);
if ((m_nType == TYPE_CHECKBOX) && (nStyle & BS_PUSHLIKE) && (m_nCheck == 2))
pDC->SetTextColor(m_crShadow);
// Draw the text.
pDC->SetBkMode(TRANSPARENT);
if (nState & ODS_DISABLED)
{
pDC->SetTextColor(m_crShadow);
pDC->DrawText(sText, rect, DT_CENTER);
}
else
pDC->DrawText(sText, rect, DT_CENTER);
// Restore the original font.
pDC->SelectObject(pOldFont);
if (m_bBold)
fontBold.DeleteObject();
} // DrawButtonText
//-------------------------------------------------------------------
//
void CMacButton::SetImageEffect(int nEffect)
//
// Return Value: None.
//
// Parameters : nEffect - The effect to add when drawing the image.
// Can be one of the following values:
// - IMAGE_EFFECT_NONE - No effect is added to the image.
// - IMAGE_EFFECT_RAISED - The image will appear raised.
// - IMAGE_EFFECT_SUNKEN - The image will appear sunken.
//
// Remarks : Sets the image effect member.
//
{
m_nImageEffect = nEffect;
RedrawWindow();
} // SetImageEffect
//-------------------------------------------------------------------
//
void CMacButton::SetCheck(int nCheck)
//
// Return Value: None.
//
// Parameters : nCheck - Specifies the check state. This parameter
// can be one of the following:
// Value Meaning
// 0 Set the button state to unchecked.
// 1 Set the button state to checked.
// 2 Set the button state to indeterminate.
//
// Remarks : Sets or resets the check state of a radio button or
// check box. This member function has no effect on a pushbutton.
//
{
if (m_nType == TYPE_STANDARD)
return;
int nOldCheck = m_nCheck;
m_nCheck = nCheck;
if (m_nCheck != nOldCheck)
{
if (GetStyle() & BS_PUSHLIKE)
RedrawWindow();
else
RedrawCheck();
}
} // SetCheck
//-------------------------------------------------------------------
//
int CMacButton::GetCheck() const
//
// Return Value: 1 if checked, 0 otherwise.
//
// Parameters : None.
//
// Remarks : Retrieves the check state of button.
//
{
return m_nCheck;
} // GetCheck
//-------------------------------------------------------------------
//
void CMacButton::SetBold(BOOL bBold /*= TRUE*/)
//
// Return Value: None.
//
// Parameters : bBold - Used to sed the m_bBold flag. Default value: TRUE.
//
// Remarks : Sets the m_bBold flag.
//
{
m_bBold = bBold;
RedrawWindow();
} // SetBold
//-------------------------------------------------------------------
//
BOOL CMacButton::GetBold() const
//
// Return Value: The bold flag.
//
// Parameters : None.
//
// Remarks : Returns the bold flag.
//
{
return m_bBold;
} // GetBold
/////////////////////////////////////////////////////////////////////////////
//
// CMacCheckBox class, version 2.0
//
// Copyright (c) 1999, 2000 Paul M. Meidinger (pmmeidinger@yahoo.com)
//
// Thanks to:
// Eric Hwang <erichw@21cn.com>
// For fixing the problem that was not sending a message
// to the parent when the button was clicked.
//
// History:
// PMM 12/13/1999 Initial implementation.
//
// PMM 12/17/1999 Modified drawing code to use a memory DC.
// Added CPen member variables in an attempt to
// speed up drawing. Made other minor changes.
//
// PMM 12/29/1999 Fixed a bug that would not send a message to the parent
// when the button was clicked (fix by Eric Hwang). Added
// code to draw a push-like button when that style is used.
//
/////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------
//
CMacCheckBox::CMacCheckBox()
: CMacButton()
//
// Return Value: None.
//
// Parameters : None.
//
// Remarks : Standard constructor.
//
{
m_nType = TYPE_CHECKBOX;
m_nCheckStyle = CHECK_STYLE_CHECK;
} // CMacCheckBox
//-------------------------------------------------------------------
//
CMacCheckBox::~CMacCheckBox()
//
// Return Value: None.
//
// Parameters : None.
//
// Remarks : Destructor. Does nothing.
//
{
} // ~CMacCheckBox
BEGIN_MESSAGE_MAP(CMacCheckBox, CMacButton)
//{{AFX_MSG_MAP(CMacCheckBox)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_KEYUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDBLCLK()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMacCheckBox message handlers
//-------------------------------------------------------------------
//
void CMacCheckBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
//
// Return Value: None.
//
// Parameters : lpDIS - A long pointer to a DRAWITEMSTRUCT structure.
//
// Remarks : Called by the framework when a visual aspect of an
// owner-drawn button has changed.
//
{
DrawButton(lpDIS);
} // DrawItem
//-------------------------------------------------------------------
//
void CMacCheckBox::OnLButtonUp(UINT /*nFlags*/, CPoint point)
//
// Return Value: None.
//
// Parameters : nFlags - Indicates whether various virtual keys are down.
// point - Specifies the x- and y-coordinate of the cursor.
// These coordinates are always relative to the upper-left
// corner of the window.
//
// Remarks : The framework calls this member function when the user
// releases the left mouse button. Checks to see if the mouse
// was released over the button. If so, check it.
//
{
ReleaseCapture();
UINT nStyle = GetStyle();
if (!(nStyle & BS_AUTOCHECKBOX))
return;
CRect rect;
GetClientRect(rect);
if (rect.PtInRect(point))
{
m_nCheck = (m_nCheck == 0 ? 1 : 0);
// Send notify message to parent window.
// Added by Eric Hwang.
GetParent()->SendMessage(WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), BN_CLICKED), (LPARAM)GetSafeHwnd());
}
m_bMouseDown = FALSE;
if (nStyle & BS_PUSHLIKE)
RedrawWindow();
else
RedrawCheck();
} // OnLButtonUp
//-------------------------------------------------------------------
//
void CMacCheckBox::OnLButtonDown(UINT /*nFlags*/, CPoint /*point*/)
//
// Return Value: None.
//
// Parameters : nFlags - Indicates whether various virtual keys are down.
// point - Specifies the x- and y-coordinate of the cursor.
// These coordinates are always relative to the upper-left
// corner of the window.
//
// Remarks : The framework calls this member function when the user
// presses the left mouse button. Sets focus to this button,
// captures the mouse, and redraws the check.
//
{
m_bMouseDown = TRUE;
SetFocus();
SetCapture();
if (GetStyle() & BS_PUSHLIKE)
RedrawWindow();
else
RedrawCheck();
} // OnLButtonDown
//-------------------------------------------------------------------
//
void CMacCheckBox::OnLButtonDblClk(UINT /*nFlags*/, CPoint point)
//
// Return Value: None.
//
// Parameters : nFlags - Indicates whether various virtual keys are down.
// point - Specifies the x- and y-coordinate of the cursor.
// These coordinates are always relative to the upper-left
// corner of the window.
//
// Remarks : The framework calls this member function when the user
// double-clicks the left mouse button. The WM_LBUTTONDOWN
// message is sent to simulate a single click.
//
{
SendMessage(WM_LBUTTONDOWN, 0, MAKELPARAM(point.x, point.y));
} // OnLButtonDblClk
//-------------------------------------------------------------------
//
void CMacCheckBox::OnMouseMove(UINT /*nFlags*/, CPoint point)
//
// Return Value: None.
//
// Parameters : nFlags - Indicates whether various virtual keys are down.
// point - Specifies the x- and y-coordinate of the cursor.
// These coordinates are always relative to the upper-left
// corner of the window.
//
// Remarks : The framework calls this member function when the
// mouse cursor moves. Checks to see if the mouse is over
// the button, and redraws the check if it is, but wasn't previously.
//
{
if (GetCapture() != this)
return;
BOOL bWasMouseDown = m_bMouseDown;
CRect rect;
GetClientRect(rect);
m_bMouseDown = rect.PtInRect(point);
if (bWasMouseDown != m_bMouseDown)
{
if (GetStyle() & BS_PUSHLIKE)
RedrawWindow();
else
RedrawCheck();
}
} // OnMouseMove
//-------------------------------------------------------------------
//
void CMacCheckBox::OnKeyUp(UINT nChar, UINT /*nRepCnt*/, UINT /*nFlags*/)
//
// Return Value: None.
//
// Parameters : nChar - Specifies the virtual-key code of the given key.
// nRepCnt - Repeat count (the number of times the keystroke
// is repeated as a result of the user holding down the key).
// nFlags - Specifies the scan code, key-transition code, previous
// key state, and context code
//
// Remarks : The framework calls this member function when a nonsystem
// key is released. Checks/unchecks the button when the space bar
// is pressed.
//
{
if (nChar == VK_SPACE)
{
m_bMouseDown = FALSE;
m_nCheck = (m_nCheck == 0 ? 1 : 0);
// Send notify message to parent window.
// Added by Eric Hwang.
GetParent()->SendMessage(WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), BN_CLICKED), (LPARAM)GetSafeHwnd());
if (GetStyle() & BS_PUSHLIKE)
RedrawWindow();
else
RedrawCheck();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -