Blame | Last modification | View Log
// BtnST.cpp : implementation file//#include "stdafx.h"#include "BtnST.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CButtonST//////////////////////////////////////////////////////////////////////// CButtonST::CButtonST()//////////////////////////////////////////////////////////////////////////CButtonST::CButtonST(){m_MouseOnButton = FALSE;m_hIconIn = NULL;m_hIconOut = NULL;m_cxIcon = 0;m_cyIcon = 0;m_hCursor = NULL;// Default type is "flat" buttonm_bIsFlat = TRUE;// By default draw border in "flat" buttonm_bDrawBorder = TRUE;// By default icon is aligned horizontallym_nAlign = ST_ALIGN_HORIZ;// By default show the text buttonm_bShowText = TRUE;// By default, for "flat" button, don't draw the focus rectm_bDrawFlatFocus = FALSE;SetDefaultInactiveBgColor();SetDefaultInactiveFgColor();SetDefaultActiveBgColor();SetDefaultActiveFgColor();} //end of CButtonST//////////////////////////////////////////////////////////////////////// CButtonST::~CButtonST()//////////////////////////////////////////////////////////////////////////CButtonST::~CButtonST(){//destroy the icons (if any)if (m_hIconIn != NULL) ::DestroyIcon( m_hIconIn);if (m_hIconOut != NULL) ::DestroyIcon( m_hIconOut);//destroy the cursor (if any)if (m_hCursor != NULL) ::DestroyCursor( m_hCursor);} //end of ~CButtonSTBEGIN_MESSAGE_MAP(CButtonST, CButton)//{{AFX_MSG_MAP(CButtonST)ON_WM_CAPTURECHANGED()ON_WM_SETCURSOR()ON_WM_KILLFOCUS()ON_WM_MOUSEMOVE()//}}AFX_MSG_MAPEND_MESSAGE_MAP()//////////////////////////////////////////////////////////////////////// void CButtonST::SetIcon( int nIconInId, int nIconOutId, BYTE cx, BYTE cy)//////////////////////////////////////////////////////////////////////////void CButtonST::SetIcon( int nIconInId, int nIconOutId, BYTE cx, BYTE cy){HINSTANCE hInstResource = AfxFindResourceHandle( MAKEINTRESOURCE(nIconInId), RT_GROUP_ICON);//set icon when the mouse is IN the buttonm_hIconIn = (HICON)::LoadImage( hInstResource, MAKEINTRESOURCE( nIconInId), IMAGE_ICON, 0, 0, 0);//set icon when the mouse is OUT the buttonm_hIconOut = (nIconOutId == NULL) ? m_hIconIn : (HICON)::LoadImage( hInstResource, MAKEINTRESOURCE( nIconOutId), IMAGE_ICON, 0, 0, 0);m_cxIcon = cx;m_cyIcon = cy;RedrawWindow();} //end of SetIcon//////////////////////////////////////////////////////////////////////// BOOL CButtonST::SetBtnCursor( int nCursorId)//////////////////////////////////////////////////////////////////////////BOOL CButtonST::SetBtnCursor( int nCursorId){HINSTANCE hInstResource;//destroy any previous cursorif (m_hCursor != NULL) ::DestroyCursor( m_hCursor);m_hCursor = NULL;// If we want a cursorif (nCursorId != -1){hInstResource = AfxFindResourceHandle( MAKEINTRESOURCE(nCursorId), RT_GROUP_CURSOR);//load icon resourcem_hCursor = (HCURSOR)::LoadImage( hInstResource, MAKEINTRESOURCE(nCursorId), IMAGE_CURSOR, 0, 0, 0);// If something wrong then return FALSEif (m_hCursor == NULL) return FALSE;}return TRUE;} //end of SetBtnCursorvoid CButtonST::SetFlat(BOOL bState){m_bIsFlat = bState;Invalidate();} // End of SetFlatBOOL CButtonST::GetFlat(){return m_bIsFlat;} // End of GetFlatvoid CButtonST::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 SetAlignint CButtonST::GetAlign(){return m_nAlign;} // End of GetAlignvoid CButtonST::DrawBorder(BOOL bEnable){m_bDrawBorder = bEnable;} // End of DrawBorderconst char* CButtonST::GetVersionC(){return "2.3";} // End of GetVersionCconst short CButtonST::GetVersionI(){return 23; // Divide by 10 to get actual version} // End of GetVersionIvoid CButtonST::SetShowText(BOOL bShow){m_bShowText = bShow;Invalidate();} // End of SetShowTextBOOL CButtonST::GetShowText(){return m_bShowText;} // End of GetShowTextvoid CButtonST::OnMouseMove(UINT nFlags, CPoint point){CWnd* pWnd; // Finestra attivaCWnd* pParent; // Finestra che contiene il bottoneCButton::OnMouseMove(nFlags, point);// If the mouse enter the button with the left button pressed// then do nothingif (nFlags & MK_LBUTTON && m_MouseOnButton == FALSE) return;// If our button is not flat then do nothingif (m_bIsFlat == FALSE) return;pWnd = GetActiveWindow();pParent = GetOwner();if ((GetCapture() != this) &&(#ifndef ST_LIKEIEpWnd != NULL &&#endifpParent != NULL)){m_MouseOnButton = TRUE;//SetFocus(); // Thanks Ralph!SetCapture();Invalidate();}else{CRect rc;GetClientRect(&rc);if (!rc.PtInRect(point)){// Redraw only if mouse goes outif (m_MouseOnButton == TRUE){m_MouseOnButton = FALSE;Invalidate();}// If user is NOT pressing left button then release capture!if (!(nFlags & MK_LBUTTON)) ReleaseCapture();}}} // End of OnMouseMovevoid CButtonST::OnKillFocus(CWnd * pNewWnd){CButton::OnKillFocus(pNewWnd);// If our button is not flat then do nothingif (m_bIsFlat == FALSE) return;if (m_MouseOnButton == TRUE){m_MouseOnButton = FALSE;Invalidate();}} // End of OnKillFocusvoid CButtonST::OnCaptureChanged(CWnd *pWnd){if (m_MouseOnButton == TRUE){ReleaseCapture();Invalidate();}// Call base message handlerCButton::OnCaptureChanged(pWnd);} // End of OnCaptureChangedvoid CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDIS){#ifdef ST_USE_MEMDCCDC *pdrawDC = CDC::FromHandle(lpDIS->hDC);CMemDC memDC(pdrawDC);CDC *pDC = &memDC;#elseCDC* pDC = CDC::FromHandle(lpDIS->hDC);#endifCPen *pOldPen;BOOL bIsPressed = (lpDIS->itemState & ODS_SELECTED);BOOL bIsFocused = (lpDIS->itemState & ODS_FOCUS);BOOL bIsDisabled = (lpDIS->itemState & ODS_DISABLED);CRect itemRect = lpDIS->rcItem;if (m_bIsFlat == FALSE){if (bIsFocused){CBrush br(RGB(0,0,0));pDC->FrameRect(&itemRect, &br);itemRect.DeflateRect(1, 1);}}// Prepare draw... paint button's area with background colorCOLORREF bgColor;if ((m_MouseOnButton == TRUE) || (bIsPressed))bgColor = GetActiveBgColor();elsebgColor = GetInactiveBgColor();CBrush br(bgColor);pDC->FillRect(&itemRect, &br);// Disegno lo sfondo del bottone//CBrush br(GetSysColor(COLOR_BTNFACE));//pDC->FillRect(&itemRect, &br);// Draw pressed buttonif (bIsPressed){if (m_bIsFlat == TRUE){if (m_bDrawBorder == TRUE){CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // BiancoCPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Grigio scuro// Disegno i bordi a sinistra e in alto// Dark gray linepOldPen = 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 linepDC->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);}}else{CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));pDC->FrameRect(&itemRect, &brBtnShadow);}}else // ...else draw non pressed button{CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // WhiteCPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); // Light grayCPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Dark grayCPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Blackif (m_bIsFlat == TRUE){if (m_MouseOnButton == TRUE && m_bDrawBorder == TRUE){// Disegno i bordi a sinistra e in alto// White linepOldPen = 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 linepDC->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{// Disegno i bordi a sinistra e in alto// White linepOldPen = pDC->SelectObject(&penBtnHiLight);pDC->MoveTo(itemRect.left, itemRect.bottom-1);pDC->LineTo(itemRect.left, itemRect.top);pDC->LineTo(itemRect.right, itemRect.top);// Light gray linepDC->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 linepDC->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 linepDC->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);}}// Read the button titleCString sTitle;GetWindowText(sTitle);// If we don't want the title displayedif (m_bShowText == FALSE) sTitle.Empty();CRect captionRect = lpDIS->rcItem;// Draw the iconif (m_hIconIn != NULL){DrawTheIcon(pDC, &sTitle, &lpDIS->rcItem, &captionRect, bIsPressed, bIsDisabled);}// Write the button title (if any)if (sTitle.IsEmpty() == FALSE){// Disegno la caption del bottone// Se il bottone e' premuto muovo la captionRect di conseguenzaif (bIsPressed)captionRect.OffsetRect(1, 1);// ONLY FOR DEBUG// Evidenzia il rettangolo in cui verra' centrata la caption//CBrush brBtnShadow(RGB(255, 0, 0));//pDC->FrameRect(&captionRect, &brBtnShadow);#ifdef ST_USE_MEMDC// Get dialog's fontCFont *pCurrentFont = GetFont();CFont *pOldFont = pDC->SelectObject(pCurrentFont);#endifif ((m_MouseOnButton == TRUE) || (bIsPressed)){pDC->SetTextColor(GetActiveFgColor());pDC->SetBkColor(GetActiveBgColor());}else{pDC->SetTextColor(GetInactiveFgColor());pDC->SetBkColor(GetInactiveBgColor());}// Center textCRect centerRect = captionRect;pDC->DrawText(sTitle, -1, captionRect, DT_SINGLELINE|DT_CALCRECT);captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())/2);/* RFUcaptionRect.OffsetRect(0, (centerRect.Height() - captionRect.Height())/2);captionRect.OffsetRect((centerRect.Width() - captionRect.Width())-4, (centerRect.Height() - captionRect.Height())/2);*/pDC->DrawState(captionRect.TopLeft(), captionRect.Size(), (LPCTSTR)sTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL),TRUE, 0, (CBrush*)NULL);#ifdef ST_USE_MEMDCpDC->SelectObject(pOldFont);#endif}if (m_bIsFlat == FALSE || (m_bIsFlat == TRUE && m_bDrawFlatFocus == TRUE)){// Draw the focus rectif (bIsFocused){CRect focusRect = itemRect;focusRect.DeflateRect(3, 3);pDC->DrawFocusRect(&focusRect);}}} // End of DrawItemvoid CButtonST::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 horizontallyiconRect.left += ((iconRect.Width() - m_cxIcon)/2);}else{// L'icona deve vedersi subito dentro il focus recticonRect.left += 3;captionRect->left += m_cxIcon + 3;}// Center the icon verticallyiconRect.top += ((iconRect.Height() - m_cyIcon)/2);break;case ST_ALIGN_VERT:// Center the icon horizontallyiconRect.left += ((iconRect.Width() - m_cxIcon)/2);if (title->IsEmpty()){// Center the icon verticallyiconRect.top += ((iconRect.Height() - m_cyIcon)/2);}else{captionRect->top += m_cyIcon;}break;}// If button is pressed then press the icon alsoif (IsPressed) iconRect.OffsetRect(1, 1);// Ole'!pDC->DrawState(iconRect.TopLeft(),iconRect.Size(),(m_MouseOnButton == TRUE || IsPressed) ? m_hIconIn : m_hIconOut,(IsDisabled ? DSS_DISABLED : DSS_NORMAL),(CBrush*)NULL);} // End of DrawTheIconvoid CButtonST::PreSubclassWindow(){// Add BS_OWNERDRAW styleSetButtonStyle(GetButtonStyle() | BS_OWNERDRAW);CButton::PreSubclassWindow();} // End of PreSubclassWindowvoid CButtonST::SetDefaultInactiveBgColor(BOOL bRepaint){m_crInactiveBg = ::GetSysColor(COLOR_BTNFACE);if (bRepaint == TRUE) Invalidate();} // End of SetDefaultInactiveBgColorvoid CButtonST::SetInactiveBgColor(COLORREF crNew, BOOL bRepaint){m_crInactiveBg = crNew;if (bRepaint == TRUE) Invalidate();} // End of SetInactiveBgColorconst COLORREF CButtonST::GetInactiveBgColor(){return m_crInactiveBg;} // End of GetInactiveBgColorvoid CButtonST::SetDefaultInactiveFgColor(BOOL bRepaint){m_crInactiveFg = ::GetSysColor(COLOR_BTNTEXT);if (bRepaint == TRUE) Invalidate();} // End of SetDefaultInactiveFgColorvoid CButtonST::SetInactiveFgColor(COLORREF crNew, BOOL bRepaint){m_crInactiveFg = crNew;if (bRepaint == TRUE) Invalidate();} // End of SetInactiveFgColorconst COLORREF CButtonST::GetInactiveFgColor(){return m_crInactiveFg;} // End of GetInactiveFgColorvoid CButtonST::SetDefaultActiveBgColor(BOOL bRepaint){m_crActiveBg = ::GetSysColor(COLOR_BTNFACE);if (bRepaint == TRUE) Invalidate();} // End of SetDefaultActiveBgColorvoid CButtonST::SetActiveBgColor(COLORREF crNew, BOOL bRepaint){m_crActiveBg = crNew;if (bRepaint == TRUE) Invalidate();} // End of SetActiveBgColorconst COLORREF CButtonST::GetActiveBgColor(){return m_crActiveBg;} // End of GetActiveBgColorvoid CButtonST::SetDefaultActiveFgColor(BOOL bRepaint){m_crActiveFg = ::GetSysColor(COLOR_BTNTEXT);if (bRepaint == TRUE) Invalidate();} // End of SetDefaultActiveFgColorvoid CButtonST::SetActiveFgColor(COLORREF crNew, BOOL bRepaint){m_crActiveFg = crNew;if (bRepaint == TRUE) Invalidate();} // End of SetActiveFgColorconst COLORREF CButtonST::GetActiveFgColor(){return m_crActiveFg;} // End of GetActiveFgColorvoid CButtonST::SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint){m_bDrawFlatFocus = bDrawFlatFocus;// Repaint the buttonif (bRepaint == TRUE) Invalidate();} // End of SetFlatFocusBOOL CButtonST::GetFlatFocus(){return m_bDrawFlatFocus;} // End of GetFlatFocusBOOL CButtonST::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message){// If a cursor was specified then use it!if (m_hCursor != NULL){::SetCursor(m_hCursor);return TRUE;}return CButton::OnSetCursor(pWnd, nHitTest, message);} // End of OnSetCursor#undef ST_USE_MEMDC#undef ST_LIKEIE