前言:
在Vista系統(tǒng)中利用dwapi.dll實現(xiàn)窗體全透明的方法,在網(wǎng)上更多的資源是基于WTL或是C#,借此發(fā)布一下基本MFC對話框?qū)崿F(xiàn)此此功能的最簡單的方法。
本方法只是實現(xiàn)整個程序的窗口界面(包括客戶區(qū))呈現(xiàn)半透明的效果,因此很簡單。編譯及運行需要 Windows Vista 或者 Windows Server 2008 系統(tǒng)的支持并且需要開啟透明功能才能顯示其效果。效果圖改日回家附上。
因為實現(xiàn)方法未包含系統(tǒng)檢測,未作其它系統(tǒng)(即XP、200、98)的處理方式,在這些系統(tǒng)中運行會報錯。
實現(xiàn)方法:
1、創(chuàng)建工程,打開Visual Studio 2005 或者 Visual Studio 2008 創(chuàng)建基本MFC的對話框程序,在例子中我采用的工程名是VcAeroGlass。
2、在stdafx.h中添加利用dwapi.dll的聲明
//stdafx.h
#include
#pragma comment(lib,"dwmapi")
3、在對話框?qū)崿F(xiàn)文件VcAeroGlassDlg.cpp的初始函數(shù)OnInitDialog()中作如下添加:
BOOL CVcAeroGlassDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//............................
//系統(tǒng)自動成,省略
MARGINS mar;
mar.cxLeftWidth = -1;
mar.cxRightWidth = -1;
mar.cyTopHeight = -1;
mar.cyBottomHeight =-1;
CRect rectClient;
GetClientRect (rectClient);
mar.cyTopHeight = rectClient.Height ();
DwmExtendFrameIntoClientArea (CDialog::GetSafeHwnd(),&mar );
return TRUE;
}
4、在實現(xiàn)文件的繪制函數(shù)OnPaint()中作考試大提示如下添加:
void CVcAeroGlassDlg::OnPaint()
{
CPaintDC dc(this); // 用于繪制的設備上下文
CRect rcGlassArea;
GetClientRect ( &rcGlassArea );
dc.FillSolidRect ( &rcGlassArea, RGB(0,0,0) );
//............................
//系統(tǒng)自動成,省略
}
5、編譯運行程序即可看到效果。
源碼以上傳:
For VC2005: http://download.csdn.net/source/710225
For VC2008SP1: http://download.csdn.net/source/710216
在Vista系統(tǒng)中利用dwapi.dll實現(xiàn)窗體全透明的方法,在網(wǎng)上更多的資源是基于WTL或是C#,借此發(fā)布一下基本MFC對話框?qū)崿F(xiàn)此此功能的最簡單的方法。
本方法只是實現(xiàn)整個程序的窗口界面(包括客戶區(qū))呈現(xiàn)半透明的效果,因此很簡單。編譯及運行需要 Windows Vista 或者 Windows Server 2008 系統(tǒng)的支持并且需要開啟透明功能才能顯示其效果。效果圖改日回家附上。
因為實現(xiàn)方法未包含系統(tǒng)檢測,未作其它系統(tǒng)(即XP、200、98)的處理方式,在這些系統(tǒng)中運行會報錯。
實現(xiàn)方法:
1、創(chuàng)建工程,打開Visual Studio 2005 或者 Visual Studio 2008 創(chuàng)建基本MFC的對話框程序,在例子中我采用的工程名是VcAeroGlass。
2、在stdafx.h中添加利用dwapi.dll的聲明
//stdafx.h
#include
#pragma comment(lib,"dwmapi")
3、在對話框?qū)崿F(xiàn)文件VcAeroGlassDlg.cpp的初始函數(shù)OnInitDialog()中作如下添加:
BOOL CVcAeroGlassDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//............................
//系統(tǒng)自動成,省略
MARGINS mar;
mar.cxLeftWidth = -1;
mar.cxRightWidth = -1;
mar.cyTopHeight = -1;
mar.cyBottomHeight =-1;
CRect rectClient;
GetClientRect (rectClient);
mar.cyTopHeight = rectClient.Height ();
DwmExtendFrameIntoClientArea (CDialog::GetSafeHwnd(),&mar );
return TRUE;
}
4、在實現(xiàn)文件的繪制函數(shù)OnPaint()中作考試大提示如下添加:
void CVcAeroGlassDlg::OnPaint()
{
CPaintDC dc(this); // 用于繪制的設備上下文
CRect rcGlassArea;
GetClientRect ( &rcGlassArea );
dc.FillSolidRect ( &rcGlassArea, RGB(0,0,0) );
//............................
//系統(tǒng)自動成,省略
}
5、編譯運行程序即可看到效果。
源碼以上傳:
For VC2005: http://download.csdn.net/source/710225
For VC2008SP1: http://download.csdn.net/source/710216