1. 程式人生 > >設定視窗背景透明

設定視窗背景透明

 SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(GetSafeHwnd(),GWL_EXSTYLE)|0x80000);

 typedef BOOL (WINAPI *FSetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);

 FSetLayeredWindowAttributes SetLayeredWindowAttributes ;

 HINSTANCE hInst = LoadLibrary("User32.DLL");

 SetLayeredWindowAttributes = (FSetLayeredWindowAttributes)GetProcAddress(hInst,"SetLayeredWindowAttributes");

 if (SetLayeredWindowAttributes)
 {
  SetLayeredWindowAttributes(GetSafeHwnd(),RGB(0,0,0),128,2);
 }

 FreeLibrary(hInst);

通過這種方式可以使得視窗背景透明,但同時會使得其子視窗也變得透明。我嘗試過使用同樣的方式讓子視窗不透明也即

 SetWindowLong(子視窗控制代碼,GWL_EXSTYLE,GetWindowLong(GetSafeHwnd(),GWL_EXSTYLE)|0x80000);

 typedef BOOL (WINAPI *FSetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);

 FSetLayeredWindowAttributes SetLayeredWindowAttributes ;

 HINSTANCE hInst = LoadLibrary("User32.DLL");

 SetLayeredWindowAttributes = (FSetLayeredWindowAttributes)GetProcAddress(hInst,"SetLayeredWindowAttributes");

 if (SetLayeredWindowAttributes)
 {
  
SetLayeredWindowAttributes(子視窗控制代碼,RGB(0,0,0),255,2);

 }

 FreeLibrary(hInst);

發現行不通

另外SetLayeredWindowAttributes 在 msdn 上雖然可以查到使用說明,但嘗試包含宣告的標頭檔案並不能找到其宣告。