QQ登录

只需一步,快速开始

上位机MFC平面按钮实例

[ 复制链接 ]
效果演示

上位机MFC平面按钮实例

上位机MFC平面按钮实例

当前按钮集成类是在CButton基础之上派生了一个叫做CButtonST的类实现的,
其特征如下:
标准CButton属性。
文本和图标按钮。
仅有文本或仅有图标按钮。
16色和256色图标支持。
多种平面风格。
在运行时间从酷风格改变成标准风格。
具有两个图标:一个用于鼠标光标在按钮上,一个用于鼠标光标不在光标上。
每种颜色可以定制。
可以通过DDX_调用。
包含所有代码及可编译后的可执行程序。

请点击此处下载

请先注册会员后在进行下载

已注册会员,请先登录后下载

文件名称:CBtn.rar 
文件大小:69.51 KB  售价:10金币
下载权限: 不限 以上或 VIP会员   [购买捐助会员]   [充值积分]   有问题联系我



实现过程
1.新建立一基于对话框工程,按照上图添加按钮控件,并进行排版。
2.将工程根目录两文件BTNST.CPP,BTNST.H,复制到自己工程目录下,
并加载到自己项目中,文件代码在帖子底部,也可以复制新建两文件。
这样工程中就多了两个类CButtonST,CMemDC,如图:

上位机MFC平面按钮实例

上位机MFC平面按钮实例


3.在主对话框中包含上面的头文件,并添加类实例对象,及对象初始化
#include "BtnST.h"
private:
        CButtonST m_btnAbout16;
        CButtonST m_btnExit16;
        CButtonST m_btnScrew16;
        CButtonST m_btnRight16;
        CButtonST m_btnLeft16;
        CButtonST m_btnQuestion16;
        CButtonST m_btnText16;
        CButtonST m_btnView16;
        CButtonST m_btnExplore16;
        CButtonST m_btnCancelBor16;
        CButtonST m_btnOkBor16;
        CButtonST m_btnCDGold256;
        CButtonST m_btnCancel256;
        CButtonST m_btnOk256;
        CButtonST m_btnLamp256;
        CButtonST m_btnHand256;
        CButtonST m_btnOk16;
        CButtonST m_btnCancel16;
        CButtonST m_btnZip256;


m_btnHand256.SubclassDlgItem(IDC_HAND256, this);
  m_btnHand256.SetIcon(IDI_HAND256);

  // Create the Lamp 256 colors button
  m_btnLamp256.SubclassDlgItem(IDC_LAMP256, this);
  m_btnLamp256.SetIcon(IDI_LAMP256, IDI_LAMP2);
  // Don't draw border for this button
  m_btnLamp256.DrawBorder(FALSE);

  // Create the CD-Gold 256 colors button
  m_btnCDGold256.SubclassDlgItem(IDC_CDGOLD256, this);
  m_btnCDGold256.SetIcon(IDI_CDGOLD256);
  // Don't draw border for this button
  m_btnCDGold256.DrawBorder(FALSE);

  // Create the Ok 256 colors button
  m_btnOk256.SubclassDlgItem(IDC_OK256, this);
  m_btnOk256.SetIcon(IDI_OK256);

  // Create the Cancel 256 colors button
  m_btnCancel256.SubclassDlgItem(IDC_CANCEL256, this);
  m_btnCancel256.SetIcon(IDI_CANCEL256);

  // Create the Ok 16 colors button
  m_btnOk16.SubclassDlgItem(IDC_OK16, this);
  m_btnOk16.SetIcon(IDI_OK16);

  // Create the Cancel 16 colors button
  m_btnCancel16.SubclassDlgItem(IDC_CANCEL16, this);
  m_btnCancel16.SetIcon(IDI_CANCEL16);

  // Create the OkBor 16 colors button
  m_btnOkBor16.SubclassDlgItem(IDC_OKBOR16, this);
  m_btnOkBor16.SetIcon(IDI_OKBOR16, IDI_OKBOR2);
  m_btnOkBor16.SetActiveBgColor(RGB(220,220,220));

  // Create the CancelBor 16 colors button
  m_btnCancelBor16.SubclassDlgItem(IDC_CANCELBOR16, this);
  m_btnCancelBor16.SetIcon(IDI_CANCELBOR16, IDI_CANCELBOR2);
  m_btnCancelBor16.SetActiveBgColor(RGB(220,220,220));

  // Create the Explore 16 colors button
  m_btnExplore16.SubclassDlgItem(IDC_EXPLORE16, this);
  m_btnExplore16.SetIcon(IDI_EXPLORE16);

  // Create the View 16 colors button
  m_btnView16.SubclassDlgItem(IDC_VIEW16, this);
  m_btnView16.SetIcon(IDI_VIEW16);

  // Create the Text 16 colors button
  m_btnText16.SubclassDlgItem(IDC_TEXT16, this);
  m_btnText16.SetIcon(IDI_TEXT16);

  // Create the Question 16 colors button
  m_btnQuestion16.SubclassDlgItem(IDC_QUESTION16, this);
  m_btnQuestion16.SetIcon(IDI_QUESTION16);

  // Create the Left 16 colors button
  m_btnLeft16.SubclassDlgItem(IDC_LEFT16, this);
  m_btnLeft16.SetIcon(IDI_LEFT16);

  // Create the Right 16 colors button
  m_btnRight16.SubclassDlgItem(IDC_RIGHT16, this);
  m_btnRight16.SetIcon(IDI_RIGHT16);

  // Create the Screw 16 colors button
  m_btnScrew16.SubclassDlgItem(IDC_SCREW16, this);
  m_btnScrew16.SetIcon(IDI_SCREW16);
  // Set some color effect
  m_btnScrew16.SetActiveFgColor(RGB(255,0,0));
  m_btnScrew16.SetInactiveFgColor(RGB(0,255,0));

  // Create the Exit 16 colors button
  m_btnExit16.SubclassDlgItem(IDOK, this);
  m_btnExit16.SetIcon(IDI_EXIT16);
  // Draw this button as a standard button
  m_btnExit16.SetFlat(FALSE);
  // Assign a custom cursor
  m_btnExit16.SetBtnCursor(IDC_HAND);

  // Create the About 16 colors button
  COLORREF crStandard = m_btnExit16.GetInactiveBgColor();
  m_btnAbout16.SubclassDlgItem(IDC_ABOUT16, this);
  m_btnAbout16.SetIcon(IDI_ABOUT16);
  // Align icon vertically
  m_btnAbout16.SetAlign(CButtonST::ST_ALIGN_VERT);
  // Set some color effect
  m_btnAbout16.SetInactiveBgColor(crStandard - RGB(20,20,20));
  m_btnAbout16.SetActiveBgColor(crStandard + RGB(20,20,20));


3.可以看到初始按钮对象时,使用了相当多的图标 资源,这里一并使用例程根目录的图标,
也可自己制作,资源ID要一致:
IDI_ABOUT16
IDI_CANCEL16IDI_CANCEL256
IDI_CANCEL256
IDI_CANCELBOR2
IDI_CDGOLD256
IDI_EXIT16
IDI_EXPLORE16

4.编译运行就可以查看效果。下面是用到的类文件源代码
  1. #ifndef _BTNST_H
  2. #define _BTNST_H

  3. #if _MSC_VER >= 1000
  4. #pragma once
  5. #endif // _MSC_VER >= 1000

  6. // CBtnST.h : header file
  7. //

  8. // Comment this if you don't want that CButtonST hilights itself
  9. // also when the window is inactive (like happens in Internet Explorer)
  10. #define ST_LIKEIE

  11. // Comment this if you don't want to use CMemDC class
  12. #define ST_USE_MEMDC

  13. /////////////////////////////////////////////////////////////////////////////
  14. // CButtonST window

  15. class CButtonST : public CButton
  16. {
  17. // Construction
  18. public:
  19.     CButtonST();
  20.         ~CButtonST();
  21.     enum {ST_ALIGN_HORIZ, ST_ALIGN_VERT};

  22. // Attributes
  23. public:

  24. // Operations
  25. public:

  26. // Overrides
  27.         // ClassWizard generated virtual function overrides
  28.     //{{AFX_VIRTUAL(CButtonST)
  29.         public:
  30.         virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
  31.         protected:
  32.         virtual void PreSubclassWindow();
  33.         //}}AFX_VIRTUAL

  34. // Implementation
  35. public:
  36.         BOOL SetBtnCursor(int nCursorId = -1);

  37.         void SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint = FALSE);
  38.         BOOL GetFlatFocus();

  39.         void SetDefaultActiveFgColor(BOOL bRepaint = FALSE);
  40.         void SetActiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE);
  41.         const COLORREF GetActiveFgColor();
  42.        
  43.         void SetDefaultActiveBgColor(BOOL bRepaint = FALSE);
  44.         void SetActiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE);
  45.         const COLORREF GetActiveBgColor();
  46.        
  47.         void SetDefaultInactiveFgColor(BOOL bRepaint = FALSE);
  48.         void SetInactiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE);
  49.         const COLORREF GetInactiveFgColor();

  50.         void SetDefaultInactiveBgColor(BOOL bRepaint = FALSE);
  51.         void SetInactiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE);
  52.         const COLORREF GetInactiveBgColor();

  53.         void SetShowText(BOOL bShow = TRUE);
  54.         BOOL GetShowText();

  55.         void SetAlign(int nAlign);
  56.         int GetAlign();

  57.         void SetFlat(BOOL bState = TRUE);
  58.         BOOL GetFlat();

  59.         void DrawBorder(BOOL bEnable = TRUE);
  60.         void SetIcon(int nIconInId, int nIconOutId = NULL, BYTE cx = 32, BYTE cy = 32);

  61.         static const short GetVersionI();
  62.         static const char* GetVersionC();

  63. protected:
  64.     //{{AFX_MSG(CButtonST)
  65.         afx_msg void OnCaptureChanged(CWnd *pWnd);
  66.         afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
  67.         afx_msg void OnKillFocus(CWnd* pNewWnd);
  68.         afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  69.         //}}AFX_MSG

  70.         DECLARE_MESSAGE_MAP()
  71. private:
  72.         void DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled);

  73.         int m_nAlign;
  74.         BOOL m_bShowText;
  75.         BOOL m_bDrawBorder;
  76.         BOOL m_bIsFlat;
  77.         BOOL m_MouseOnButton;
  78.         BOOL m_bDrawFlatFocus;

  79.         HCURSOR m_hCursor;

  80.         HICON m_hIconIn;
  81.         HICON m_hIconOut;
  82.         BYTE m_cyIcon;
  83.         BYTE m_cxIcon;

  84.         COLORREF  m_crInactiveBg;
  85.     COLORREF  m_crInactiveFg;
  86.     COLORREF  m_crActiveBg;
  87.     COLORREF  m_crActiveFg;
  88. };


  89. #ifdef ST_USE_MEMDC



  90. class CMemDC : public CDC
  91. {
  92. public:

  93.     // constructor sets up the memory DC
  94.     CMemDC(CDC* pDC) : CDC()
  95.     {
  96.         ASSERT(pDC != NULL);

  97.         m_pDC = pDC;
  98.         m_pOldBitmap = NULL;
  99.         m_bMemDC = !pDC->IsPrinting();
  100.               
  101.         if (m_bMemDC)    // Create a Memory DC
  102.         {
  103.             pDC->GetClipBox(&m_rect);
  104.             CreateCompatibleDC(pDC);
  105.             m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
  106.             m_pOldBitmap = SelectObject(&m_bitmap);
  107.             SetWindowOrg(m_rect.left, m_rect.top);
  108.         }
  109.         else        // Make a copy of the relevent parts of the current DC for printing
  110.         {
  111.             m_bPrinting = pDC->m_bPrinting;
  112.             m_hDC       = pDC->m_hDC;
  113.             m_hAttribDC = pDC->m_hAttribDC;
  114.         }
  115.     }
  116.    
  117.     // Destructor copies the contents of the mem DC to the original DC
  118.     ~CMemDC()
  119.     {
  120.         if (m_bMemDC)
  121.         {   
  122.             // Copy the offscreen bitmap onto the screen.
  123.             m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
  124.                           this, m_rect.left, m_rect.top, SRCCOPY);

  125.             //Swap back the original bitmap.
  126.             SelectObject(m_pOldBitmap);
  127.         } else {
  128.             // All we need to do is replace the DC with an illegal value,
  129.             // this keeps us from accidently deleting the handles associated with
  130.             // the CDC that was passed to the constructor.
  131.             m_hDC = m_hAttribDC = NULL;
  132.         }
  133.     }

  134.     // Allow usage as a pointer
  135.     CMemDC* operator->() {return this;}
  136.         
  137.     // Allow usage as a pointer
  138.     operator CMemDC*() {return this;}

  139. private:
  140.     CBitmap  m_bitmap;      // Offscreen bitmap
  141.     CBitmap* m_pOldBitmap;  // bitmap originally found in CMemDC
  142.     CDC*     m_pDC;         // Saves CDC passed in constructor
  143.     CRect    m_rect;        // Rectangle of drawing area.
  144.     BOOL     m_bMemDC;      // TRUE if CDC really is a Memory DC.
  145. };

  146. #endif

  147. /////////////////////////////////////////////////////////////////////////////

  148. //{{AFX_INSERT_LOCATION}}
  149. // Microsoft Developer Studio will insert additional declarations immediately before the previous line.

  150. #endif
复制代码

  1. // BtnST.cpp : implementation file
  2. //

  3. #include "stdafx.h"
  4. #include "BtnST.h"

  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif

  10. /////////////////////////////////////////////////////////////////////////////
  11. // CButtonST

  12. CButtonST::CButtonST()
  13. {
  14.   m_MouseOnButton = FALSE;

  15.   m_hIconIn = NULL;
  16.   m_hIconOut = NULL;
  17.   m_cxIcon = 0;
  18.   m_cyIcon = 0;
  19.   m_hCursor = NULL;
  20.   
  21.   // Default type is "flat" button
  22.   m_bIsFlat = TRUE;
  23.   
  24.   // By default draw border in "flat" button
  25.   m_bDrawBorder = TRUE;
  26.   
  27.   // By default icon is aligned horizontally
  28.   m_nAlign = ST_ALIGN_HORIZ;
  29.   
  30.   // By default show the text button
  31.   m_bShowText = TRUE;
  32.   
  33.   // By default, for "flat" button, don't draw the focus rect
  34.   m_bDrawFlatFocus = FALSE;
  35.        
  36.   SetDefaultInactiveBgColor();
  37.   SetDefaultInactiveFgColor();
  38.   SetDefaultActiveBgColor();
  39.   SetDefaultActiveFgColor();
  40. } // End of CButtonST


  41. CButtonST::~CButtonST()
  42. {
  43.         // Destroy the icons (if any)
  44.         if (m_hIconIn != NULL) ::DeleteObject(m_hIconIn);
  45.         if (m_hIconOut != NULL) ::DeleteObject(m_hIconOut);
  46.         // Destroy the cursor (if any)
  47.         if (m_hCursor != NULL) ::DestroyCursor(m_hCursor);
  48. } // End of ~CButtonST


  49. BEGIN_MESSAGE_MAP(CButtonST, CButton)
  50.     //{{AFX_MSG_MAP(CButtonST)
  51.         ON_WM_CAPTURECHANGED()
  52.         ON_WM_SETCURSOR()
  53.         ON_WM_KILLFOCUS()
  54.         ON_WM_MOUSEMOVE()
  55.         //}}AFX_MSG_MAP
  56. END_MESSAGE_MAP()


  57. void CButtonST::SetIcon(int nIconInId, int nIconOutId, BYTE cx, BYTE cy)
  58. {
  59.         HINSTANCE hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconInId),
  60.                                                                                                         RT_GROUP_ICON);
  61.         // Set icon when the mouse is IN the button
  62.         m_hIconIn = (HICON)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nIconInId), IMAGE_ICON, 0, 0, 0);
  63.   
  64.         // Set icon when the mouse is OUT the button
  65.         m_hIconOut = (nIconOutId == NULL) ? m_hIconIn : (HICON)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nIconOutId), IMAGE_ICON, 0, 0, 0);
  66.   
  67.         m_cxIcon = cx;
  68.         m_cyIcon = cy;

  69.         RedrawWindow();
  70. } // End of SetIcon


  71. BOOL CButtonST::SetBtnCursor(int nCursorId)
  72. {
  73.         HINSTANCE hInstResource;
  74.         // Destroy any previous cursor
  75.         if (m_hCursor != NULL) ::DestroyCursor(m_hCursor);
  76.         m_hCursor = NULL;

  77.         // If we want a cursor
  78.         if (nCursorId != -1)
  79.         {
  80.                 hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nCursorId),
  81.                                                                                         RT_GROUP_CURSOR);
  82.                 // Load icon resource
  83.                 m_hCursor = (HCURSOR)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nCursorId), IMAGE_CURSOR, 0, 0, 0);
  84.                 // If something wrong then return FALSE
  85.                 if (m_hCursor == NULL) return FALSE;
  86.         }

  87.         return TRUE;
  88. } // End of SetBtnCursor


  89. void CButtonST::SetFlat(BOOL bState)
  90. {
  91.   m_bIsFlat = bState;
  92.   Invalidate();
  93. } // End of SetFlat


  94. BOOL CButtonST::GetFlat()
  95. {
  96.   return m_bIsFlat;
  97. } // End of GetFlat


  98. void CButtonST::SetAlign(int nAlign)
  99. {
  100.   switch (nAlign)
  101.   {   
  102.     case ST_ALIGN_HORIZ:
  103.          m_nAlign = ST_ALIGN_HORIZ;
  104.          break;
  105.     case ST_ALIGN_VERT:
  106.          m_nAlign = ST_ALIGN_VERT;
  107.          break;
  108.   }
  109.   Invalidate();
  110. } // End of SetAlign


  111. int CButtonST::GetAlign()
  112. {
  113.   return m_nAlign;
  114. } // End of GetAlign


  115. void CButtonST::DrawBorder(BOOL bEnable)
  116. {
  117.   m_bDrawBorder = bEnable;
  118. } // End of DrawBorder


  119. const char* CButtonST::GetVersionC()
  120. {
  121.   return "2.3";
  122. } // End of GetVersionC


  123. const short CButtonST::GetVersionI()
  124. {
  125.   return 23; // Divide by 10 to get actual version
  126. } // End of GetVersionI


  127. void CButtonST::SetShowText(BOOL bShow)
  128. {
  129.   m_bShowText = bShow;
  130.   Invalidate();
  131. } // End of SetShowText


  132. BOOL CButtonST::GetShowText()
  133. {
  134.   return m_bShowText;
  135. } // End of GetShowText


  136. void CButtonST::OnMouseMove(UINT nFlags, CPoint point)
  137. {
  138.   CWnd* pWnd;  // Finestra attiva
  139.   CWnd* pParent; // Finestra che contiene il bottone

  140.   CButton::OnMouseMove(nFlags, point);

  141.   // If the mouse enter the button with the left button pressed
  142.   // then do nothing
  143.   if (nFlags & MK_LBUTTON && m_MouseOnButton == FALSE) return;

  144.   // If our button is not flat then do nothing
  145.   if (m_bIsFlat == FALSE) return;

  146.   pWnd = GetActiveWindow();
  147.   pParent = GetOwner();

  148.         if ((GetCapture() != this) &&
  149.                 (
  150. #ifndef ST_LIKEIE
  151.                 pWnd != NULL &&
  152. #endif
  153.                 pParent != NULL))
  154.         {
  155.                 m_MouseOnButton = TRUE;
  156.                 //SetFocus();        // Thanks Ralph!
  157.                 SetCapture();
  158.                 Invalidate();
  159.         }
  160.         else
  161.   {
  162.     CRect rc;
  163.     GetClientRect(&rc);
  164.     if (!rc.PtInRect(point))
  165.     {
  166.       // Redraw only if mouse goes out
  167.       if (m_MouseOnButton == TRUE)
  168.       {
  169.         m_MouseOnButton = FALSE;
  170.         Invalidate();
  171.       }
  172.       // If user is NOT pressing left button then release capture!
  173.       if (!(nFlags & MK_LBUTTON)) ReleaseCapture();
  174.     }
  175.   }
  176. } // End of OnMouseMove


  177. void CButtonST::OnKillFocus(CWnd * pNewWnd)
  178. {
  179.   CButton::OnKillFocus(pNewWnd);

  180.   // If our button is not flat then do nothing
  181.   if (m_bIsFlat == FALSE) return;

  182.   if (m_MouseOnButton == TRUE)
  183.   {
  184.     m_MouseOnButton = FALSE;
  185.     Invalidate();
  186.   }
  187. } // End of OnKillFocus


  188. void CButtonST::OnCaptureChanged(CWnd *pWnd)
  189. {
  190.         if (m_MouseOnButton == TRUE)
  191.         {
  192.                 ReleaseCapture();
  193.                 Invalidate();
  194.         }

  195.         // Call base message handler
  196.         CButton::OnCaptureChanged(pWnd);
  197. } // End of OnCaptureChanged


  198. void CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  199. {
  200. #ifdef ST_USE_MEMDC
  201.   CDC  *pdrawDC = CDC::FromHandle(lpDIS->hDC);
  202.   CMemDC memDC(pdrawDC);
  203.   CDC  *pDC = &memDC;
  204. #else       
  205.   CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  206. #endif

  207.   CPen *pOldPen;
  208.   BOOL bIsPressed  = (lpDIS->itemState & ODS_SELECTED);
  209.   BOOL bIsFocused  = (lpDIS->itemState & ODS_FOCUS);
  210.   BOOL bIsDisabled = (lpDIS->itemState & ODS_DISABLED);

  211.   CRect itemRect = lpDIS->rcItem;

  212.   if (m_bIsFlat == FALSE)
  213.   {
  214.     if (bIsFocused)
  215.     {
  216.       CBrush br(RGB(0,0,0));  
  217.       pDC->FrameRect(&itemRect, &br);
  218.       itemRect.DeflateRect(1, 1);
  219.     }
  220.   }

  221.   // Prepare draw... paint button's area with background color
  222.   COLORREF bgColor;
  223.   if ((m_MouseOnButton == TRUE) || (bIsPressed))
  224.     bgColor = GetActiveBgColor();
  225.   else
  226.     bgColor = GetInactiveBgColor();

  227.   CBrush br(bgColor);
  228.   pDC->FillRect(&itemRect, &br);

  229.         // Disegno lo sfondo del bottone
  230. //CBrush br(GetSysColor(COLOR_BTNFACE));  
  231. //pDC->FillRect(&itemRect, &br);

  232.   // Draw pressed button
  233.   if (bIsPressed)
  234.   {
  235.     if (m_bIsFlat == TRUE)
  236.     {
  237.       if (m_bDrawBorder == TRUE)
  238.       {
  239.             CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // Bianco
  240.         CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));   // Grigio scuro

  241.         // Disegno i bordi a sinistra e in alto
  242.         // Dark gray line
  243.         pOldPen = pDC->SelectObject(&penBtnShadow);
  244.         pDC->MoveTo(itemRect.left, itemRect.bottom-1);
  245.         pDC->LineTo(itemRect.left, itemRect.top);
  246.         pDC->LineTo(itemRect.right, itemRect.top);
  247.         // Disegno i bordi a destra e in basso
  248.         // White line
  249.         pDC->SelectObject(penBtnHiLight);
  250.         pDC->MoveTo(itemRect.left, itemRect.bottom-1);
  251.         pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
  252.         pDC->LineTo(itemRect.right-1, itemRect.top-1);
  253.         //
  254.         pDC->SelectObject(pOldPen);
  255.       }
  256.     }
  257.     else   
  258.     {
  259.       CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
  260.       pDC->FrameRect(&itemRect, &brBtnShadow);
  261.     }
  262.   }
  263.   else // ...else draw non pressed button
  264.   {
  265.     CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
  266.     CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));       // Light gray
  267.     CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));   // Dark gray
  268.     CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black

  269.     if (m_bIsFlat == TRUE)
  270.     {
  271.       if (m_MouseOnButton == TRUE && m_bDrawBorder == TRUE)
  272.       {
  273.               // Disegno i bordi a sinistra e in alto
  274.         // White line
  275.         pOldPen = pDC->SelectObject(&penBtnHiLight);
  276.         pDC->MoveTo(itemRect.left, itemRect.bottom-1);
  277.         pDC->LineTo(itemRect.left, itemRect.top);
  278.         pDC->LineTo(itemRect.right, itemRect.top);
  279.         // Disegno i bordi a destra e in basso
  280.         // Dark gray line
  281.         pDC->SelectObject(penBtnShadow);
  282.         pDC->MoveTo(itemRect.left, itemRect.bottom-1);
  283.         pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
  284.         pDC->LineTo(itemRect.right-1, itemRect.top-1);
  285.         //
  286.         pDC->SelectObject(pOldPen);
  287.       }
  288.     }
  289.     else
  290.     {
  291.       // Disegno i bordi a sinistra e in alto
  292.       // White line
  293.       pOldPen = pDC->SelectObject(&penBtnHiLight);
  294.       pDC->MoveTo(itemRect.left, itemRect.bottom-1);
  295.       pDC->LineTo(itemRect.left, itemRect.top);
  296.       pDC->LineTo(itemRect.right, itemRect.top);
  297.       // Light gray line
  298.       pDC->SelectObject(pen3DLight);
  299.       pDC->MoveTo(itemRect.left+1, itemRect.bottom-1);
  300.       pDC->LineTo(itemRect.left+1, itemRect.top+1);
  301.       pDC->LineTo(itemRect.right, itemRect.top+1);
  302.       // Disegno i bordi a destra e in basso
  303.       // Black line
  304.       pDC->SelectObject(pen3DDKShadow);
  305.       pDC->MoveTo(itemRect.left, itemRect.bottom-1);
  306.       pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
  307.       pDC->LineTo(itemRect.right-1, itemRect.top-1);
  308.       // Dark gray line
  309.       pDC->SelectObject(penBtnShadow);
  310.       pDC->MoveTo(itemRect.left+1, itemRect.bottom-2);
  311.       pDC->LineTo(itemRect.right-2, itemRect.bottom-2);
  312.       pDC->LineTo(itemRect.right-2, itemRect.top);
  313.       //
  314.       pDC->SelectObject(pOldPen);
  315.     }
  316.   }

  317.   // Read the button title
  318.   CString sTitle;
  319.   GetWindowText(sTitle);

  320.   // If we don't want the title displayed
  321.   if (m_bShowText == FALSE) sTitle.Empty();

  322.   CRect captionRect = lpDIS->rcItem;

  323.   // Draw the icon
  324.   if (m_hIconIn != NULL)
  325.   {
  326.     DrawTheIcon(pDC, &sTitle, &lpDIS->rcItem, &captionRect, bIsPressed, bIsDisabled);
  327.   }

  328.   // Write the button title (if any)
  329.   if (sTitle.IsEmpty() == FALSE)
  330.   {
  331.     // Disegno la caption del bottone
  332.     // Se il bottone e' premuto muovo la captionRect di conseguenza
  333.     if (bIsPressed)
  334.       captionRect.OffsetRect(1, 1);
  335.    
  336.     // ONLY FOR DEBUG
  337.     // Evidenzia il rettangolo in cui verra' centrata la caption
  338.     //CBrush brBtnShadow(RGB(255, 0, 0));
  339.     //pDC->FrameRect(&captionRect, &brBtnShadow);

  340. #ifdef ST_USE_MEMDC
  341.         // Get dialog's font
  342.     CFont *pCurrentFont = GetFont();
  343.     CFont *pOldFont = pDC->SelectObject(pCurrentFont);
  344. #endif
  345.     if ((m_MouseOnButton == TRUE) || (bIsPressed))
  346.         {
  347.       pDC->SetTextColor(GetActiveFgColor());
  348.       pDC->SetBkColor(GetActiveBgColor());
  349.     }
  350.         else
  351.         {
  352.       pDC->SetTextColor(GetInactiveFgColor());
  353.       pDC->SetBkColor(GetInactiveBgColor());
  354.     }
  355.     // Center text
  356.     CRect centerRect = captionRect;
  357.     pDC->DrawText(sTitle, -1, captionRect, DT_SINGLELINE|DT_CALCRECT);
  358.     captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())/2);
  359.         /* RFU
  360.     captionRect.OffsetRect(0, (centerRect.Height() - captionRect.Height())/2);
  361.     captionRect.OffsetRect((centerRect.Width() - captionRect.Width())-4, (centerRect.Height() - captionRect.Height())/2);
  362.         */

  363.     pDC->DrawState(captionRect.TopLeft(), captionRect.Size(), (LPCTSTR)sTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
  364.                    TRUE, 0, (CBrush*)NULL);
  365. #ifdef ST_USE_MEMDC
  366.     pDC->SelectObject(pOldFont);
  367. #endif
  368.   }

  369.   if (m_bIsFlat == FALSE || (m_bIsFlat == TRUE && m_bDrawFlatFocus == TRUE))
  370.   {
  371.     // Draw the focus rect
  372.     if (bIsFocused)
  373.     {
  374.       CRect focusRect = itemRect;
  375.       focusRect.DeflateRect(3, 3);
  376.       pDC->DrawFocusRect(&focusRect);
  377.     }
  378.   }
  379. } // End of DrawItem


  380. void CButtonST::DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled)
  381. {
  382.   CRect iconRect = rcItem;

  383.   switch (m_nAlign)
  384.   {
  385.     case ST_ALIGN_HORIZ:
  386.          if (title->IsEmpty())
  387.          {
  388.            // Center the icon horizontally
  389.            iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
  390.          }
  391.          else
  392.          {
  393.            // L'icona deve vedersi subito dentro il focus rect
  394.            iconRect.left += 3;  
  395.            captionRect->left += m_cxIcon + 3;
  396.          }
  397.          // Center the icon vertically
  398.          iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
  399.          break;
  400.     case ST_ALIGN_VERT:
  401.          // Center the icon horizontally
  402.          iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
  403.          if (title->IsEmpty())
  404.          {
  405.            // Center the icon vertically
  406.            iconRect.top += ((iconRect.Height() - m_cyIcon)/2);           
  407.          }
  408.          else
  409.          {
  410.            captionRect->top += m_cyIcon;
  411.          }
  412.          break;
  413.   }
  414.    
  415.   // If button is pressed then press the icon also
  416.   if (IsPressed) iconRect.OffsetRect(1, 1);
  417.   // Ole'!
  418.   pDC->DrawState(iconRect.TopLeft(),
  419.                        iconRect.Size(),
  420.                                          (m_MouseOnButton == TRUE || IsPressed) ? m_hIconIn : m_hIconOut,
  421.                                          (IsDisabled ? DSS_DISABLED : DSS_NORMAL),
  422.                  (CBrush*)NULL);
  423. } // End of DrawTheIcon


  424. void CButtonST::PreSubclassWindow()
  425. {
  426.   // Add BS_OWNERDRAW style
  427.   SetButtonStyle(GetButtonStyle() | BS_OWNERDRAW);
  428.   CButton::PreSubclassWindow();
  429. } // End of PreSubclassWindow


  430. void CButtonST::SetDefaultInactiveBgColor(BOOL bRepaint)
  431. {
  432.         m_crInactiveBg = ::GetSysColor(COLOR_BTNFACE);
  433.         if (bRepaint == TRUE) Invalidate();
  434. } // End of SetDefaultInactiveBgColor


  435. void CButtonST::SetInactiveBgColor(COLORREF crNew, BOOL bRepaint)
  436. {
  437.         m_crInactiveBg = crNew;
  438.         if (bRepaint == TRUE) Invalidate();
  439. } // End of SetInactiveBgColor


  440. const COLORREF CButtonST::GetInactiveBgColor()
  441. {
  442.         return m_crInactiveBg;
  443. } // End of GetInactiveBgColor


  444. void CButtonST::SetDefaultInactiveFgColor(BOOL bRepaint)
  445. {
  446.         m_crInactiveFg = ::GetSysColor(COLOR_BTNTEXT);
  447.         if (bRepaint == TRUE) Invalidate();
  448. } // End of SetDefaultInactiveFgColor


  449. void CButtonST::SetInactiveFgColor(COLORREF crNew, BOOL bRepaint)
  450. {
  451.         m_crInactiveFg = crNew;
  452.         if (bRepaint == TRUE) Invalidate();
  453. } // End of SetInactiveFgColor


  454. const COLORREF CButtonST::GetInactiveFgColor()
  455. {
  456.         return m_crInactiveFg;
  457. } // End of GetInactiveFgColor


  458. void CButtonST::SetDefaultActiveBgColor(BOOL bRepaint)
  459. {
  460.         m_crActiveBg = ::GetSysColor(COLOR_BTNFACE);
  461.         if (bRepaint == TRUE) Invalidate();
  462. } // End of SetDefaultActiveBgColor


  463. void CButtonST::SetActiveBgColor(COLORREF crNew, BOOL bRepaint)
  464. {
  465.         m_crActiveBg = crNew;
  466.         if (bRepaint == TRUE) Invalidate();
  467. } // End of SetActiveBgColor


  468. const COLORREF CButtonST::GetActiveBgColor()
  469. {
  470.         return m_crActiveBg;
  471. } // End of GetActiveBgColor


  472. void CButtonST::SetDefaultActiveFgColor(BOOL bRepaint)
  473. {
  474.         m_crActiveFg = ::GetSysColor(COLOR_BTNTEXT);
  475.         if (bRepaint == TRUE) Invalidate();
  476. } // End of SetDefaultActiveFgColor


  477. void CButtonST::SetActiveFgColor(COLORREF crNew, BOOL bRepaint)
  478. {
  479.         m_crActiveFg = crNew;
  480.         if (bRepaint == TRUE) Invalidate();
  481. } // End of SetActiveFgColor


  482. const COLORREF CButtonST::GetActiveFgColor()
  483. {
  484.         return m_crActiveFg;
  485. } // End of GetActiveFgColor


  486. void CButtonST::SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint)
  487. {
  488.         m_bDrawFlatFocus = bDrawFlatFocus;
  489.        
  490.         // Repaint the button
  491.         if (bRepaint == TRUE) Invalidate();
  492. } // End of SetFlatFocus


  493. BOOL CButtonST::GetFlatFocus()
  494. {
  495.         return m_bDrawFlatFocus;
  496. } // End of GetFlatFocus


  497. BOOL CButtonST::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  498. {
  499.         // If a cursor was specified then use it!
  500.         if (m_hCursor != NULL)
  501.         {
  502.                 ::SetCursor(m_hCursor);
  503.                 return TRUE;
  504.         }

  505.         return CButton::OnSetCursor(pWnd, nHitTest, message);
  506. } // End of OnSetCursor

  507. #undef ST_USE_MEMDC
  508. #undef ST_LIKE
复制代码


  

如果您认可,可联系功能定制!

  

如果您着急,充值会员可直接联系发您资料!

  

QQ联系我

微信扫扫联系我

  

回复

使用道具 举报

快速回复 返回列表 客服中心 搜索