ColReadOnlyEdit.cpp
上传用户:lcxiong207
上传日期:2010-02-24
资源大小:31k
文件大小:3k
- // ColReadOnlyEdit.cpp : implementation file
- //
- #include "stdafx.h"
- #include "ReadOnlyEditDemo.h"
- #include "ColReadOnlyEdit.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CColReadOnlyEdit
- CColReadOnlyEdit::CColReadOnlyEdit()
- {
- bReadOnly = TRUE;
- m_TextColor = RGB(255,0,0);
- m_BackColor = RGB(255,128,64);
- }
- CColReadOnlyEdit::~CColReadOnlyEdit()
- {
- }
- BEGIN_MESSAGE_MAP(CColReadOnlyEdit, CEdit)
- //{{AFX_MSG_MAP(CColReadOnlyEdit)
- ON_WM_PAINT()
- ON_WM_RBUTTONDOWN()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CColReadOnlyEdit message handlers
- BOOL CColReadOnlyEdit::PreTranslateMessage(MSG* pMsg)
- {
- // TODO: Add your specialized code here and/or call the base class
- if(pMsg->message == WM_KEYDOWN)
- {
- if(bReadOnly)
- return TRUE;
- }
- return CEdit::PreTranslateMessage(pMsg);
- }
- void CColReadOnlyEdit::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
-
- // TODO: Add your message handler code here
- GetWindowText(m_Text);
- // Delete the old brush
- m_Brush.DeleteObject();
- m_Brush.CreateSolidBrush(m_BackColor);
- CDC* pDC = GetDC();
- pDC->SetBkMode(OPAQUE);
- pDC->SetBkColor(m_BackColor);
- pDC->SelectObject(&m_Brush);
- CRect rc;
- GetClientRect(&rc);
- ScreenToClient(&rc);
- pDC->Rectangle(0, 0, rc.Width(), rc.Height());
- pDC->SetTextColor(m_TextColor);
- pDC->TextOut(2, 2, m_Text.GetBuffer(m_Text.GetLength()));
- // Do not call CEdit::OnPaint() for painting messages
- }
- void CColReadOnlyEdit::SetEditBkColor(COLORREF bkColor)
- {
- this->m_BackColor = bkColor;
- Invalidate();
- }
- void CColReadOnlyEdit::SetEditTextColor(COLORREF textColor)
- {
- this->m_TextColor = textColor;
- Invalidate();
- }
- COLORREF CColReadOnlyEdit::GetEditTextColor()
- {
- return this->m_TextColor;
- }
- COLORREF CColReadOnlyEdit::GetEditBkColor()
- {
- return this->m_BackColor;
- }
- BOOL CColReadOnlyEdit::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult)
- {
- // TODO: Add your specialized code here and/or call the base class
- if (message != WM_CTLCOLOREDIT)
- {
- return CEdit::OnChildNotify(message, wParam, lParam, pLResult);
- }
-
- HDC hdcChild = (HDC)wParam;
- SetTextColor(hdcChild, m_TextColor);
- SetBkColor(hdcChild, m_BackColor);
- Invalidate();
- return TRUE;
- }
- void CColReadOnlyEdit::SetEditReadOnly(BOOL bReadOnly)
- {
- this->bReadOnly = bReadOnly;
- }
- BOOL CColReadOnlyEdit::GetEditReadOnly()
- {
- return this->bReadOnly;
- }
- void CColReadOnlyEdit::OnRButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- if(bReadOnly)
- {
- ::AfxMessageBox("设置成为只读模式,右键菜单屏蔽!");
- return;
- }
- else
- CEdit::OnRButtonDown(nFlags, point);
- }