标题
http://www.ccrun.com/article.asp?i=643&d=n5u8o4
clq
浏览(0) +
2008-12-23 11:33:29 发表
编辑
关键字:
http://www.ccrun.com/article.asp?i=643&d=n5u8o4 用GDI+实现半透明渐变的特效窗口 关键字:GDI+,GDIPlus,半透明,渐变,特效窗口 作者:ccrun 更新:2006-03-15 浏览:8944 偶然间甜石榴兄弟给我一个东东,是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: // 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 #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; // ... 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 HDC hdcTemp = GetDC(0); m_hdcMemory = CreateCompatibleDC(hdcTemp); HBITMAP hBitMap = CreateCompatibleBitmap(hdcTemp, m_Image->GetWidth(), m_Image->GetHeight()); // 本文转自 C++Builder研究 - http://www.ccrun.com/article.asp?i=643&d=n5u8o4 SelectObject(m_hdcMemory, hBitMap); // Alpha Value if (nTran<0 || nTran >100) nTran = 100; m_Blend.SourceConstantAlpha = int(nTran * 2.55); // 1~255 // HDC hdcScreen = ::GetDC(0); RECT rct; GetWindowRect(Handle, &rct); // POINT ptWinPos = {rct.left, rct.top}; Gdiplus::Graphics graph(m_hdcMemory); // 63 63 72 75 6E 2E 63 6F 6D graph.DrawImage(m_Image, 0, 0, m_Image->GetWidth(), m_Image->GetHeight()); // SIZE sizeWindow = {m_Image->GetWidth(), m_Image->GetHeight()}; POINT ptSrc = {0, 0}; // Set Window style DWORD dwExStyle = GetWindowLong(Handle, GWL_EXSTYLE); if((dwExStyle & 0x80000) != 0x80000) SetWindowLong(Handle, GWL_EXSTYLE, dwExStyle ^ 0x80000); // perform the alpha blend BOOL bRet = 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; return bRet; } //--------------------------------------------------------------------------- void __fastcall TfrmMain::FormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { if(Button == mbLeft) { ReleaseCapture(); Perform(WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0); } } //--------------------------------------------------------------------------- void __fastcall TfrmMain::miShowAboutClick(TObject *Sender) { MessageBox(Handle, "在BCB中用GDI+实现半透明渐变的特效窗口\r\n" "-------------------------\r\n" "by ccrun(老妖)\r\n" "Welcome to www.ccrun.com", "GDI+ Window", MB_OK | MB_ICONINFORMATION); } //--------------------------------------------------------------------------- void __fastcall TfrmMain::miCloseAppClick(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void __fastcall TfrmMain::miGoToCcrunClick(TObject *Sender) { ShellExecute(Handle, "Open", "http://www.ccrun.com", NULL, NULL, SW_SHOW); } //--------------------------------------------------------------------------- void __fastcall TfrmMain::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 __fastcall TfrmMain::miChangeSkinClick(TObject *Sender) { TOpenDialog *dlgOpen = new TOpenDialog(this); dlgOpen->Filter = "PNG file(*.png)|*.png"; if(dlgOpen->Execute()) { SetTransparent(WideString(dlgOpen->FileName), 100); Invalidate(); } delete dlgOpen; } 完整示例代码在这里下载(查看页面)http://www.ccrun.com/src/v.asp?id=36
[图片]
NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.