MFC半透明对话框
int CTestDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDialog::OnCreate(lpCreateStruct) == -1) return -1; //=============================================================== //要使使窗体拥有透明效果,首先要有WS_EX_LAYERED扩展属性 #ifndef WS_EX_LAYERED #define WS_EX_LAYERED 0x00080000 #endif SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(GetSafeHwnd(),GWL_EXSTYLE) | WS_EX_LAYERED); HINSTANCE hInst= LoadLibraryA("User32.dll"); if(hInst) { //hWnd是透明窗体的句柄 //crKey为掩码的颜色,可以用RGB(r,g,b)来指定 //bAlpha是不透明度,取值范围是(0,255),其中0代表全透明,255代表不透明 //dwFlags是透明方式,当取值为LWA_ALPHA时,crKey参数无效,bAlpha参数有效;当取值为LWA_COLORKEY时,窗体中的所有颜色为crKey的地方将变为透明,bAlpha参数无效 //dwFlags可取两个值的组合:LWA_ALPHA | LWA_COLORKEY,这样crKey的地方将变为全透明,而其它地方根据bAlpha参数确定透明度 typedef BOOL (WINAPI *FUNC)(HWND hwnd,COLORREF crKey,BYTE bAlpha,DWORD dwFlags); FUNC func=NULL; func=(FUNC)GetProcAddress(hInst,"SetLayeredWindowAttributes"); if(func) { func(GetSafeHwnd(),0,0x80,LWA_ALPHA/*bAlpha生效*/); } FreeLibrary(hInst); } //=============================================================== return 0; }
效果图: