程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用GDI+實現半透明漸變的特效窗口

用GDI+實現半透明漸變的特效窗口

編輯:關於C語言

偶然間甜石榴兄弟給我一個東東,是BlueCrab用VC寫的利用GDI+技術實現半透明漸變窗口的特效,看起來很不錯。在此對BlueCrab和甜石榴深表感謝。ccrun(老妖)花了點時間將其在BCB中實現,並實現了簡單的動態換膚。效果圖:

在C++Builder中使用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文件中包含所需的頭文件,注意先後順序:

#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(老妖)[email protected]
//---------------------------------------------------------------------------
//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_NOMOVE|SWP_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<0||nTran>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_MOVE|HTCAPTION,0);
}
}
//---------------------------------------------------------------------------
void__fastcallTfrmMain::miShowAboutClick(TObject*Sender)
{
MessageBox(Handle,
"在BCB中用GDI+實現半透明漸變的特效窗口 "
"------------------------- "
"byccrun(老妖) "
"Welcometowww.ccrun.com",
"GDI+Window",MB_OK|MB_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_NOMOVE|SWP_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;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved