MFC设置透明窗口

MFC设置透明窗口,第1张

得到屏幕大小,把窗口设置为全屛

      int m_xScreen = GetSystemMetrics(SM_CXSCREEN)

int m_yScreen = GetSystemMetrics(SM_CYSCREEN)

SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,WS_EX_TOOLWINDOW)SetLayeredWindow(m_hWnd,80)SetWindowPos(&CWnd::wndTopMost,0,0,m_xScreen,m_yScreen,SWP_SHOWWINDOW)SetForegroundWindow()

实现透明函数

void SetLayeredWindow(HWND m_hWnd,int nRate)

{HINSTANCE hInstif ((hInst = LoadLibrary(_T("USER32.dll"))) != NULL)

    {

        SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED)typedef BOOL (WINAPI* lpfnSetTransparent)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags)if (hInst){lpfnSetTransparent pFnSetTransparent = NULLpFnSetTransparent= (lpfnSetTransparent)GetProcAddress(hInst, "SetLayeredWindowAttributes")if (pFnSetTransparent)pFnSetTransparent(m_hWnd, 0, (255 * nRate) / 100, LWA_ALPHA)}

    }

}

在其父窗口中响应OnCtlColor

然后添加如下代码

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)

{

HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor)

if (nCtlColor==CTLCOLOR_STATIC)

{

pDC->SetTextColor(RGB(255,0,0))//字体颜色

pDC->SetBkMode(TRANSPARENT)// 背景透明

}

return hbr

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11065709.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存