用GDI 实现半透明渐变的特效窗口

2008-02-23 05:38:01来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

偶然间甜石榴兄弟给我一个东东,是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 <algorithm>
using std::min;
using std::max;
#include "gdiplus.h"
using namespace Gdiplus;

完整示例代码在这里下载(查看页面)http://www.ccrun.com/src/v.asp?id=36

.h文档中:
private
: // User declarations
ULONG_PTR m_GdiplusToken;
Gdiplus::GdiplusStartupInput m_GdiplusStartupInput;
int __fastcall SetTransparent(LPWSTR lpSkinFile, int nTran);

BLENDFUNCTION m_Blend;
HDC m_hdcMemory;
Gdiplus::Image *m_Image;

public: // User declarations
__fastcall TfrmMain(TComponent* Owner);
__fastcall ~TfrmMain(void);

.cpp文档中:
//---------------------------------------------------------------------------
// 用GDI 实现半透明渐变的特效窗口
// by ccrun(老妖) - info@ccrun.com
//---------------------------------------------------------------------------
// Welcome C Builder Study - http://www.ccrun.com
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "uMain.h"


//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;

//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
BorderStyle = bsNone;
// init GDI
GdiplusStartup(&m_GdiplusToken, &m_GdiplusStartupInput, NULL);
//
m_Blend.BlendOp = 0; // the only BlendOp defined in Windows 2000
m_Blend.BlendFlags = 0; // nothing else is special ...
m_Blend.AlphaFormat = 1; // ...
// 本文转自 C Builder 研究 - http://www.ccrun.com/article.asp?i=643&d=n5u8o4
m_Blend.SourceConstantAlpha = 255; // AC_SRC_ALPHA
//
if(FileExists(ExtractFilePath(ParamStr(0)) "\\test.png"))
SetTransparent(WideString("test.png"), 100);
// Stay on top
SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
//---------------------------------------------------------------------------
__fastcall TfrmMain::~TfrmMain(void)
{
GdiplusShutdown(m_GdiplusToken); // Close GDI
}
//---------------------------------------------------------------------------
int __fastcall TfrmMain::SetTransparent(LPWSTR lpSkinFile, int nTran)
{
// Use GDI load image
m_Image = Gdiplus::Image::FromFile(lpSkinFile);
// Change Form size
Width = m_Image->GetWidth();
Height = m_Image->GetHeight();
// Create Compatible Bitmap

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: 如何自定义提示窗口(Hint Window)

下一篇: C程式研发经典实例之1