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                 

效果如下:

转载于:https://www.cnblogs.com/submarinex/archive/2013/01/03/2843501.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值