Use GDI + to implement a special window with a translucent gradient

Source: Internet
Author: User

By accident, the sweet pomegranate brother gave me something that was written by BlueCrab using VC and realized the special effect of translucent gradient window using the GDI + technology. It looks very good. I would like to express my deep gratitude to BlueCrab and sweet pomegranate. The ccrun (old demon) took some time to implement it in BCB, and implemented a simple dynamic skin replacement. :

The methods and code using GDI + in C ++ Builder are everywhere on the Internet. Here, for integrity, let's briefly talk about the process:

1.) The ghiplus. h file is already included in BCB6, so you only need to generate the gdiplus. lib file:

Run implib gdiplus. lib gdiplus. dll in the command line. (If ghiplus. dll is not in the current folder, write the complete path)

2) Add STRICT condition compilation to the compilation options of the project:

Project --> Options --> Directories/Conditionals --> Condtionals --> click "..." next to it --> enter STRICT, and then Add.

3) add Gdiplus. lib to the project:

Project --> Add To Project --> Find Gdiplus. lib To Add it.

4) include the required header files in the. h file of the Project. Pay attention to the sequence:

#include "math.hpp"
#include
using std::min;
using std::max;
#include "gdiplus.h"
using namespace Gdiplus;
The complete sample code is downloaded here (view page) http://www.ccrun.com/src/v.asp? Id = 36
. H file: 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 );
In the. cpp file:
//---------------------------------------------------------------------------
// Use GDI + to implement a special effect window with a translucent gradient
// 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, 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, & RDBMS );
//
POINTptWinPos = {RDBMS. left, RDBMS. 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 ^ 0 x80000 );
// Define mthealphablend
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,
"Use GDI + in BCB to implement a special effect window with a translucent gradient"
"-------------------------"
"Byccrun (old demon )"
"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, 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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.