📄 xbtnst.cpp
字号:
// XBtnST.cpp : implementation file
//
#include "stdafx.h"
#include "XBtnST.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// XButton
XButton::XButton()
{
m_MouseOnButton = FALSE;
m_hIcon = NULL;
m_cxIcon = 0;
m_cyIcon = 0;
m_bIsFlat = TRUE; // Default type is 'flat' button
m_bDrawBorder = TRUE; // By default draw border in 'flat' button
m_nAlign = ST_ALIGN_HORIZ; // By default icon is aligned horizontally
mouse_on_color=RGB(0,255,0);
press_color=RGB(255,0,0);
} // End of XButton
BEGIN_MESSAGE_MAP(XButton, CButton)
//{{AFX_MSG_MAP(XButton)
//}}AFX_MSG_MAP
ON_WM_MOUSEMOVE()
ON_WM_KILLFOCUS()
END_MESSAGE_MAP()
void XButton::SetIcon(int nIconId, BYTE cx, BYTE cy)
{
m_hIcon = (HICON)::LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(nIconId), IMAGE_ICON, cx, cy, 0);
m_cxIcon = cx;
m_cyIcon = cy;
} // End of SetIcon
void XButton::SetFlat(BOOL bState)
{
m_bIsFlat = bState;
Invalidate();
} // End of SetFlat
BOOL XButton::GetFlat()
{
return m_bIsFlat;
} // End of GetFlat
void XButton::SetAlign(int nAlign)
{
switch (nAlign)
{
case ST_ALIGN_HORIZ:
m_nAlign = ST_ALIGN_HORIZ;
break;
case ST_ALIGN_VERT:
m_nAlign = ST_ALIGN_VERT;
break;
}
Invalidate();
} // End of SetAlign
int XButton::GetAlign()
{
return m_nAlign;
} // End of GetAlign
void XButton::DrawBorder(BOOL bEnable)
{
m_bDrawBorder = !m_bDrawBorder;
} // End of DrawBorder
const char* XButton::GetVersionC()
{
return "2.0";
} // End of GetVersionC
const short XButton::GetVersionI()
{
return 20; // Divide by 10 to get actual version
} // End of GetVersionI
void XButton::OnMouseMove(UINT nFlags, CPoint point)
{
CWnd* pWnd; // Finestra attiva
CWnd* pParent; // Finestra che contiene il bottone
// If our button is not flat then do nothing
//if (m_bIsFlat == FALSE) return;
pWnd = GetActiveWindow();
pParent = GetOwner();
if ((m_MouseOnButton == FALSE) && (GetCapture() != this) &&
((pWnd != NULL) ))//&& (pWnd->m_hWnd == pParent->m_hWnd)))
{
m_MouseOnButton = TRUE;
//SetFocus();
if (m_bDrawBorder == TRUE)
{
SetCapture();
Invalidate();
}
}
else
{
CRect rc;
GetClientRect(&rc);
if (!rc.PtInRect(point) || GetCapture() != this)
{
m_MouseOnButton = FALSE;
Invalidate();
ReleaseCapture();
}
}
CButton::OnMouseMove(nFlags, point);
} // End of OnMouseMove
void XButton::OnKillFocus(CWnd * pNewWnd)
{
CButton::OnKillFocus(pNewWnd);
// If our button is not flat then do nothing
if (m_bIsFlat == FALSE) return;
if (m_MouseOnButton == TRUE)
{
m_MouseOnButton = FALSE;
Invalidate();
UpdateWindow();
}
} // End of OnKillFocus
void XButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CPen* pOldPen;
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
unsigned int IsPressed = (lpDIS->itemState & ODS_SELECTED);
unsigned int IsFocused = (lpDIS->itemState & ODS_FOCUS);
unsigned int IsDisabled = (lpDIS->itemState & ODS_DISABLED);
CRect itemRect = lpDIS->rcItem;
if (m_bIsFlat == FALSE)
{
if (IsFocused)
{
CBrush br(RGB(0,0,0));
pDC->FrameRect(&itemRect, &br);
itemRect.DeflateRect(1, 1);
}
}
// Disegno lo sfondo del bottone
CBrush br(GetSysColor(COLOR_BTNFACE));
pDC->FillRect(&itemRect, &br);
// Draw pressed button
if (IsPressed)
{
if (m_bIsFlat == TRUE)
{
if (m_bDrawBorder == TRUE)
{
CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // Bianco
CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Grigio scuro
// Disegno i bordi a sinistra e in alto
// Dark gray line
pOldPen = pDC->SelectObject(&penBtnShadow);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.left, itemRect.top);
pDC->LineTo(itemRect.right, itemRect.top);
// Disegno i bordi a destra e in basso
// White line
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(pOldPen);
CBrush br(press_color);
itemRect.DeflateRect(2,2);
pDC->FillRect(&itemRect, &br);
CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
pDC->FrameRect(&itemRect, &brBtnShadow);
m_MouseOnButton = FALSE;
}
}
else
{
CBrush br(press_color);
pDC->FillRect(&itemRect, &br);
CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
pDC->FrameRect(&itemRect, &brBtnShadow);
m_MouseOnButton = FALSE;
}
}
else // ...else draw not pressed button
{
CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); // Light gray
CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Dark gray
CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black
if (m_bIsFlat == TRUE)
{
if (m_MouseOnButton == TRUE && m_bDrawBorder == TRUE)
{
// Disegno i bordi a sinistra e in alto
// White line
pOldPen = pDC->SelectObject(&penBtnHiLight);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.left, itemRect.top);
pDC->LineTo(itemRect.right, itemRect.top);
// Disegno i bordi a destra e in basso
// Dark gray line
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);
// Disegno i bordi a sinistra e in alto
// White line
pOldPen = pDC->SelectObject(&penBtnHiLight);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.left, itemRect.top);
pDC->LineTo(itemRect.right, itemRect.top);
// Light gray line
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);
// Disegno i bordi a destra e in basso
// Black line
pDC->SelectObject(pen3DDKShadow);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.top-1);
// Dark gray line
pDC->SelectObject(penBtnShadow);
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);
if (m_MouseOnButton == TRUE)
{
itemRect.DeflateRect(2, 2);
CBrush br(mouse_on_color);
pDC->FillRect(&itemRect, &br);
m_MouseOnButton = FALSE;
}
}
}
else
{
// Disegno i bordi a sinistra e in alto
// White line
pOldPen = pDC->SelectObject(&penBtnHiLight);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.left, itemRect.top);
pDC->LineTo(itemRect.right, itemRect.top);
// Light gray line
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);
// Disegno i bordi a destra e in basso
// Black line
pDC->SelectObject(pen3DDKShadow);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.top-1);
// Dark gray line
pDC->SelectObject(penBtnShadow);
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);
if (m_MouseOnButton == TRUE)
{
CBrush br(mouse_on_color);
pDC->FillRect(&itemRect, &br);
m_MouseOnButton = FALSE;
}
}
}
if (m_bIsFlat == FALSE)
{
// Draw the focus rect
if (IsFocused)
{
CRect focusRect = itemRect;
focusRect.DeflateRect(3, 3);
pDC->DrawFocusRect(&focusRect);
}
}
// Read the button title
CString title;
GetWindowText(title);
CRect captionRect = lpDIS->rcItem;
// Draw the icon
if (m_hIcon != NULL)
{
DrawTheIcon(pDC, &title, &lpDIS->rcItem, &captionRect, IsPressed, IsDisabled);
}
// Write the button title (if any)
if (title.IsEmpty() == FALSE)
{
// Disegno la caption del bottone
// pDC->SetBkMode(TRANSPARENT);
// Se il bottone e' premuto muovo la captionRect di conseguenza
if (IsPressed)
captionRect.OffsetRect(1, 1);
// SOLO PER DEBUG
// Evidenzia il rettangolo in cui verra' centrata la caption
//CBrush brBtnShadow(RGB(255, 0, 0));
//pDC->FrameRect(&captionRect, &brBtnShadow);
// Center text
CRect centerRect = captionRect;
pDC->SetBkMode(TRANSPARENT);
pDC->DrawText(title, -1, captionRect, DT_SINGLELINE|DT_CALCRECT);
captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())/2);
pDC->DrawState(captionRect.TopLeft(), captionRect.Size(), (LPCTSTR)title, (IsDisabled ? DSS_DISABLED : DSS_NORMAL),
TRUE, 0, (CBrush*)NULL);
}
} // End of DrawItem
void XButton::DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled)
{
CRect iconRect = rcItem;
switch (m_nAlign)
{
case ST_ALIGN_HORIZ:
if (title->IsEmpty())
{
// Center the icon horizontally
iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
}
else
{
// L'icona deve vedersi subito dentro il focus rect
iconRect.left += 3;
captionRect->left += m_cxIcon + 3;
}
// Center the icon vertically
iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
break;
case ST_ALIGN_VERT:
// Center the icon horizontally
iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
if (title->IsEmpty())
{
// Center the icon vertically
iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
}
else
{
captionRect->top += m_cyIcon;
}
break;
}
// If button is pressed then press the icon also
if (IsPressed) iconRect.OffsetRect(1, 1);
// Ole'!
pDC->DrawState(iconRect.TopLeft(), iconRect.Size(), m_hIcon, (IsDisabled ? DSS_DISABLED : DSS_NORMAL),
(CBrush*)NULL);
} // End of DrawTheIcon
BOOL XButton::SubclassDlgItem(UINT nID, CWnd * pParent)
{
BOOL retValue = CButton::SubclassDlgItem(nID, pParent);
// If something wrong then abort
if (retValue == 0) return 0;
// Add BS_OWNERDRAW style
LONG bs = ::GetWindowLong(m_hWnd, GWL_STYLE);
bs |= BS_OWNERDRAW;
::SetWindowLong(m_hWnd, GWL_STYLE, bs);
return retValue;
} // End of SubclassDlgItem
void XButton::SetMouseOnColor(COLORREF r)
{
mouse_on_color=r;
}
void XButton::SetPressColor(COLORREF r)
{
press_color=r;
}
void XButton::ReSet()
{
m_MouseOnButton=FALSE;
Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -