I. Preparation (PNG Image blank format)
1. Prepare image resources. First, edit the used image in photoshop, and save the image as a. PNG with a transparent channel (the background can be transparently tuned during the display of the GDI + call ). This wayProgramThe image resources are ready.
2. The basic preparations for this work are as follows.
Create a dialog box application named GDI + PNG
Settings in vc6.0:
(1) download the SDK for gdiplus forvc6.0 (more than two megabytes in total)
(2) create a folder "GDI +" on drive C to copy the development kit, that is, create the following path for example.CodeCompile smoothly (of course, you can put it wherever you like, as long as the path is correctly included in your project !).
(You can also set gdiplus. H is placed in the include path of your vc6.0, gdiplus. place Lib in your lib path and set gdiplus. DLL is stored in your system32 directory. gdiplus is required for earlier versions of Windows2000. DLL files)
C: \ GDI ++ \ regiondes
C: \ GDI ++ \ Lib
C: \ GDI + \ gdiplus. dll
(3) Add the settings for the GDI + Environment in stdafx. h # define Unicode
# Ifndef ulong_ptr // none on vc2003
# Define ulong_ptr unsigned long * // It is optional on vc2003 (actually, It is 4 bytes without an Sn)
# Endif // available on vc2003
# Include <comdef. h> // initialize the comport
# Include "C: \ GDI + \ des \ gdiplus. H" // modify the path of your header file
Using namespace gdiplus; // namespace
# Pragma comment (Lib, "C: \ GDI + \ Lib \ gdiplus. lib") // modify it to your. Lib file path.
The following settings are available in vc2003:
(1) Open "project" --- "property" --- "linker" --- "input" --- "Additional dependency" in vs2003.net and enter "gdiplus. lib ".
(2) In the "stdafx. H" file, enter
# Include <comdef. h> // initialize the comport
# Include <gdiplus. h> // GDI + header file
Using namespace gdiplus; // namespace
(3) vs2003 already has the GDI + Library.
In the main header file of the application (GDI + PNG. h), add a member variable to the application class of the application project, as shown in the following code:
Ulong_ptr m_pgditoken; // Where ulong_ptr is a DWORD data type,
// This member variable is used to save the GDI + identifier in the application after the GDI + is initialized,
// This identifier can be referenced to call gdiplus: gdiplusshutdown after the application exits to disable GDI +
Add the following code to the class behavior (GDI + PNG. cpp) initinstance () initialization function of the application:
Cwinapp: initinstance (); // after this function
Gdiplusstartupinput m_gdiplusstartupinput;
Gdiplusstartup (& m_pgditoken, & m_gdiplusstartupinput, null );
// After the dialog box program ends, add the overload of exitinstance to the application class, and add the following code to close the GDI +,
// Disable the gdiplus Environment
Add:
Gdiplusshutdown (m_pgditoken );
Return cwinapp: exitinstance (); // before this statement
Ii. Procedures
1. Define all class member variables in GDI + pngdlg. H, including the pointers of all images and the length and width of images.
// Custom data
Image * m_pimageback; // background image pointer. image is an image class.
HDC m_hdcmemory;
Int m_bakwidth; // The background image width.
Int m_bakheight; // The background image height.
Blendfunction m_blend;
Hinstance hfuncinst;
Typedef bool (winapi * myfunc) (hwnd, HDC, point *, size *, HDC, point *, colorref, blendfunction *, DWORD );
Myfunc updatelayeredwindow;
In this step, you must note that when creating a transparent window, you need to call a Windows API function updatelayeredwindow.. Net or later versions of the SDK have a statement, but in vc6.0, you must either download a later version SDK of over 200 MB or call it from the dynamic link library "user32.dll, here we choose to call it from "user32.dll. The last three items in the above definition are prepared for this purpose.
Updatelayeredwindow (hwnd: hwnd; // window handle
Hdcdst: HDC; // Target DC
Ptdst: Ppoint; // topleft of the target
Size: psize; // display size
Hdcsrc: HDC; // source DC
Ptsrc: Ppoint; // topleft of the source DC
Crkey: colorref; // transparent color value
Blend: pblendfunction; // Alpha hybrid function
Dwflags: DWORD // a set of flag Constants
);
2. In the oncreate () dialog box, add the following code: Initialize the function and member variables! (The imagefromidresource () function is a method for loading PNG images from resources !)
int cgdipclockdlg: oncreate (maid)
{< br> If (cdialog: oncreate (lpcreatestruct) =-1)
return-1;
// todo: add your specific creation code here
hfuncinst = loadlibrary ("user32.dll");
If (hfuncinst)
updatelayeredwindow = (myfunc) getprocaddress (hfuncinst, "updatelayeredwindow");
else
{< br> afxmessagebox ("user32.dll error! ");
exit (0);
}< br> // initialize the gdiplus environment
// initialize GDI +.
m_blend.blendop = 0; // theonlyblendopdefinedinwindows2000
m_blend.blendflags = 0; // nothingelseisspecial...
m_blend.alphaformat = 1 ;//...
m_blend.sourceconstantalpha = 255; // ac_src_alpha // transparency
Using namespace gdiplus; // namespace
M_pimageback = gdiplus: Image: fromfile (L "F: \ abc.png"); // read the file directly. Note that the double slash "\" is used.
// Imagefromidresource (idr_png2, "PNG", m_pimageback); // read the PNG image from the resource and define the class "PNG"
// Here, image does not provide bytes to call the functions of images in resources,
// Imagefromidresource () transmits the image pointer of the image to the pointer application by the Resource Name "PNG" and resource ID. .
// This function is taken from the Internet
// Imagefileme ("F: \ abc.png", m_pimageback); // read binary files
M_bakwidth = m_pimageback-> getwidth (); // returns the image width.
M_bakheight = m_pimageback-> getheight (); // returns the Image Height.
// The following figure shows the implementation window at the top.
: Setwindowpos (m_hwnd, hwnd_topmost, 0, 0, m_bakwidth, m_bakheight, swp_nosize | swp_nomove );
Return 0;
}
3. Enter the code for deleting the image in ondestroy ().
Void cmfcgdi1dlg: ondestroy ()
{
Cdialog: ondestroy ();
// Todo: add the message processing program code here
Delete m_pimageback;
M_pimageback = NULL;
}
5. Enter the following code in onpaint (). m_blend.sourceconstantalpha indicates the transparency of the entire form.
HDC hdctemp = getdc ()-> m_hdc;
M_hdcmemory = createcompatibledc (hdctemp );
Hbitmap = createcompatiblebitmap (hdctemp, m_bakwidth, m_bakheight );
SelectObject (m_hdcmemory, hbitmap );
// M_blend.sourceconstantalpha = 100; // The maximum window transparency is 255, and the minimum window transparency is 0.
HDC hdcscreen =: getdc (m_hwnd );
Rect CTS;
Getwindowrect (& RDBMS );
Point ptwinpos = {RDBMS. Left, RDBMS. Top };
Graphics graph (m_hdcmemory );
Point Points [] = {point (0, 0 ),
Point (m_bakwidth, 0 ),
Point (0, m_bakheight )};
Static bool bfly = false;
Graph. drawimage (m_pimageback, points, 3 );
Size sizewindow = {m_bakwidth, m_bakheight };
Point ptsrc = {0, 0 };
DWORD dwexstyle = getwindowlong (m_hwnd, gwl_exstyle );
If (dwexstyle & 0x80000 )! = 0x80000)
Setwindowlong (m_hwnd, gwl_exstyle, dwexstyle ^ 0 x80000 );
Updatelayeredwindow (m_hwnd, hdcscreen, & ptwinpos, & sizewindow, m_hdcmemory, & ptsrc, 0, & m_blend, 2 );
graph. releasehdc (m_hdcmemory);
:: releasedc (m_hwnd, hdcscreen);
hdcscreen = NULL;
:: releasedc (m_hwnd, hdctemp );
hdctemp = NULL;
deleteobject (hbitmap);
deletedc (m_hdcmemory);
m_hdcmemory = NULL;
The following is a custom function for reading resources. You do not need to enter an image if it is not in the resource.
bool cgdipclockdlg: imagefromidresource (uint NID, lpctstr STR, image * & pimg)
{< br> hinstance hinst = afxgetresourcehandle ();
hrsrc =: findresource (hinst, makeintresource (NID), STR); // type
If (! Hrsrc)
return false;
// load resource into memory
DWORD Len = sizeofresource (hinst, hrsrc );
byte * lprsrc = (byte *) loadresource (hinst, hrsrc);
If (! Lprsrc)
return false;
// allocate global memory on which to create stream
hglobal m_hmem = globalalloc (gmem_fixed, Len );
byte * pmem = (byte *) globallock (m_hmem);
memcpy (pmem, lprsrc, Len);
istream * PSTM;
createstreamonhglobal (m_hmem, false, & PSTM);
// load from stream
pimg = gdiplus: Image: fromstream (PSTM );
// free/release stuff
globalunlock (m_hmem);
PSTM-> release ();
freeresource (lprsrc );
}
bool cmfcgdi1dlg: imagefileme (lpctstr filename, image * & pimg) // I added it to read binary files. You do not need to enter
{< br> // allocate global memory on which to create stream
hfile filehwnd;
ofstruct fileopenbuff;
filehwnd = openfile (filename, & fileopenbuff, of_read);
If (filehwnd = hfile_error)
return false;
int Len = getfilesize (handle) filehwnd, 0);
Hglobal m_hmem = globalalloc (gmem_fixed, Len );
Byte * pmem = (byte *) globallock (m_hmem );
_ Lread (filehwnd, m_hmem, Len );
Istream * PSTM;
Createstreamonhglobal (m_hmem, false, & PSTM );
// Load from stream
Pimg = gdiplus: Image: fromstream (PSTM );
// Free/release stuff
Globalunlock (m_hmem );
PSTM-> release ();
_ Lclose (filehwnd );
}
You can drag the window as follows.
Void cgdipclockdlg: onlbuttondown (uint nflags, cpoint point)
{
// Disable the display of a box with a moving rectangle
: Systemparametersinfo (spi_setdragfullwindows, true, null, 0 );
// Move the entire window without the title bar
Sendmessage (wm_syscommand, 0xf012, 0 );
// Postmessage (wm_nclbuttondown, htcaption, makelparam (point. X, point. y ));
Cdialog: onlbuttondown (nflags, point );
}