ColReadOnlyEdit.cpp
上传用户:lcxiong207
上传日期:2010-02-24
资源大小:31k
文件大小:3k
源码类别:

按钮控件

开发平台:

Visual C++

  1. // ColReadOnlyEdit.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ReadOnlyEditDemo.h"
  5. #include "ColReadOnlyEdit.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CColReadOnlyEdit
  13. CColReadOnlyEdit::CColReadOnlyEdit()
  14. {
  15. bReadOnly = TRUE;
  16. m_TextColor = RGB(255,0,0);
  17. m_BackColor = RGB(255,128,64);
  18. }
  19. CColReadOnlyEdit::~CColReadOnlyEdit()
  20. {
  21. }
  22. BEGIN_MESSAGE_MAP(CColReadOnlyEdit, CEdit)
  23. //{{AFX_MSG_MAP(CColReadOnlyEdit)
  24. ON_WM_PAINT()
  25. ON_WM_RBUTTONDOWN()
  26. //}}AFX_MSG_MAP
  27. END_MESSAGE_MAP()
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CColReadOnlyEdit message handlers
  30. BOOL CColReadOnlyEdit::PreTranslateMessage(MSG* pMsg) 
  31. {
  32. // TODO: Add your specialized code here and/or call the base class
  33. if(pMsg->message == WM_KEYDOWN)
  34. {
  35. if(bReadOnly)
  36. return TRUE;
  37. }
  38. return CEdit::PreTranslateMessage(pMsg);
  39. }
  40. void CColReadOnlyEdit::OnPaint() 
  41. {
  42. CPaintDC dc(this); // device context for painting
  43. // TODO: Add your message handler code here
  44. GetWindowText(m_Text);
  45. // Delete the old brush
  46. m_Brush.DeleteObject();
  47. m_Brush.CreateSolidBrush(m_BackColor);
  48. CDC* pDC = GetDC();
  49. pDC->SetBkMode(OPAQUE);
  50. pDC->SetBkColor(m_BackColor);
  51. pDC->SelectObject(&m_Brush);
  52. CRect rc;
  53. GetClientRect(&rc);
  54. ScreenToClient(&rc);
  55. pDC->Rectangle(0, 0, rc.Width(), rc.Height());
  56. pDC->SetTextColor(m_TextColor);
  57. pDC->TextOut(2, 2, m_Text.GetBuffer(m_Text.GetLength()));
  58. // Do not call CEdit::OnPaint() for painting messages
  59. }
  60. void CColReadOnlyEdit::SetEditBkColor(COLORREF bkColor)
  61. {
  62. this->m_BackColor = bkColor;
  63. Invalidate();
  64. }
  65. void CColReadOnlyEdit::SetEditTextColor(COLORREF textColor)
  66. {
  67. this->m_TextColor = textColor;
  68. Invalidate();
  69. }
  70. COLORREF CColReadOnlyEdit::GetEditTextColor()
  71. {
  72. return this->m_TextColor;
  73. }
  74. COLORREF CColReadOnlyEdit::GetEditBkColor()
  75. {
  76. return this->m_BackColor;
  77. }
  78. BOOL CColReadOnlyEdit::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult) 
  79. {
  80. // TODO: Add your specialized code here and/or call the base class
  81. if (message != WM_CTLCOLOREDIT) 
  82. {
  83. return CEdit::OnChildNotify(message, wParam, lParam, pLResult);
  84. }
  85.  
  86. HDC hdcChild = (HDC)wParam;
  87. SetTextColor(hdcChild, m_TextColor);
  88. SetBkColor(hdcChild, m_BackColor);
  89. Invalidate();
  90. return TRUE;
  91. }
  92. void CColReadOnlyEdit::SetEditReadOnly(BOOL bReadOnly)
  93. {
  94. this->bReadOnly = bReadOnly;
  95. }
  96. BOOL CColReadOnlyEdit::GetEditReadOnly()
  97. {
  98. return this->bReadOnly;
  99. }
  100. void CColReadOnlyEdit::OnRButtonDown(UINT nFlags, CPoint point) 
  101. {
  102. // TODO: Add your message handler code here and/or call default
  103. if(bReadOnly)
  104. {
  105. ::AfxMessageBox("设置成为只读模式,右键菜单屏蔽!");
  106. return;
  107. }
  108. else
  109. CEdit::OnRButtonDown(nFlags, point);
  110. }