MFC SDI(单文档)中创建一个新窗口(半透明)

思路来自:MFC Tutorial - Creating a window with two classes

在工程中新建一个类 CChildFrame

class CChildFrame : public CFrameWnd

在构造函数中:

1 CChildFrame::CChildFrame()
2 {
3     Create(NULL,"MFC Tutorial Part 1 CoderSource Window");
4 }

可选:添加WM_PAINT消息:

 1 void CChildFrame::OnPaint() 
 2 {
 3     CPaintDC dc(this); // device context for painting
 4     
 5     // TODO: Add your message handler code here
 6     CDC *pDC = GetDC();
 7 
 8     CBrush brush(RGB(0, 0, 0));
 9     CBrush *pOldBrush = pDC->SelectObject(&brush);
10     pDC->Rectangle(0, 0, 500, 500);
11     pDC->SelectObject(pOldBrush);
12     // Do not call CFrameWnd::OnPaint() for painting messages
13 }

在CXXApp类中,添加 CChildFrame 指针变量:

1 class CXXApp : public CWinApp
2 {
3 public:
4     CChildFrame *m_pChildWnd;
5 
6         ...
7 }

在CXXApp类的 InitInstance 方法中添加:

 1 BOOL CMultilayerDisplayApp::InitInstance()
 2 {
 3     ...
 4     
 5     m_pChildWnd = new CChildFrame(); // 创建新窗口类对象
 6         //////// 这部分用于透明此窗口对象关联的窗口///////////
 7     //加入WS_EX_LAYERED扩展属性
 8     SetWindowLong(m_pChildWnd->m_hWnd, GWL_EXSTYLE, GetWindowLong(m_pChildWnd->m_hWnd, GWL_EXSTYLE)^0x80000);
 9     HINSTANCE hInst = LoadLibrary("User32.DLL");
10     if (hInst)
11     {
12         typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
13         MYFUNC fun = NULL;
14         //取得SetLayeredWindowAttributes函数指针
15         fun = (MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
16         if (fun)
17         {
18             fun(m_pChildWnd->m_hWnd, 0, 128, 2);
19         }
20         FreeLibrary(hInst);
21     }
22         //////// 这部分用于透明此窗口对象关联的窗口///////////
23 
24     // The one and only window has been initialized, so show and update it.
25     m_pMainWnd->ShowWindow(SW_SHOW);
26     m_pMainWnd->UpdateWindow();
27 
28     m_pChildWnd->ShowWindow(SW_SHOW);
29     m_pChildWnd->UpdateWindow();
30 
31     return TRUE;
32 }
33                 

效果如下:

posted @ 2013-01-03 23:41  SubmarineX  阅读(2933)  评论(0编辑  收藏  举报