gdi特效字体:GDI+实现半透明渐变的特效窗口



偶然间甜石榴兄弟给我个东东是BlueCrab用VC写利用GDI+技术实现半透明渐变窗口特效看起来很不错在此对BlueCrab和甜石榴深表感谢ccrun(老妖)花了点时间将其在BCB中实现并实现了简单动态换肤效果图:
用GDI+实现半透明渐变<img src='/icons/94277de.gif' />特效窗口





在CBuilder中使用GDI+思路方法和代码网上遍地都是这里为了完整性简单说说流程:

1.) 在BCB6中已自带了ghiplus.h文件故只需要生成gdiplus.lib文件就可以:

在命令行下运行implib gdiplus.lib gdiplus.dll(如果ghiplus.dll不在当前文件夹下,注意写完整路径)

2.) 在工程编译选项中加入STRICT条件编译:

Project-->Options-->Directories/Conditionals-->Condtionals-->点击旁边"..."按钮-->输入STRICT然后Add

3.) 在工程中加入Gdiplus.lib:

Project-->Add To Project-->找到Gdiplus.lib添加进来

4.) 在工程.h文件中包含所需头文件,注意先后顺序:

# "math.hpp"
#
using std::min;
using std::max;
# "gdiplus.h"
using Gdiplus;
完整举例代码在这里下载(查看页面)http://www.ccrun.com/src/v.asp?id=36
.h文件中:private: //Userdeclarations
ULONG_PTRm_GdiplusToken;
Gdiplus::GdiplusStartupInputm_GdiplusStartupInput;
__fastcallSetTransparent(LPWSTRlpSKINFile,nTran);
BLENDFUNCTIONm_Blend;
HDC m_hdcMemory;
Gdiplus::Image*m_Image;
public: //Userdeclarations
__fastcallTfrmMain(TComponent*Owner);
__fastcall~TfrmMain(void);
.cpp文件中:
//---------------------------------------------------------------------------
//用GDI+实现半透明渐变特效窗口
//byccrun(老妖)[email protected]
//---------------------------------------------------------------------------
//WelcomeCBuilderStudy-http://www.ccrun.com
//---------------------------------------------------------------------------
#
#pragmahdrstop
#"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; //theonlyBlendOpdinWindows2000
m_Blend.BlendFlags=0; //nothingisspecial...
m_Blend.AlphaFormat=1; //...
m_Blend.SourceConstantAlpha=255;//AC_SRC_ALPHA
//
(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+
}
//---------------------------------------------------------------------------
__fastcallTfrmMain::SetTransparent(LPWSTRlpSkinFile,nTran)




{
//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
(nTran<0nTran>100)
nTran=100;
m_Blend.SourceConstantAlpha=(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);
((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;
bRet;
}
//---------------------------------------------------------------------------
void__fastcallTfrmMain::FormMouseDown(TObject*Sender,
TMouseButtonButton,TShtStateSht,X,Y)
{
(ButtonmbLeft)
{
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=TOpenDialog(this);
dlgOpen->Filter="PNGfile(*.png)*.png";
(dlgOpen->Execute)
{
SetTransparent(WideString(dlgOpen->FileName),100);
Invalidate;
}
deletedlgOpen;
}

Tags:  特效生命 收付实现制 css实现背景渐变 gdi特效字体

延伸阅读

最新评论

发表评论