📄 guibutton.cpp
字号:
rcBordes.top+=1;
switch(Estado)
{
case OVER:
pDC->Draw3dRect(rcBordes,RGB(255,202,130),RGB(255,193,111));
rcBordes.DeflateRect(1,1);
pDC->Draw3dRect(rcBordes,RGB(255,202,130),RGB(255,193,111));
break;
case FOCUS:
pDC->Draw3dRect(rcBordes,GuiDrawLayer::GetRGBFondoXP(),GuiDrawLayer::GetRGBFondoXP());
rcBordes.DeflateRect(1,1);
pDC->Draw3dRect(rcBordes,GuiDrawLayer::GetRGBFondoXP(),GuiDrawLayer::GetRGBFondoXP());
break;
}
//****************************************************************
//pintar los vertices
//left,top
pDC->SetPixel(rc.left+1,rc.top,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
pDC->SetPixel(rc.left,rc.top+1,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
pDC->SetPixel(rc.left+1,rc.top+1,Estado != DISABLED?clrPress:clrShadow);
//pintar los vertices
//left,bottom
pDC->SetPixel(rc.left+1,rc.bottom,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
pDC->SetPixel(rc.left,rc.bottom-1,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
pDC->SetPixel(rc.left+1,rc.bottom-1,Estado != DISABLED?clrPress:clrShadow);
//right,top
pDC->SetPixel(rc.right-1,rc.top,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
pDC->SetPixel(rc.right,rc.top+1,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
pDC->SetPixel(rc.right-1,rc.top+1,Estado != DISABLED?clrPress:clrShadow);
//right,bottom
pDC->SetPixel(rc.right-1,rc.bottom,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
pDC->SetPixel(rc.right,rc.bottom-1,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
pDC->SetPixel(rc.right-1,rc.bottom-1,Estado != DISABLED?clrPress:clrShadow);
}
pDC->SelectObject(pOld);
}
//*****************************************************************************
void CGuiButton::SetColor(COLORREF m_clrFaces)
{
m_clrface=m_clrFaces;
}
//*****************************************************************************
void CGuiButton::ShowImage( BOOL m_viewimage)
{
m_viewImage=m_viewimage;
CalLayout();
}
//*****************************************************************************
void CGuiButton::ShowText( BOOL m_viewText)
{
m_ViewText=m_viewText;
CalLayout();
}
//*****************************************************************************
void CGuiButton::CalLayout()
{
CString m_str;
CRect m_rect;
CSize sizeImag=CSize(0,0);
int m_High=0;
if (GetSafeHwnd() == NULL) return;
GetWindowText(m_str);
CClientDC dc(this);
ICONINFO bm;
ZeroMemory(&bm, sizeof(ICONINFO));
GetClientRect(&m_rect);
CSize SizeCad=CSize(0,0);
if (m_viewImage == TRUE)
if (m_hicon[0]!= NULL)
{
::GetIconInfo(m_hicon[0], &bm);
sizeImag=CSize((BYTE)(bm.xHotspot * 2),(BYTE)(bm.yHotspot * 2));
}
if ( m_ViewText == TRUE)
if (m_str.GetLength() > 0)
{
SizeCad=dc.GetTextExtent(m_str);
SizeCad.cx-=7;
}
m_High=max(sizeImag.cy,SizeCad.cy);
m_High=max(m_High,m_rect.Height());
m_szButton= CSize(SizeCad.cx+sizeImag.cx+7,m_High);
if (m_ReposWindow==TRUE)
SetWindowPos (NULL, 0, 0, SizeCad.cx+sizeImag.cx+10, m_High,
SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
}
//*****************************************************************************
CSize CGuiButton::GetSizeButton()
{
m_ReposWindow=FALSE;
CalLayout();
m_ReposWindow=TRUE;
return m_szButton;
}
//*****************************************************************************
void CGuiButton::SetImage(HICON hIcon)
{
m_hicon[0] =hIcon;
m_hicon[1] =hIcon;
m_hicon[2] =hIcon;
ShowImage( TRUE);
}
BOOL CGuiButton::SetImage(UINT uBitmapNormal,UINT uBitmapFocus,UINT uBitmapPush)
{
BOOL result;
HINSTANCE hirc = AfxFindResourceHandle(MAKEINTRESOURCE(uBitmapNormal), RT_BITMAP);
if (uBitmapNormal != -1)
{
m_hicon[0] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapNormal), IMAGE_ICON, 16, 16, LR_LOADMAP3DCOLORS);
result=TRUE;
}
else
result=FALSE;
if (uBitmapFocus != -1)
{
m_hicon[1] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapFocus), IMAGE_ICON, 16,16, LR_LOADMAP3DCOLORS);
if (m_hicon[1] == NULL && m_hicon[0]!= NULL)
m_hicon[1] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapNormal), IMAGE_ICON, 16,16, LR_LOADMAP3DCOLORS);
}
else
{
if (m_hicon[0]!= NULL)
m_hicon[1] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapNormal), IMAGE_ICON, 16,16, LR_LOADMAP3DCOLORS);
}
if (uBitmapPush != -1)
{
m_hicon[2] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapFocus), IMAGE_ICON, 16,16, LR_LOADMAP3DCOLORS);
if (m_hicon[2] == NULL && m_hicon[0]!= NULL)
m_hicon[2] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapNormal), IMAGE_ICON, 16,16, LR_LOADMAP3DCOLORS);
}
else
{
if (m_hicon[0]!= NULL)
m_hicon[2] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapNormal), IMAGE_ICON, 16,16, LR_LOADMAP3DCOLORS|LR_LOADTRANSPARENT);
}
CalLayout();
return result;
}
//*****************************************************************************
void CGuiButton::SetToolTip(LPCTSTR lpMsg)
{
CRect rc;
m_lpMsg=(LPCSTR)lpMsg;
if (GetSafeHwnd ()== NULL) return;
if (m_lpMsg != NULL)
{
if (m_ToolTipCtrl.GetSafeHwnd () == NULL)
{
m_ToolTipCtrl.Create(this,TTS_ALWAYSTIP);
m_ToolTipCtrl.Activate(TRUE);
m_ToolTipCtrl.AddTool(this,lpMsg);
}
else
{
m_ToolTipCtrl.UpdateTipText(lpMsg,this);
}
}
else
{
if (m_ToolTipCtrl.GetSafeHwnd () == NULL)
{
m_ToolTipCtrl.Create(this,TTS_ALWAYSTIP);
m_ToolTipCtrl.AddTool(this,lpMsg);
}
else
m_ToolTipCtrl.UpdateTipText(lpMsg,this);
m_ToolTipCtrl.Activate(FALSE);
}
}
//*****************************************************************************
void CGuiButton::SetCaption(LPCSTR lpMsg)
{
SetWindowText((LPCTSTR)lpMsg);
m_ViewText=TRUE;
CalLayout();
Invalidate();
}
//*****************************************************************************
void CGuiButton::SetTypeButton(TypeButton m_TypeButton)
{
m_iTypeButton=m_TypeButton;
if (m_iTypeButton == TYPE_ARROW)
{
m_iStyleArrow=ARROW_RIGHT;
m_iAling=ALING_LEFT;
m_iTypeButton =TYPE_ARROW;
m_iStyle=STYLE_3D;
m_lpMsg="";
m_iSelected=0;
m_viewImage=TRUE;
m_ViewText=TRUE;
SetAlingArrow(ARROW_RIGHT);
}
}
//*****************************************************************************
void CGuiButton::SetAlingArrow(StyleArrow m_AlingArron)
{
if (m_iTypeButton == TYPE_ARROW)
{
m_iStyleArrow=m_AlingArron;
if (m_iStyleArrow==ARROW_RIGHT)
{
if (m_hicon[0]== NULL)
m_hicon[0]=GuiDrawLayer::LoadIconLib(IDI_ARROWRIGHT);
else
{
m_hicon[0]=GuiDrawLayer::LoadIconLib(IDI_ARROWRIGHT);
m_hicon[1]=GuiDrawLayer::LoadIconLib(IDI_ARROWRIGHT);
m_hicon[2]=NULL;
}
}
else
{
if (m_hicon[0] == NULL)
m_hicon[0]=GuiDrawLayer::LoadIconLib(IDI_ARROWDOWN);
else
{
m_hicon[0]=GuiDrawLayer::LoadIconLib(IDI_ARROWDOWN);
m_hicon[1]=GuiDrawLayer::LoadIconLib(IDI_ARROWDOWN);
m_hicon[2]=NULL;
}
}
}
}
//*****************************************************************************
void CGuiButton::SetStyleBorder(Style m_Style)
{
m_iStyle=m_Style;
}
//*****************************************************************************
void CGuiButton::SetAlingMsg(AlingMsg m_AlingMsg)
{
if (m_iTypeButton == TYPE_NORMAL)
m_iAling=m_AlingMsg;
}
//*****************************************************************************
int CGuiButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CButton::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
//*****************************************************************************
BOOL CGuiButton::OnEraseBkgnd(CDC* pDC)
{
pDC;
return TRUE;
}
//*****************************************************************************
void CGuiButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect m_rect;
GetClientRect(&m_rect);
if (m_rect.PtInRect(point))
{
if (m_iSelected == 1) return;
m_iSelected =1;
Invalidate();
UpdateWindow();
SetTimer(1,10,NULL);
}
CButton::OnMouseMove(nFlags, point);
}
//*****************************************************************************
void CGuiButton::PreSubclassWindow()
{
ModifyStyle(0,BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
//*****************************************************************************
void CGuiButton::OnLButtonUp(UINT nFlags, CPoint point)
{
CButton::OnLButtonUp(nFlags, point);
if (m_iSelected == 1) m_iSelected = 0;
Invalidate ();
UpdateWindow ();
}
//*****************************************************************************
void CGuiButton::OnLButtonDown(UINT nFlags, CPoint point)
{
CButton::OnLButtonDown(nFlags, point);
Invalidate();
UpdateWindow();
if (m_iTypeButton == TYPE_ARROW)
{
SetFocus ();
ShowMenu();
}
}
void CGuiButton::SetPopupMenu(CMenu* pMenu)
{
m_pMenu=pMenu;
}
//*****************************************************************************
void CGuiButton::ShowMenu()
{
CRect rcW;
int x,y;
GetWindowRect(&rcW);
if (m_iStyleArrow==ARROW_RIGHT)
{
x=rcW.right+1;
y=rcW.top;
}
else
{
x=rcW.left;
y=rcW.bottom;
}
if (m_pMenu->GetSafeHmenu() != NULL && m_iMenuActivo==FALSE)
{
m_iMenuActivo=TRUE;
int m_iResMenu=m_pMenu->GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
x, y, GetParent(), NULL);
if (m_iResMenu!=0)
{
CWnd* pParent= GetParent();
pParent->SendMessage ( WM_COMMAND,MAKEWPARAM (GetDlgCtrlID (), BN_CLICKED),(LPARAM) m_hWnd);
}
m_iMenuActivo=FALSE;
}
else
return;
ReleaseCapture();
}
//*****************************************************************************
BOOL CGuiButton::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message==WM_MOUSEMOVE)
if (m_ToolTipCtrl.GetSafeHwnd())
m_ToolTipCtrl.RelayEvent(pMsg);
return CButton::PreTranslateMessage(pMsg);
}
//*****************************************************************************
void CGuiButton::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if (nIDEvent == 1)
{
CRect m_rect;
CPoint pt(GetMessagePos());
ScreenToClient(&pt);
GetClientRect(m_rect);
if (!m_rect.PtInRect(pt))
{
m_iSelected=0;
Invalidate();
UpdateWindow();
KillTimer(1);
}
}
CButton::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -