您的位置:首页 > 其它

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

2013-01-03 23:41 549 查看
思路来自:MFC Tutorial - Creating a window with two classes

在工程中新建一个类 CChildFrame

class CChildFrame : public CFrameWnd

在构造函数中:

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


可选:添加WM_PAINT消息:

void CChildFrame::OnPaint()
{
CPaintDC dc(this); // device context for painting

// TODO: Add your message handler code here
CDC *pDC = GetDC();

CBrush brush(RGB(0, 0, 0));
CBrush *pOldBrush = pDC->SelectObject(&brush);
pDC->Rectangle(0, 0, 500, 500);
pDC->SelectObject(pOldBrush);
// Do not call CFrameWnd::OnPaint() for painting messages
}


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

class CXXApp : public CWinApp
{
public:
CChildFrame *m_pChildWnd;

...
}


在CXXApp类的 InitInstance 方法中添加:

BOOL CMultilayerDisplayApp::InitInstance()
{
...

m_pChildWnd = new CChildFrame(); // 创建新窗口类对象
//////// 这部分用于透明此窗口对象关联的窗口///////////
//加入WS_EX_LAYERED扩展属性
SetWindowLong(m_pChildWnd->m_hWnd, GWL_EXSTYLE, GetWindowLong(m_pChildWnd->m_hWnd, GWL_EXSTYLE)^0x80000);
HINSTANCE hInst = LoadLibrary("User32.DLL");
if (hInst)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL;
//取得SetLayeredWindowAttributes函数指针
fun = (MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
if (fun)
{
fun(m_pChildWnd->m_hWnd, 0, 128, 2);
}
FreeLibrary(hInst);
}
//////// 这部分用于透明此窗口对象关联的窗口///////////

// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();

m_pChildWnd->ShowWindow(SW_SHOW);
m_pChildWnd->UpdateWindow();

return TRUE;
}


效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐