第一步:
在資源視圖添加Dialog窗口
第二步:
生成窗口類,改變窗口屬性,將窗口設置爲根據背景色透明;主要實現代碼如下:
2.1先在OnPaint()中將背景色改成白色
void xxxx::OnPaint()
{
CPaintDC dc(this);
CRect rect;
GetClientRect(rect);
dc.FillSolidRect(rect,RGB(255,255,255));
}
2.2在OnInitDialog()實現窗口透明
BOOL xxxx::OnInitDialog()
{
xxxx::OnInitDialog();
//創建畫筆
m_newPen.CreatePen(PS_SOLID, 1, RGB(0, 255, 0));//創建畫筆
#if 0
//設置窗體透明
SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(GetSafeHwnd(),GWL_EXSTYLE)|WS_EX_LAYERED);
SetLayeredWindowAttributes(0,200,LWA_ALPHA);
#else
//設置窗體的某種顏色透明
CClientDC dc(this);
COLORREF maskColor = RGB(255,255,255);//掩碼顏色
SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE, GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)| 0x80000);//設定窗體使用擴展模式
HINSTANCE hInst = LoadLibrary(_T("User32.DLL"));
if( hInst )
{
typedef BOOL( WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC AlphaFunc = NULL;
AlphaFunc=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
if(AlphaFunc)
{
AlphaFunc(this->GetSafeHwnd(),maskColor,255,1);//將與掩碼顏色相同的窗體部分置爲透明
}
FreeLibrary(hInst);
}
#endif
return TRUE; // return TRUE unless you set the focus to a control
}
第三步:
將窗體附在IPC控件之上,當IPC控件捕獲鼠標信息時,在透明窗體上繪圖即可。
最終效果如下: