WM_ERASEBKGND
The WM_ERASEBKGND message is sent when the window background must be erased (for example, when a window is resized). The message is sent to prepare an invalidated portion of a window for painting.
The WM_ERASEBKGND message is sent when the window background must be erased (for example, when a window is resized). The message is sent to prepare an invalidated portion of a window for painting.
加入这些成员
COLORREF m_TextColor;
COLORREF m_BackColor;
CBrush m_Brush;
void UpdateCtrl(); afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg void OnUpdate();
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnKillfocus();
afx_msg BOOL OnEraseBkgnd (CDC* pDC);
afx_msg void OnSetFocus(CWnd* pOldWnd);
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
初始化
m_TextColor = RGB(0, 0, 0);
m_BackColor = TRANS_BACK;
函数实现
HBRUSH CEditEx::CtlColor(CDC* pDC, UINT nCtlColor)
{
m_Brush.DeleteObject(); if (m_BackColor == TRANS_BACK) {
m_Brush.CreateStockObject(HOLLOW_BRUSH);
pDC->SetBkMode(TRANSPARENT);
}
else {
m_Brush.CreateSolidBrush(m_BackColor);
pDC->SetBkColor(m_BackColor);
} pDC->SetTextColor(m_TextColor); return (HBRUSH)m_Brush;
}
void CEditEx::OnKillfocus()
{
UpdateCtrl();
}
void CEditEx::OnUpdate()
{
UpdateCtrl();
}void CEditEx::UpdateCtrl()
{
CWnd* pParent = GetParent();
CRect rect; GetWindowRect(rect);
pParent->ScreenToClient(rect);
rect.DeflateRect(2, 2); pParent->InvalidateRect(rect, FALSE);
Invalidate();
}BOOL CEditEx::OnEraseBkgnd (CDC* pDC)
{
if(!m_bTra)
return CEdit::OnEraseBkgnd(pDC);//进行重绘背景
return true;
}void CEditEx::OnSetFocus(CWnd* pOldWnd)
{
// Don't allow user to select text
if (m_bSelectable)
CEdit::OnSetFocus (pOldWnd);
else
pOldWnd->SetFocus();
}void CEditEx::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
CEdit::OnVScroll(nSBCode, nPos, pScrollBar);
Invalidate();
}void CEditEx::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
CEdit::OnHScroll(nSBCode, nPos, pScrollBar);
Invalidate();
}void CEditEx::OnLButtonDown(UINT nFlags, CPoint point)
{
UpdateCtrl();
CEdit::OnLButtonDown(nFlags, point);
}
加入这些成员
COLORREF m_TextColor;
COLORREF m_BackColor;
CBrush m_Brush;
void UpdateCtrl(); afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg void OnUpdate();
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnKillfocus();
afx_msg BOOL OnEraseBkgnd (CDC* pDC);
afx_msg void OnSetFocus(CWnd* pOldWnd);
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
初始化
m_TextColor = RGB(0, 0, 0);
m_BackColor = TRANS_BACK;
函数实现
HBRUSH CEditEx::CtlColor(CDC* pDC, UINT nCtlColor)
{
m_Brush.DeleteObject(); if (m_BackColor == TRANS_BACK) {
m_Brush.CreateStockObject(HOLLOW_BRUSH);
pDC->SetBkMode(TRANSPARENT);
}
else {
m_Brush.CreateSolidBrush(m_BackColor);
pDC->SetBkColor(m_BackColor);
} pDC->SetTextColor(m_TextColor); return (HBRUSH)m_Brush;
}
void CEditEx::OnKillfocus()
{
UpdateCtrl();
}
void CEditEx::OnUpdate()
{
UpdateCtrl();
}void CEditEx::UpdateCtrl()
{
CWnd* pParent = GetParent();
CRect rect; GetWindowRect(rect);
pParent->ScreenToClient(rect);
rect.DeflateRect(2, 2); pParent->InvalidateRect(rect, FALSE);
Invalidate();
}BOOL CEditEx::OnEraseBkgnd (CDC* pDC)
{
if(!m_bTra)
return CEdit::OnEraseBkgnd(pDC);//进行重绘背景
return true;
}void CEditEx::OnSetFocus(CWnd* pOldWnd)
{
// Don't allow user to select text
if (m_bSelectable)
CEdit::OnSetFocus (pOldWnd);
else
pOldWnd->SetFocus();
}void CEditEx::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
CEdit::OnVScroll(nSBCode, nPos, pScrollBar);
Invalidate();
}void CEditEx::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
CEdit::OnHScroll(nSBCode, nPos, pScrollBar);
Invalidate();
}void CEditEx::OnLButtonDown(UINT nFlags, CPoint point)
{
UpdateCtrl();
CEdit::OnLButtonDown(nFlags, point);
}
不知如何做。我做的方法肯定有问题,但我不是太理解,现粘贴我的代
码如下,请大侠们帮忙。我只是处理的WM_PAINT消息。代码如下:void CMyEdit::OnPaint()
{
CPaintDC dc(this); // device context for painting
HDC m_hDC;
dc.Attach(m_hDC); // TODO: Add your message handler code here
CBitmap bmp;
//if(! bmp.LoadBitmap((const char *)m_arCredits.GetAt(n).Mid(2)))
if(! bmp.LoadBitmap(IDB_BK) )
{
CString str;
str.Format("%s","装载位图失败");
AfxMessageBox(str);
return;
} BITMAP bmInfo;
bmp.GetBitmap(&bmInfo); CClientDC* pDC;
pDC = new CClientDC(this);
dc.CreateCompatibleDC(pDC);
CBitmap* pOldBmp = dc.SelectObject(&bmp); // draw the bitmap
CRect m_rectScreen;
GetClientRect(&m_rectScreen);
pDC->BitBlt((m_rectScreen.Width() - bmInfo.bmWidth) / 2, 1, bmInfo.bmWidth, bmInfo.bmHeight, &dc, 0, 0, SRCCOPY);
pDC->SetBkMode(TRANSPARENT);
pDC->TextOut(10,20,"asdfasdfsadfsadfsadf");//在此显示我自己的字符串等 dc.SelectObject(pOldBmp);
bmp.DeleteObject();
return;// CEdit::OnEraseBkgnd(pDC);
// Do not call CEdit::OnPaint() for painting messages
}
所以大家不用奇怪我怎么就在里面TextOut了。如果哪位能提供可读写型的更
好啊。
其父窗口类定义一个m_brush成员变量,在构造函数里初始化:
HBITMAP bmp = LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1));
m_brush = CreatePatternBrush(bmp);
DeleteObject(bmp);在父窗口的OnCtlColor中加入:
if(pWnd->m_hWnd == GetDlgItem(IDC_EDIT1)->m_hWnd)
{
pDC->SetBkMode(TRANSPARENT);
return m_brush;
}