📄 roundbutton.cpp
字号:
m_ptLeft.x = m_nRadius;
m_ptRight.x = rect.right - m_nRadius - 1;
// Set the window region so mouse clicks only activate the round section
// of the button
m_rgn.DeleteObject();
SetWindowRgn(NULL, FALSE);
m_rgn.CreateEllipticRgnIndirect(rect);
SetWindowRgn(m_rgn, TRUE);
// Convert client coords to the parents client coords
ClientToScreen(rect);
CWnd* pParent = GetParent();
if (pParent) pParent->ScreenToClient(rect);
// Resize the window if it is not stretched
if(!m_bStretch) MoveWindow(rect.left, rect.top, rect.Width(), rect.Height(), TRUE);
}
void CRoundButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct != NULL);
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect = lpDrawItemStruct->rcItem;
UINT state = lpDrawItemStruct->itemState;
UINT nStyle = GetStyle();
int nRadius = m_nRadius;
int nSavedDC = pDC->SaveDC();
pDC->SelectStockObject(NULL_BRUSH);
pDC->FillSolidRect(rect, ::GetSysColor(COLOR_BTNFACE));
// Draw the focus circle around the button for non-stretched buttons
if ((state & ODS_FOCUS) && m_bDrawDashedFocusCircle && !m_bStretch)
DrawCircle(pDC, m_ptCentre, nRadius--, RGB(0,0,0));
// Draw the raised/sunken edges of the button (unless flat)
if (nStyle & BS_FLAT) {
// for stretched buttons: draw left and right arcs and connect the with lines
if(m_bStretch)//不是正方形
{
CPen* oldpen;
CRect LeftBound(0,0,nRadius*2,nRadius*2);
CRect RightBound(m_ptRight.x-nRadius,0,m_ptRight.x+nRadius,nRadius*2);
oldpen = pDC->SelectObject(new CPen(PS_SOLID, 1, ::GetSysColor(COLOR_3DDKSHADOW)));
pDC->Arc(LeftBound, CPoint(m_ptLeft.x,0), CPoint(m_ptLeft.x,nRadius*2));
pDC->Arc(RightBound, CPoint(m_ptRight.x,nRadius*2), CPoint(m_ptRight.x,0));
pDC->MoveTo(m_ptLeft.x,0); pDC->LineTo(m_ptRight.x,0);
pDC->MoveTo(m_ptLeft.x,nRadius*2-1); pDC->LineTo(m_ptRight.x,nRadius*2-1);
nRadius--;
LeftBound.DeflateRect(1,1);
RightBound.DeflateRect(1,1);
delete pDC->SelectObject(new CPen(PS_SOLID, 1, ::GetSysColor(COLOR_3DHIGHLIGHT)));
pDC->Arc(LeftBound, CPoint(m_ptLeft.x,1), CPoint(m_ptLeft.x,nRadius*2));
pDC->Arc(RightBound, CPoint(m_ptRight.x,nRadius*2), CPoint(m_ptRight.x,0));
pDC->MoveTo(m_ptLeft.x,1); pDC->LineTo(m_ptRight.x,1);
pDC->MoveTo(m_ptLeft.x,nRadius*2); pDC->LineTo(m_ptRight.x,nRadius*2);
delete pDC->SelectObject(oldpen);
}
// for non-stretched buttons: draw two circles
else
{
DrawCircle(pDC, m_ptCentre, nRadius--, ::GetSysColor(COLOR_3DDKSHADOW));
DrawCircle(pDC, m_ptCentre, nRadius--, ::GetSysColor(COLOR_3DHIGHLIGHT));
}
} else {
if ((state & ODS_SELECTED)) {
// draw the circular segments for stretched AND non-stretched buttons
DrawCircleLeft(pDC, m_ptLeft, nRadius,
::GetSysColor(COLOR_3DDKSHADOW), ::GetSysColor(COLOR_3DHIGHLIGHT));
DrawCircleRight(pDC, m_ptRight, nRadius,
::GetSysColor(COLOR_3DDKSHADOW), ::GetSysColor(COLOR_3DHIGHLIGHT));
DrawCircleLeft(pDC, m_ptLeft, nRadius-1,
::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DLIGHT));
DrawCircleRight(pDC, m_ptRight, nRadius-1,
::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DLIGHT));
// draw connecting lines for stretched buttons only
if (m_bStretch)
{
CPen* oldpen;
// CPen* mypen;
oldpen = pDC->SelectObject(new CPen(PS_SOLID, 1, ::GetSysColor(COLOR_3DDKSHADOW)));
pDC->MoveTo(m_ptLeft.x, 1); pDC->LineTo(m_ptRight.x, 1);
delete pDC->SelectObject(new CPen(PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW)));
pDC->MoveTo(m_ptLeft.x, 2); pDC->LineTo(m_ptRight.x, 2);
delete pDC->SelectObject(new CPen(PS_SOLID, 1, ::GetSysColor(COLOR_3DLIGHT)));
pDC->MoveTo(m_ptLeft.x, m_ptLeft.y + nRadius-1); pDC->LineTo(m_ptRight.x, m_ptLeft.y + nRadius-1);
delete pDC->SelectObject(new CPen(PS_SOLID, 1, ::GetSysColor(COLOR_3DHIGHLIGHT)));
pDC->MoveTo(m_ptLeft.x, m_ptLeft.y + nRadius); pDC->LineTo(m_ptRight.x, m_ptLeft.y + nRadius);
delete pDC->SelectObject(oldpen);
}
} else {
// draw the circular segments for stretched AND non-stretched buttons
DrawCircleLeft(pDC, m_ptLeft, nRadius,
::GetSysColor(COLOR_3DHIGHLIGHT), ::GetSysColor(COLOR_3DDKSHADOW));
DrawCircleRight(pDC, m_ptRight, nRadius,
::GetSysColor(COLOR_3DHIGHLIGHT), ::GetSysColor(COLOR_3DDKSHADOW));
DrawCircleLeft(pDC, m_ptLeft, nRadius - 1,
::GetSysColor(COLOR_3DLIGHT), ::GetSysColor(COLOR_3DSHADOW));
DrawCircleRight(pDC, m_ptRight, nRadius - 1,
::GetSysColor(COLOR_3DLIGHT), ::GetSysColor(COLOR_3DSHADOW));
// draw connecting lines for stretched buttons only
if (m_bStretch)
{
CPen* oldpen;
oldpen = pDC->SelectObject(new CPen(PS_SOLID, 1, pDC->GetPixel(m_ptLeft.x, 1)));
pDC->MoveTo(m_ptLeft.x, 1); pDC->LineTo(m_ptRight.x, 1);
delete pDC->SelectObject(new CPen(PS_SOLID, 1, pDC->GetPixel(m_ptLeft.x, 2)));
pDC->MoveTo(m_ptLeft.x, 2); pDC->LineTo(m_ptRight.x, 2);
delete pDC->SelectObject(new CPen(PS_SOLID, 1, pDC->GetPixel(m_ptLeft.x, m_ptLeft.y + nRadius)));
pDC->MoveTo(m_ptLeft.x, m_ptLeft.y + nRadius); pDC->LineTo(m_ptRight.x, m_ptLeft.y + nRadius);
delete pDC->SelectObject(new CPen(PS_SOLID, 1, pDC->GetPixel(m_ptLeft.x, m_ptLeft.y + nRadius - 1)));
pDC->MoveTo(m_ptLeft.x, m_ptLeft.y + nRadius - 1); pDC->LineTo(m_ptRight.x, m_ptLeft.y + nRadius - 1);
delete pDC->SelectObject(oldpen);
}
}
}
// draw the text if there is any
if (hIcon)
{CRect rImage;
rImage=rect;
rImage.left=(rect.Width()-hIcon_w)/2;
rImage.top=(rect.Height()-hIcon_h)/2;
if(state & ODS_SELECTED)
{ rImage.OffsetRect(1, 1);
}
//rImage.right=rImage.left+hIcon_w;
// rImage.bottom=rImage.top+hIcon_h;
pDC->DrawState( rImage.TopLeft(),
rImage.Size(),
hIcon,
DSS_NORMAL,
(CBrush*)NULL);
} // if
CString strText;
GetWindowText(strText);
if (!strText.IsEmpty())
{
CRgn rgn;
if (m_bStretch){
rgn.CreateRectRgn(m_ptLeft.x-nRadius/2, m_ptCentre.y-nRadius,
m_ptRight.x+nRadius/2, m_ptCentre.y+nRadius);}
else{
rgn.CreateEllipticRgn(m_ptCentre.x-nRadius, m_ptCentre.y-nRadius,
m_ptCentre.x+nRadius, m_ptCentre.y+nRadius);}
pDC->SelectClipRgn(&rgn);
CSize Extent = pDC->GetTextExtent(strText);
CPoint pt = CPoint( m_ptCentre.x - Extent.cx/2, m_ptCentre.y - Extent.cy/2 );
if (state & ODS_SELECTED) pt.Offset(1,1);
pDC->SetBkMode(TRANSPARENT);
if (state & ODS_DISABLED)
pDC->DrawState(pt, Extent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
else
{
// changed this code to give the text a 3d-look
COLORREF oldcol = pDC->SetTextColor(::GetSysColor(COLOR_3DHIGHLIGHT));
pDC->TextOut(pt.x, pt.y, strText);
pDC->SetTextColor(textColor); //::GetSysColor(COLOR_3DDKSHADOW)
pDC->TextOut(pt.x-1, pt.y-1, strText);
pDC->SetTextColor(oldcol);
}
pDC->SelectClipRgn(NULL);
rgn.DeleteObject();
}
// Draw the focus circle on the inside of the button if it is non-stretched
if ((state & ODS_FOCUS) && m_bDrawDashedFocusCircle && !m_bStretch)
DrawCircle(pDC, m_ptCentre, nRadius-2, RGB(0,0,0), TRUE);
pDC->RestoreDC(nSavedDC);
}
DWORD CRoundButton::SetIcon(int nIconIn)
{
HICON hIconIn = NULL;
HINSTANCE hInstResource = NULL;
BOOL bRetValue;
ICONINFO ii;
// Find correct resource handle
hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconIn), RT_GROUP_ICON);
// Set icon when the mouse is IN the button
hIconIn = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconIn), IMAGE_ICON, 0, 0, 0);
if (hIcon)
::DestroyIcon(hIcon);
hIcon=hIconIn;
::ZeroMemory(&ii, sizeof(ICONINFO));
bRetValue = ::GetIconInfo(hIconIn, &ii);
if (bRetValue == FALSE)
{
return false;
}
hIcon_w = (DWORD)(ii.xHotspot * 2);
hIcon_h = (DWORD)(ii.yHotspot * 2);
::DeleteObject(ii.hbmMask);
::DeleteObject(ii.hbmColor);
Invalidate();
return true;
}
void CRoundButton::DrawTheIcon(CDC *pDC, BOOL bHasTitle, RECT *rpItem, CRect *rpTitle, BOOL bIsPressed, BOOL bIsDisabled)
{/*
BYTE byIndex = 0;
// Select the icon to use
if ((m_bIsCheckBox && bIsPressed) || (!m_bIsCheckBox && (bIsPressed || m_bMouseOnButton)))
byIndex = 0;
else
byIndex = (m_csIcons[1].hIcon == NULL ? 0 : 1);
CRect rImage;
//PrepareImageRect(bHasTitle, rpItem, rpTitle, bIsPressed, m_csIcons[byIndex].dwWidth, m_csIcons[byIndex].dwHeight, &rImage);
// Ole'!
pDC->DrawState( rImage.TopLeft(),
rImage.Size(),
hIcon,
(bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
(CBrush*)NULL);*/
}
void CRoundButton::SetTextColor(COLORREF color)
{
textColor=color;
Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -