用GDI+实现半透明渐变的特效窗口
2008-03-08 21:57:13 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鎯у⒔閹虫捇鈥旈崘顏佸亾閿濆簼绨绘い鎺嬪灪閵囧嫰骞囬姣挎捇鏌熸笟鍨妞ゎ偅绮撳畷鍗炍旈埀顒勭嵁婵犲嫮纾介柛灞捐壘閳ь剛鎳撻~婵嬪Ω閳轰胶鐤呯紓浣割儐椤戞瑩宕ョ€n喗鐓曟い鎰靛亝缁舵氨绱撻崘鈺傜婵﹤顭峰畷鎺戔枎閹搭厽袦婵犵數濮崑鎾绘⒑椤掆偓缁夌敻骞嗛悙鍝勭婵烇綆鍓欐俊鑲╃磼閹邦収娈滈柡灞糕偓鎰佸悑閹肩补鈧尙鏁栧┑鐐村灦閹稿摜绮旈悽绋课﹂柛鏇ㄥ灠閸愨偓濡炪倖鍔﹀鈧繛宀婁邯濮婅櫣绱掑Ο璇茶敿闂佺ǹ娴烽弫璇差嚕婵犳碍鏅插璺猴工瀹撳棝姊虹紒妯哄缂佷焦鎸冲畷鎴﹀箻鐠囧弶宓嶅銈嗘尰缁嬫垶绂嶉悙顒佸弿婵☆垳鍘ф禍楣冩倵濮樼偓瀚�

核心提示:偶然间甜石榴兄弟给我一个东东,是BlueCrab用VC写的利用GDI+技术实现半透明渐变窗口的特效,用GDI+实现半透明渐变的特效窗口,看起来很不错,在此对BlueCrab和甜石榴深表感谢,(假如ghiplus.dll不在当前文件夹下,注重写完整路径)2.) 在工程的编译选项中加入STRICT条件编译:PRoject-
偶然间甜石榴兄弟给我一个东东,是BlueCrab用VC写的利用GDI+技术实现半透明渐变窗口的特效,看起来很不错。在此对BlueCrab和甜石榴深表感谢。ccrun(老妖)花了点时间将其在BCB中实现,并实现了简单的动态换肤。效果图:

#include "math.hpp"
#include
using std::min;
using std::max;
#include "gdiplus.h"
using namespace Gdiplus;
完整示例代码在这里下载(查看页面)http://www.ccrun.com/src/v.asp?id=36 .h文件中:
private: //Userdeclarations
ULONG_PTRm_GdiplusToken;
Gdiplus::GdiplusStartupInputm_GdiplusStartupInput;
int__fastcallSetTransparent(LPWSTRlpSKINFile,intnTran);
BLENDFUNCTIONm_Blend;
HDC m_hdcMemory;
Gdiplus::Image*m_Image;
public: //Userdeclarations
__fastcallTfrmMain(TComponent*Owner);
__fastcall~TfrmMain(void);
.cpp文件中:
//---------------------------------------------------------------------------
//用GDI+实现半透明渐变的特效窗口
//byccrun(老妖)-info@ccrun.com
//---------------------------------------------------------------------------
//WelcomeC++BuilderStudy-http://www.ccrun.com
//---------------------------------------------------------------------------
#include
#pragmahdrstop
#include"uMain.h"
//---------------------------------------------------------------------------
#pragmapackage(smart_init)
#pragmaresource"*.dfm"
TfrmMain*frmMain;
//---------------------------------------------------------------------------
__fastcallTfrmMain::TfrmMain(TComponent*Owner)
:TForm(Owner)
{
BorderStyle=bsNone;
//initGDI+
GdiplusStartup(&m_GdiplusToken,&m_GdiplusStartupInput,NULL);
//
m_Blend.BlendOp=0; //theonlyBlendOpdefinedinWindows2000
m_Blend.BlendFlags=0; //nothingelseisspecial...
m_Blend.AlphaFormat=1; //...
m_Blend.SourceConstantAlpha=255;//AC_SRC_ALPHA
//
if(FileExists(ExtractFilePath(ParamStr(0))+"\\test.png"))
SetTransparent(WideString("test.png"),100);
//Stayontop
SetWindowPos(Handle,HWND_TOPMOST,0,0,0,0,SWP_NOMOVESWP_NOSIZE);
}
//---------------------------------------------------------------------------
__fastcallTfrmMain::~TfrmMain(void)
{
GdiplusShutdown(m_GdiplusToken);//CloseGDI+
}
//---------------------------------------------------------------------------
int__fastcallTfrmMain::SetTransparent(LPWSTRlpSkinFile,intnTran)
{
//UseGDI+loadimage
m_Image=Gdiplus::Image::FromFile(lpSkinFile);
//ChangeFormsize
Width=m_Image->GetWidth();
Height=m_Image->GetHeight();
//CreateCompatibleBitmap
HDChdcTemp=GetDC(0);
m_hdcMemory=CreateCompatibleDC(hdcTemp);
HBITMAPhBitMap=CreateCompatibleBitmap(hdcTemp,
m_Image->GetWidth(),m_Image->GetHeight());
SelectObject(m_hdcMemory,hBitMap);
//AlphaValue
if(nTran<0nTran>100)
nTran=100;
m_Blend.SourceConstantAlpha=int(nTran*2.55);//1~255
//
HDChdcScreen=::GetDC(0);
RECTrct;
GetWindowRect(Handle,&rct);
//
POINTptWinPos={rct.left,rct.top};
Gdiplus::Graphicsgraph(m_hdcMemory);
//636372756E2E636F6D
graph.DrawImage(m_Image,0,0,m_Image->GetWidth(),m_Image->GetHeight());
//
SIZEsizeWindow={m_Image->GetWidth(),m_Image->GetHeight()};
POINTptSrc={0,0};
//SetWindowstyle
DWorddwExStyle=GetWindowLong(Handle,GWL_EXSTYLE);
if((dwExStyle&0x80000)!=0x80000)
SetWindowLong(Handle,GWL_EXSTYLE,dwExStyle^0x80000);
//performthealphablend
BOOLbRet=UpdateLayeredWindow(Handle,hdcScreen,&ptWinPos,
&sizeWindow,m_hdcMemory,&ptSrc,0,&m_Blend,2);
//
graph.ReleaseHDC(m_hdcMemory);
ReleaseDC(0,hdcScreen);
hdcScreen=NULL;
ReleaseDC(0,hdcTemp);
hdcTemp=NULL;
DeleteObject(hBitMap);
DeleteDC(m_hdcMemory);
m_hdcMemory=NULL;
m_Image=NULL;
returnbRet;
}
//---------------------------------------------------------------------------
void__fastcallTfrmMain::FormMouseDown(TObject*Sender,
TMouseButtonButton,TShiftStateShift,intX,intY)
{
if(Button==mbLeft)
{
ReleaseCapture();
Perform(WM_SYSCOMMAND,SC_MOVEHTCAPTION,0);
}
}
//---------------------------------------------------------------------------
void__fastcallTfrmMain::miShowAboutClick(TObject*Sender)
{
MessageBox(Handle,
"在BCB中用GDI+实现半透明渐变的特效窗口\r\n"
"-------------------------\r\n"
"byccrun(老妖)\r\n"
"Welcometowww.ccrun.com",
"GDI+Window",MB_OKMB_ICONINFORMATION);
}
//---------------------------------------------------------------------------
void__fastcallTfrmMain::miCloseAppClick(TObject*Sender)
{
Close();
}
//---------------------------------------------------------------------------
void__fastcallTfrmMain::miGoToCcrunClick(TObject*Sender)
{
ShellExecute(Handle,"Open","http://www.ccrun.com",NULL,NULL,SW_SHOW);
}
//---------------------------------------------------------------------------
void__fastcallTfrmMain::miStayOnTopClick(TObject*Sender)
{
TMenuItem*mi=(TMenuItem*)Sender;
mi->Checked=!mi->Checked;
SetWindowPos(Handle,mi->Checked?HWND_TOPMOST:HWND_NOTOPMOST,
0,0,0,0,SWP_NOMOVESWP_NOSIZE);
}
//---------------------------------------------------------------------------
void__fastcallTfrmMain::miChangeSkinClick(TObject*Sender)
{
TOpenDialog*dlgOpen=newTOpenDialog(this);
dlgOpen->Filter="PNGfile(*.png)*.png";
if(dlgOpen->Execute())
{
SetTransparent(WideString(dlgOpen->FileName),100);
Invalidate();
}
deletedlgOpen;
}
[]
更多精彩
赞助商链接