窗口重绘与背景擦除的问题
我想创建一个窗口,它的背景是透明的,当这个窗口被鼠标按住标题栏拖动时,窗口的背景始终是透明的,在窗口被拖动的过程中,背景能不断自动更新,从而能看到窗口后面被挡住的内容。
我知道窗口被移动会产生WM_MOVE消息,我还知道如果要擦除窗口的背景,应该重载响应WM_ERASEBKGND消息的处理程序,并让其返回值为非零以允许擦除窗口背景,但是具体怎么做,望大婶指点迷津。
下面是创建一个透明窗口的代码,我尝试过在OnMove函数中显示调用Invalidate,但是不管用。空出的OnPaint、OnMove以及
OnEraseBkgnd函数留给大婶,代码直接复制过去就可以运行了。
//in the Test_TicTac.h
class CMyApp:public CWinApp
{
virtual BOOL InitInstance();
};
class CMainWindow:public CWnd
{
public:
CMainWindow();
protected:
afx_msg void OnPaint( );
afx_msg void OnMove( int, int );
afx_msg BOOL OnEraseBkgnd( CDC* pDC );
virtual void PostNcDestroy();
DECLARE_MESSAGE_MAP()
};
//in the Test_TicTac.cpp
#include <afxwin.h>
#include "Test_TicTac.h"
CMyApp myApp;
BOOL CMyApp::InitInstance()
{
m_pMainWnd=new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return true;
}
BEGIN_MESSAGE_MAP(CMainWindow,CWnd)
ON_WM_PAINT()
ON_WM_MOVE()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
CMainWindow::CMainWindow()
{
CString strWndClass=AfxRegisterWndClass(CS_DBLCLKS,
AfxGetApp()->LoadStandardCursor(IDC_ARROW),
0,//使用默认值0以创建一个透明的窗口背景。
AfxGetApp()->LoadStandardIcon(IDI_WINLOGO));
CreateEx(NULL,strWndClass,_T("TicTac"),WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX,
CRect(0,0,352,352),this,NULL);
}
void CMainWindow::OnPaint()
{
CPaintDC dc(this);
}
void CMainWindow::OnMove(int x,int y)
{
}
BOOL CMainWindow::OnEraseBkgnd( CDC* pDC )
{
return TRUE;
}
void CMainWindow::PostNcDestroy()
{
delete this;
}