CheckRadioDlg.h:
// CCheckRadioDlg 对话框
class CCheckRadioDlg : public CDialog
{
// 构造
public:
CCheckRadioDlg(CWnd* pParent = NULL); // 标准构造函数
CRect m_RectSample;
...
CheckRadioDlg.cpp:
BOOL CCheckRadioDlg::OnInitDialog()
{...
// TODO: 在此添加额外的初始化代码
GetDlgItem(IDC_SAMPLE)->GetWindowRect(&m_RectSample);
ScreenToClient(&m_RectSample);
int Border=(m_RectSample.right - m_RectSample.left)/8;
m_RectSample.InflateRect(-Border, -Border);
...}
void CCheckRadioDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
if(IsDlgButtonChecked(IDC_LIGHT))
m_bLight = TRUE;
COLORREF Color=RGB(m_bRed ? (m_bLight==TRUE ? 255 : 128) :0,
m_bGreen ? (m_bLight==TRUE ? 255 :128) :0,
m_bBlue ? (m_bLight==TRUE ? 255 :128) :0);
CBrush Brush(Color);
CPaintDC dc(this);
dc.FillRect(&m_RectSample, &Brush);
CDialog::OnPaint();
}
}
void CCheckRadioDlg::OnBnClickedRed()
{
m_bRed=IsDlgButtonChecked(IDC_RED);
InvalidateRect(&m_RectSample);
UpdateWindow();
}
void CCheckRadioDlg::OnBnClickedGreen()
{
m_bGreen=IsDlgButtonChecked(IDC_GREEN);
InvalidateRect(&m_RectSample);
UpdateWindow();
}
void CCheckRadioDlg::OnBnClickedBlue()
{
m_bBlue=IsDlgButtonChecked(IDC_BLUE);
InvalidateRect(&m_RectSample);
UpdateWindow();
}
void CCheckRadioDlg::OnBnClickedLight()
{
if(IsDlgButtonChecked(IDC_LIGHT))
{
m_bLight=TRUE;
InvalidateRect(&m_RectSample);
UpdateWindow();
}
}
void CCheckRadioDlg::OnBnClickedDark()
{
if(IsDlgButtonChecked(IDC_DARK))
{
m_bLight=FALSE;
InvalidateRect(&m_RectSample);
UpdateWindow();
}
}
注:
InflateRect:
InflateRect inflates CRect by moving its sides away from its center.
void InflateRect( int x, int y ) throw( );void InflateRect( SIZE size ) throw( );voidInflateRect( LPCRECT lpRect ) throw( );void InflateRect( int l, int t, int r, int b ) throw( ); |
Parameters
- x
Specifies the number of units to inflate the left and right sides of CRect.
- y
Specifies the number of units to inflate the top and bottom of CRect.
- size
A SIZE or CSize that specifies the number of units to inflate CRect. The cx value specifies the number of units to inflate the left and right sides and the cy value specifies the number of units to inflate the top and bottom.
- lpRect
Points to a RECT structure or CRect that specifies the number of units to inflate each side.
- l
Specifies the number of units to inflate the left side of CRect.
- t
Specifies the number of units to inflate the top of CRect.
- r
Specifies the number of units to inflate the right side of CRect.
- b
Specifies the number of units to inflate the bottom of CRect.
Remarks
To do this, InflateRect subtracts units from the left and top and adds units to the right and bottom. The parameters of InflateRect are signed values; positive values inflate CRect and negative values deflate it.
The first two overloads inflate both pairs of opposite sides of CRect so that its total width is increased by two times x (or cx) and its total height is increased by two times y (or cy). The other two overloads inflate each side of CRect independently of the others.
Example
CRect rect(0, 0, 300, 300);rect.InflateRect(50, 200);// rect is now (-50, -200, 350, 500)ASSERT(rect == CRect(-50, -200, 350, 500));
InvalidateRect:
Invalidates the client area within the given rectangle by adding that rectangle to the CWndupdate region.
void InvalidateRect( LPCRECT lpRect, BOOL bErase = TRUE ); |
Parameters
- lpRect
Points to a CRect object or a RECT structure that contains the rectangle (in client coordinates) to be added to the update region. If lpRect is NULL, the entire client area is added to the region.
- bErase
Specifies whether the background within the update region is to be erased.
Remarks
The invalidated rectangle, along with all other areas in the update region, is marked for painting when the next WM_PAINT message is sent. The invalidated areas accumulate in the update region until the region is processed when the next WM_PAINT call occurs, or until the region is validated by the ValidateRect or ValidateRgn member function.
The bErase parameter specifies whether the background within the update area is to be erased when the update region is processed. If bErase is TRUE, the background is erased when theBeginPaint member function is called; if bErase is FALSE, the background remains unchanged. If bErase is TRUE for any part of the update region, the background in the entire region is erased, not just in the given part.
Windows sends a WM_PAINT message whenever the CWnd update region is not empty and there are no other messages in the application queue for that window.