MFC如何实现弹出窗口后背景变灰
展开全部
用一个灰色半透明对话框覆与对话框取异或即可
1.半透明对话框
void CForbiddenDlg::Transparent()
{
SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(this->GetSafeHwnd(), 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(this->GetSafeHwnd(), 0, 180, 2);
FreeLibrary(hInst);
}
}
2.对话框背景色
BOOL CForbiddenDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
BOOL retValue = CDialog::OnEraseBkgnd(pDC);
CRect rc;
GetClientRect(&rc);
pDC->FillSolidRect(&rc, RGB(0, 0, 0));
return retValue;
//return CDialog::OnEraseBkgnd(pDC);
}
3.与原对话框求异或
void CForbiddenDlg::SetupRegion()
{
CRect wRect, cRect, aRect;
CRgn wndRgn, rgnTemp;
CMainFrame *pMain = (CMainFrame *)AfxGetApp()->GetMainWnd();
pMain->GetWindowRect(wRect);
MoveWindow(wRect);
GetWindowRect(aRect);
pMain->GetClientRect(cRect);
wndRgn.CreateRectRgn(cRect.left, cRect.top, cRect.right + 5, cRect.bottom + 5);
rgnTemp.CreateRectRgn(aRect.left + 5 - wRect.left, aRect.top + 10 - wRect.top, aRect.right - 5 - wRect.left, aRect.bottom - 5 - wRect.top);
wndRgn.CombineRgn(&wndRgn, &rgnTemp, RGN_XOR);
rgnTemp.DeleteObject();
SetWindowRgn((HRGN)wndRgn, TRUE);
}
1.半透明对话框
void CForbiddenDlg::Transparent()
{
SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(this->GetSafeHwnd(), 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(this->GetSafeHwnd(), 0, 180, 2);
FreeLibrary(hInst);
}
}
2.对话框背景色
BOOL CForbiddenDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
BOOL retValue = CDialog::OnEraseBkgnd(pDC);
CRect rc;
GetClientRect(&rc);
pDC->FillSolidRect(&rc, RGB(0, 0, 0));
return retValue;
//return CDialog::OnEraseBkgnd(pDC);
}
3.与原对话框求异或
void CForbiddenDlg::SetupRegion()
{
CRect wRect, cRect, aRect;
CRgn wndRgn, rgnTemp;
CMainFrame *pMain = (CMainFrame *)AfxGetApp()->GetMainWnd();
pMain->GetWindowRect(wRect);
MoveWindow(wRect);
GetWindowRect(aRect);
pMain->GetClientRect(cRect);
wndRgn.CreateRectRgn(cRect.left, cRect.top, cRect.right + 5, cRect.bottom + 5);
rgnTemp.CreateRectRgn(aRect.left + 5 - wRect.left, aRect.top + 10 - wRect.top, aRect.right - 5 - wRect.left, aRect.bottom - 5 - wRect.top);
wndRgn.CombineRgn(&wndRgn, &rgnTemp, RGN_XOR);
rgnTemp.DeleteObject();
SetWindowRgn((HRGN)wndRgn, TRUE);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询