📄 buttonst.cpp
字号:
pDC->MoveTo(itemRect.left,itemRect.bottom-1);
pDC->LineTo(itemRect.left,itemRect.top);
pDC->LineTo(itemRect.right,itemRect.top);
//深灰色线,画按钮右下部
pDC->SelectObject(&penBtnShadow);
pDC->MoveTo(itemRect.left,itemRect.bottom-1);
pDC->LineTo(itemRect.right-1,itemRect.bottom-1);
pDC->LineTo(itemRect.right-1,itemRect.top-1);
pDC->SelectObject(pOldPen);
}
}
else
{
//白线画按钮的左上部
pOldPen=pDC->SelectObject(&penBtnHiLight);
pDC->MoveTo(itemRect.left,itemRect.bottom-1);
pDC->LineTo(itemRect.left,itemRect.top);
pDC->LineTo(itemRect.right,itemRect.top);
// 浅灰色线,画按钮的右下部
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);
pDC->SelectObject(pOldPen);
//黑线,画阴影左上部
pDC->SelectObject(&pen3DDKShadow);
pOldPen=pDC->SelectObject(&penBtnHiLight);
pDC->MoveTo(itemRect.left,itemRect.bottom-1);
pDC->LineTo(itemRect.right-1,itemRect.bottom-1);
pDC->LineTo(itemRect.right-1,itemRect.top-1);
//深灰色线,画阴影右下部
pDC->SelectObject(&penBtnShadow);
pDC->SelectObject(pen3DLight);
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);
}
}
CString sTitle;
GetWindowText(sTitle);
if(m_bShowText==FALSE) sTitle.Empty();
CRect captionRect=lpDrawItemStruct->rcItem;
/* if(m_hIconIn!=NULL)
{
DrawTheIcon(pDC,&sTitle,&lpDrawItemStruct->rcItem,&captionRect,bIsPressed,bIsDisabled );
}*/
CRect iconRect,textRect,oldRect;
iconRect=captionRect;
textRect=captionRect;
oldRect=captionRect;
if(m_nAlign==1/*ST_ALIGN_VERT*/)//垂直分布,图标在上
{
iconRect.left+=(oldRect.Width()-m_cxIcon)/2;
iconRect.top=oldRect.top;
iconRect.bottom=iconRect.top+m_cyIcon;
iconRect.right=oldRect.right;
textRect.left+=(oldRect.Width()-m_cxIcon)/2;
textRect.top=oldRect.top+m_cyIcon;
textRect.right=oldRect.right;
textRect.bottom=oldRect.bottom;
}
else if(m_nAlign==2/*ST_ALIGN_HORIZ*/)//水平分布,图标在左
{
iconRect.top+=3;
// iconRect.right=oldRect.left;
iconRect.bottom=oldRect.bottom;
iconRect.left=oldRect.left;
iconRect.right=iconRect.left+m_cxIcon;
int len1=sTitle.GetLength();
textRect.left+=m_cxIcon+(oldRect.Width()-iconRect.Width()-len1)/4;
textRect.top+=oldRect.Height()/4;
textRect.right=oldRect.right;
textRect.bottom=oldRect.bottom;
}
else if(m_nAlign==3)//垂直分布,图标在下
{
// iconRect.left=oldRect.left+25;
int len1=sTitle.GetLength();
iconRect.left+=(oldRect.Width()-m_cxIcon)/2;
iconRect.top=oldRect.top+2;
iconRect.right=oldRect.right;
iconRect.bottom=iconRect.top+m_cyIcon;
textRect.left+=(oldRect.Width()-len1)/4;
textRect.top=iconRect.bottom+1;
textRect.bottom=oldRect.bottom;
textRect.right-=(oldRect.Width()-len1)/4;
}
else if(m_nAlign==4)//水平分布,图标在右
{
int len1=sTitle.GetLength();
textRect.bottom=oldRect.bottom;
textRect.left=oldRect.left;
//textRect.right=textRect.left+len1;
textRect.top=oldRect.Height()/4;
iconRect.left=oldRect.right-m_cxIcon;
iconRect.top=oldRect.top;
iconRect.right=oldRect.right;
iconRect.bottom=oldRect.bottom;
}
else
textRect=oldRect;
///captionRect=textRect;
if(sTitle.IsEmpty()==FALSE)
{
if(bIsPressed)
{
captionRect=textRect;
captionRect.OffsetRect(1,1);
}
if((m_MouseOnButton==TRUE)||(bIsPressed) )
{
pDC->SetTextColor(GetActiveFgColor());
pDC->SetBkColor(GetActiveBgColor());
if(m_hIconIn!=NULL)
{
::DrawIconEx(HDC(*pDC),iconRect.left,iconRect.top,m_hIconIn,m_cxIcon,m_cyIcon,0,0,DI_NORMAL);
pDC->DrawState(iconRect.TopLeft(),iconRect.Size(), m_hIconIn,
(bIsDisabled ? DSS_DISABLED: DSS_NORMAL),(CBrush *)NULL);
}
}
else
{
pDC->SetTextColor(GetInactiveFgColor());
pDC->SetBkColor(GetInactiveBgColor());
if(m_hIconOut!=NULL)
{
::DrawIconEx(HDC(*pDC),iconRect.left,iconRect.top,m_hIconOut,m_cxIcon,m_cyIcon,0,0,DI_NORMAL);
pDC->DrawState(iconRect.TopLeft(),iconRect.Size(),m_hIconOut,
(bIsDisabled ? DSS_DISABLED: DSS_NORMAL),(CBrush *)NULL);
}
}
CRect ceterRect=textRect;
if(m_hIconIn==NULL)
{
textRect=oldRect;
textRect.top+=(int)oldRect.Height()/3;
int len=sTitle.GetLength();
textRect.left+=(int)(oldRect.Width()-len)/6;
}
pDC->DrawText(sTitle,-1,textRect,DT_SINGLELINE|DT_CALCRECT);
captionRect.OffsetRect((ceterRect.Width()-textRect.Width())/2,(ceterRect.Height()-textRect.Height())/2);
pDC->SetBkMode(TRANSPARENT);
pDC->DrawState(textRect.TopLeft(),textRect.Size(),(LPCTSTR)sTitle,
(bIsDisabled ? DSS_DISABLED:DSS_NORMAL),TRUE,0,(CBrush *)NULL);
}
if(m_bIsFlat==FALSE||(m_bIsFlat==TRUE && m_bDrawFlatFocus==TRUE))
{
if(bIsFocused)
{
CRect focusRect=itemRect;
focusRect.DeflateRect(3,3);
pDC->DrawFocusRect(&focusRect);
}
}
}
void CButtonST::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
UINT nBS;
nBS=GetButtonStyle();
//检查按钮是否却省
if(nBS & BS_DEFPUSHBUTTON) m_bIsDefault=TRUE;
SetButtonStyle(nBS|BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
void CButtonST::InitToolTip()
{
if(m_ToolTip.m_hWnd==NULL)
{
m_ToolTip.Create(this,0);
m_ToolTip.Activate(FALSE);
}
}
void CButtonST::SetTooltipText(CString *spText,BOOL bActivate)
{
if(spText==NULL)
return;
InitToolTip();
if(m_ToolTip.GetToolCount()==0)
{
CRect rectBtn;
GetClientRect(rectBtn);
m_ToolTip.AddTool(this,(LPCTSTR)*spText,rectBtn,1);
}
m_ToolTip.UpdateTipText((LPCTSTR)*spText,this,1);
m_ToolTip.Activate(bActivate);
}
void CButtonST::SetFlatFocus(BOOL bDrawFlatFocus,BOOL bRepaint)
{
m_bDrawFlatFocus=bDrawFlatFocus;
if(bRepaint==TRUE) Invalidate();
}
BOOL CButtonST::GetFlatFocus()
{
return m_bDrawFlatFocus;
}
BOOL CButtonST::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
InitToolTip();
m_ToolTip.RelayEvent(pMsg);
return CButton::PreTranslateMessage(pMsg);
}
LRESULT CButtonST::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if(message==WM_LBUTTONDBLCLK)
{
message=WM_LBUTTONDOWN;
}
return CButton::DefWindowProc(message, wParam, lParam);
}
void CButtonST::OnCaptureChanged(CWnd *pWnd)
{
// TODO: Add your message handler code here
if(m_MouseOnButton==TRUE)
{
ReleaseCapture();
Invalidate();
}
CButton::OnCaptureChanged(pWnd);
}
void CButtonST::OnKillFocus(CWnd* pNewWnd)
{
CButton::OnKillFocus(pNewWnd);
// TODO: Add your message handler code here
if(m_bIsFlat==FALSE) return;
if(m_MouseOnButton==TRUE)
{
m_MouseOnButton=FALSE;
Invalidate();
}
}
void CButtonST::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CWnd *pWnd;
CWnd *pParent;
CButton::OnMouseMove(nFlags, point);
if(nFlags & MK_LBUTTON && m_MouseOnButton==FALSE)
return ;
if(m_bIsFlat==FALSE)
return;
pWnd=GetActiveWindow();
pParent=GetOwner();
if((GetCapture()!=this) && /*#ifndef ST_LIKEIE pWnd!=NULL && #endif*/ pParent!=NULL && pWnd!=NULL)
{
m_MouseOnButton=TRUE;
SetCapture();
Invalidate();
}
else
{
POINT p2=point;
ClientToScreen(&p2);
CWnd* wndUnderMouse=WindowFromPoint(p2);
if(wndUnderMouse!=this)
{
if(m_MouseOnButton==TRUE)
{
m_MouseOnButton=FALSE;
Invalidate();
}
if(!(nFlags & MK_LBUTTON))
ReleaseCapture();
}
}
}
BOOL CButtonST::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
SetDefaultInactiveBgColor(TRUE);
SetDefaultInactiveFgColor(TRUE);
SetDefaultActiveBgColor(TRUE);
SetDefaultActiveFgColor(TRUE);
return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -