2024年4月30日发(作者:)

这是一个实现自绘CButton的类,期望对大家有点帮助,我提供这个类,只是为大家提供一个自绘控件的思路,你可以在此基础上,随意地修改代码,直

到它能完成你所需要的功能.其它控件的自绘,原理了和这个差不多.

1.#if !defined(AFX_BUTTONST_H__1271FF9C_E28C_4D3B_B429_AFE65924A5D0__INCLUDED_) 2.#define

AFX_BUTTONST_H__1271FF9C_E28C_4D3B_B429_AFE65924A5D0__INCLUDED_ 3.4.#if _MSC_VER > 1000 5.#pragma once 6.#endif //

_MSC_VER > 1000 7.// ButtonST.h : header file 8.// 9.10.///////////////////////////////////////////////////////////////////////////// 11.// CButtonST window

CButtonST : public CButton

14.{

15.// Construction :

17. CButtonST();

18.19.// Attributes :

21.

22.// Operations :

24. //设置背景颜色 25. void SetBkColor(COLORREF BkColor);

26. //设置鼠标在按钮上的偏移颜色 27. void SetShOffset(int nShOffset);

28. //设置字体颜色 29. void SetTextColor(COLORREF TextColor);

30. //设置网页链接 31. void SetURL(CString strURL);

32. //设置背景图片 33. void SetBkPicture(CBitmap *pBitMap);

34.35.// Overrides 36. // ClassWizard generated virtual function overrides 37. //{{AFX_VIRTUAL(CButtonST) 38. public:

39. virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

40. virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT

nID, CCreateContext* pContext = NULL);

41. protected:

42. virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

43. virtual void PreSubclassWindow();

44. //}}AFX_VIRTUAL 45.46.// Implementation :

48. virtual ~CButtonST();

49.

50. void DrawItem1(LPDRAWITEMSTRUCT lpDrawItemStruct);

51.52. // Generated message map functions ted:

54. //{{AFX_MSG(CButtonST) 55. afx_msg void OnMouseMove(UINT nFlags, CPoint point);

56. afx_msg BOOL OnNcActivate(BOOL bActive);

57. afx_msg void OnNcMouseMove(UINT nHitTest, CPoint point);

58. afx_msg void OnKillFocus(CWnd* pNewWnd);

59. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

60. //}}AFX_MSG ted:

62. //鼠标是否在按钮上面 63. BOOL m_IsPressed;

64. COLORREF m_BkColor;

65. int m_nShOffset;

66. COLORREF m_TextColor;

67. CString m_strURL;

68. CBitmap *m_pBitMapBK;

ted:

70. //背景颜色偏移 71. COLORREF OffsetColor(COLORREF color,int nOffset);

72. //鼠标离开 73. LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);

74. DECLARE_MESSAGE_MAP()

75.};

76.77.///////////////////////////////////////////////////////////////////////////// 78.79.//{{AFX_INSERT_LOCATION}} 80.// Microsoft Visual C++ will insert additional

declarations immediately before the previous line. 81.82.#endif

// !defined(AFX_BUTTONST_H__1271FF9C_E28C_4D3B_B429_AFE65924A5D0__INCLUDED_) 83.

1.// : implementation file 2.// 3.4.#include "stdafx.h" 5.#include "ContrlST.h" 6.#include "ButtonST.h" 7.8.#ifdef _DEBUG 9.#define new

DEBUG_NEW 10.#undef THIS_FILE char THIS_FILE[] = __FILE__; 12.#endif 13.#define BS_TYPEMASK SS_TYPEMASK

14.15.///////////////////////////////////////////////////////////////////////////// 16.// CButtonST nST::CButtonST()

19.{

20. m_IsPressed = FALSE;

21. m_BkColor = RGB(216,233,216);

22. m_nShOffset = 30;

23. m_TextColor = RGB(0,0,0);

24. m_strURL = "";

25. m_pBitMapBK = NULL;

26.}

nST::~CButtonST()

29.{

30. if(m_pBitMapBK!=NULL)

31. {

32. delete m_pBitMapBK;

33. m_pBitMapBK = NULL;

34. }

35.}

_MESSAGE_MAP(CButtonST, CButton)

39. //{{AFX_MSG_MAP(CButtonST) 40. ON_WM_MOUSEMOVE()

41. ON_WM_NCACTIVATE()

42. ON_WM_NCMOUSEMOVE()

43. ON_WM_KILLFOCUS()

44. ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)

45. ON_WM_LBUTTONDOWN()

46. //}}AFX_MSG_MAP _MESSAGE_MAP()

48.49.///////////////////////////////////////////////////////////////////////////// 50.// CButtonST message handlers 51.52.53.//设置背景颜色

CButtonST::SetBkColor(COLORREF BkColor)

55.{

56. m_BkColor = BkColor;

57. this->Invalidate();

58.}

59.//设置鼠标在按钮上的偏移颜色

CButtonST::SetShOffset(int nShOffset)

61.{

62. m_nShOffset = nShOffset;

63. this->Invalidate();

64.}

65.//设置字体颜色 CButtonST::SetTextColor(COLORREF TextColor)

67.{

68. m_TextColor = TextColor;

69. this->Invalidate();

70.}

71.//设置网页链接 CButtonST::SetURL(CString strURL)

73.{

74. m_strURL = strURL;

75.}

76.77.//设置背景图片 CButtonST::SetBkPicture(CBitmap *pBitMap)

79.{

80. if(m_pBitMapBK==pBitMap)

81. return;

82. if(m_pBitMapBK!=NULL)

83. {

84. delete m_pBitMapBK;

85. m_pBitMapBK = pBitMap;

86. }

87. m_pBitMapBK = pBitMap;

88. this->Invalidate();

89.}

90.//背景颜色偏移 EF CButtonST::OffsetColor(COLORREF color,int nOffset)

92.{

93. BYTE byRed = 0;

94. BYTE byGreen = 0;

95. BYTE byBlue = 0;

96. short shOffsetR = nOffset;

97. short shOffsetG = nOffset;

98. short shOffsetB = nOffset;

99. if (nOffset < -255 || nOffset > 255)

100. {

101. nOffset = 30;

102. }

103.

104. // Get RGB components of specified color 105. byRed = GetRValue(color);

106. byGreen = GetGValue(color);

107. byBlue = GetBValue(color);

108.

109. // Calculate max. allowed real offset 110. if (nOffset > 0)

111. {

112. if (byRed + nOffset > 255) shOffsetR = 255 - byRed;

113. if (byGreen + nOffset > 255) shOffsetG = 255 - byGreen;

114. if (byBlue + nOffset > 255) shOffsetB = 255 - byBlue;