關閉Win7的半透明效果

來源:互聯網
上載者:User

關閉Win7的半透明效果

文章出自:blog.csdn.net/windows_nt

DwmEnableComposition調用這個函數可關閉整個系統的半透明效果

DwmSetWindowAttribute調用此函數可關閉某個視窗的半透明效果

只適用xp以上系統,因為xp系統沒有dwmapi.dll這個連結庫

 

以下代碼放在程式的BOOL CXXApp::InitInstance()中調用

 

關閉系統半透明效果

 typedef  HRESULT  (__stdcall  *lpfnDwmIsCompositionEnabled)(BOOL *state);

 typedef  HRESULT  (__stdcall  *lpfnDwmEnableComposition)(UINT setstate);
 lpfnDwmIsCompositionEnabled     lpfnIsEnabled = 0;
 lpfnDwmEnableComposition        lpfnSetEnable = 0;
 
 HMODULE library = ::LoadLibrary(_T("dwmapi.dll"));
 bool result = 0;
 
 if (0 != library)
 {
  lpfnIsEnabled =(lpfnDwmIsCompositionEnabled)GetProcAddress(library,"DwmIsCompositionEnabled");
  lpfnSetEnable =(lpfnDwmEnableComposition)GetProcAddress(library,"DwmEnableComposition");
  
  if (lpfnIsEnabled != 0)
  {
   BOOL enabled = FALSE;
   result = SUCCEEDED(lpfnIsEnabled(&enabled)) && enabled;
  }
  
  if(result && lpfnSetEnable != 0)
  {
   result = SUCCEEDED(lpfnSetEnable(0/*Disable*/));
  }
  
  VERIFY(::FreeLibrary(library));
 }

 

關閉某個視窗的半透明效果
如果編譯器是vs2008以上的版本的話,代碼非常簡單,只需要在視窗初始化的地方加下如下代碼:

    DWMNCRENDERINGPOLICY ncrp = DWMNCRP_DISABLED;
    DwmSetWindowAttribute(GetSafeHwnd(),DWMWA_NCRENDERING_POLICY,&ncrp,sizeof(ncrp));

再包含下#include <dwmapi.h>

如果編譯環境低於VS2008並且沒有 VISTA SDK的話是如下的代碼:

由於編譯環境中沒有dwmapi.h檔案,所以需要的一些定義就得自己加了,如下加了兩個枚舉。

//關閉某個應用程式的半透明效果,此代碼須放在m_pMainWnd指標被賦值後調用以下代碼
enum DWMWINDOWATTRIBUTE
{
    DWMWA_NCRENDERING_ENABLED = 1,      // [get] Is non-client rendering enabled/disabled
        DWMWA_NCRENDERING_POLICY,           // [set] Non-client rendering policy
        DWMWA_TRANSITIONS_FORCEDISABLED,    // [set] Potentially enable/forcibly disable transitions
        DWMWA_ALLOW_NCPAINT,                // [set] Allow contents rendered in the non-client area to be visible on the DWM-drawn frame.
        DWMWA_CAPTION_BUTTON_BOUNDS,        // [get] Bounds of the caption button area in window-relative space.
        DWMWA_NONCLIENT_RTL_LAYOUT,         // [set] Is non-client content RTL mirrored
        DWMWA_FORCE_ICONIC_REPRESENTATION, // [set] Force this window to display iconic thumbnails.
        DWMWA_FLIP3D_POLICY,                // [set] Designates how Flip3D will treat the window.
        DWMWA_EXTENDED_FRAME_BOUNDS,        // [get] Gets the extended frame bounds rectangle in screen space
        DWMWA_LAST
};
typedef enum _DWMNCRENDERINGPOLICY {
    DWMNCRP_USEWINDOWSTYLE,
        DWMNCRP_DISABLED,
        DWMNCRP_ENABLED,
        DWMNCRP_LAST
} DWMNCRENDERINGPOLICY;

然後在動態連結程式庫中得到去掉半透明效果的函數(DwmSetWindowAttribute)進而執行該函數
    typedef HRESULT (WINAPI * TmpFun)(HWND,DWORD,LPCVOID,DWORD);
    HMODULE library = ::LoadLibrary("dwmapi.dll");

    TmpFun DwmSetWindowAttributeEX;
    if (0 != library)   
    {
        DwmSetWindowAttributeEX = (TmpFun)::GetProcAddress(library,
            "DwmSetWindowAttribute");
        if (DwmSetWindowAttributeEX)
        {   
            DWMNCRENDERINGPOLICY ncrp = DWMNCRP_DISABLED;
            (*DwmSetWindowAttributeEX)(m_pMainWnd->GetSafeHwnd(),DWMWA_NCRENDERING_POLICY,&ncrp,sizeof(ncrp));
           
        }
        VERIFY(::FreeLibrary(library));   
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.