I. Overview The Application of GDI + makes it easier to program graphic images on a plane. This article uses a dialog box-based clock program as an example to call * In vc6.0 *. PNG Image implements a translucent gradient window. The program implements two clock display modes: pointer and digital. The window implements a translucent gradient window, the window is dragged without moving a rectangular box, And the taskbar form button is hidden. As follows: Figure 1 Background of Windows XP desktop after the program is executed 2. Preparations 1. Prepare image resources. First, edit the background, hour, minute-hand, and digital clock display modes of the clock in Photoshop. For example, save the pictures as .png with transparent channels (the background can be adjusted transparently when you call the display mode ). In this way, the image resources in the program are ready. 2. The following is the basic preparation for the project 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 to it, that is, create the following path, so that the sample code can be compiled smoothly (of course, you can put it wherever you like, you only need to correctly include the path in your project !). C:/GDI+/IncludesC:/GDI+/LibC:/GDI+/gdiplus.dll (3) Add the settings for the GDI + Environment in stdafx. h. # Define Unicode # ifndef ulong_ptr # define ulong_ptr unsigned long * # endif # include "C:/GDI +/Includes/gdiplus. H "// modify your header file path to using namespace gdiplus; # pragma comment (Lib," C: // GDI ++ // lib // gdiplus. lib ") // modify it to your. lib file path (4) In gdipclock. cpp, edit the initinstance () of the app and add the following code to initialize the GDI +. Gdiplusstartupinput; ulong_ptr gdiplustoken; gdiplusstartup (& gdiplustoken, & gdiplusstartupinput, null );...... // after the completion of the dialog box, // close the gdiplusshutdown (gdiplustoken) environment of gdiplus ); Iii. Whole Process of Program Implementation 1. Create a dialog box-based project named gdipclock 2. Define all class member variables in gdipclockdlg. H, including the pointer and the length and width of all images. Image *m_pImageClock; Image *m_pImageClock1; Image *m_pImageHHour; Image *m_pImageHMinu; Image *m_pImageHSec; Image *m_pImageNum; int m_BakWidth , m_BakHeight ; int m_HourWidth, m_HourHeight; int m_MinuWidth , m_MinuHeight; int m_SecWidth , m_SecHeight ; 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. 3. In the oncreate () dialog box, add the following code: Initialize function 2 and member variables! (The imagefromidresource () function is a method for loading PNG images from resources !) Int cgdipclockdlg: oncreate (maid) {If (cdialog: oncreate (maid) =-1) Return-1; hfuncinst = loadlibrary ("user32.dll "); bool Bret = false; If (hfuncinst) updatelayeredwindow = (myfunc) getprocaddress (hfuncinst, "updatelayeredwindow"); else {afxmessagebox ("user32.dll error! "); Exit (0);} // initialize the gdiplus environment // initialize GDI +. m_blend.blendop = 0; // theonlyblendopdefinedinwindows2000 m_blend.blendflags = 0; // nothingelseisspecial... m_blend.alphaformat = 1 ;//... m_blend.sourceconstantalpha = 255; // The ac_src_alpha // PNG image is added to the resource under "PNG": So it can be called from the resource, // here, the image does not provide bytes to call the function of the image in the resource. // imagefromidresource () is to pass the image pointer of the image to the pointer application by the Resource Name "PNG" and resource ID.. Imagefromidresource (optional, "PNG", optional); imagefromidresource (idr_pngnum, "PNG", m_pimagenum); imagefromidresource (optional, "PNG", m_pimageclock); imagefromidresource (optional, "PNG", callback); imagefromidresource (idr_pngmin, "PNG", callback); imagefromidresource (idr_pngsec, "PNG", m_pimagehsec); m_bakwidth = m_pimageclock-> getwidth (); m_bakheight = m_pimageclock-> getheight (); m_hourwidth = m_pimagehhour-> getwidth (); m_hourheight = distance-> getheight (); m_minuwidth = m_pimagehminu-> getwidth (); m_minuheight = margin-> getheight (); m_secwidth = m_pimagehsec-> getwidth (); m_secheight = m_pimagehsec-> getheight ();: setwindowpos (m_hwnd, distance, 0, 0, m_bakwidth, m_bakheight, swp_nosize | swp_nomove); Return 0 ;} 4. Add the following code in oninitdialog () to initialize the call transparent form and set the clock to refresh. The code indicates the following: // Update the window style as transparent form updateclockdisplay (); settimer (1,500, null) immediately after startup; // remove the corresponding buttons of the taskbar window modifystyleex (ws_ex_appwindow, ws_ex_toolwindow); void cgdipclockdlg :: ontimer (uint nidevent) {// todo: add your message handler code here and/or call default updateclockdisplay (); cdialog: ontimer (nidevent );} 5. When a transparent form is created and refreshed, the following functions are called. The function parameters indicate the transparency of the entire form. This function includes the use of the centralized overload mode of the image. drawimage () function in GDI +, and a preliminary study on the use of the image transformation matrix in GDI +. Bool temperature: updateclockdisplay (INT transparent) {HDC hdctemp = getdc ()-> m_hdc; temperature = createcompatibledc (hdctemp); hbitmap = temperature (hdctemp, m_bakwidth, m_bakheight ); selectObject (m_hdcmemory, hbitmap); If (transparent <0 | transparent> 100) Transparent = 100; m_blend.sourceconstantalpha = int (transparent * 2.55); HDC hdcscreen = :: getdc (m_hwnd); rect CTS; getwindowrect (& RDBMS); PO Int 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; bfly? Graph. drawimage (m_pimageclock, points, 3): Graph. drawimage (m_pimageclock1, points, 3); bfly =! Bfly; int oxyx = 140; // m_bakwidth/2 + 8; int oxyy = 90; // m_bakheight/2 + 10; systemtime; // address of system time structure getlocaltime (& systemtime); // defines a matrix of units. The coordinate origin is in the center of the dial matrix matrixh (, oxyx, oxyy ); // The angle of the clockwise rotation matrixh. rotate (systemtime. whour * 30 + systemtime. wminute/2.0-180); point pointsh [] = {point (0, 0), point (m_hourwidth, 0), point (0, m_hourheight)}; matrixh. translate (-m_hourwidth/2 ,- M_hourheight/6); // use this matrix to convert points matrixh. transformpoints (pointsh, 3); graph. drawimage (m_pimagehhour, pointsh, 3); // defines a matrix of units. The coordinate origin is in the center of the dial matrix matrixm (, oxyx, oxyy ); // matrixm. rotate (systemtime. wminute * 6-180); point pointsm [] = {point (0, 0), point (m_minuwidth, 0), point (0, m_minuheight)}; matrixm. translate (-m_minuwidth/2,-m_minuheight/6); // use this matrix to convert pointsm matrixm. transformpoints (PO Intsm, 3); graph. drawimage (m_pimagehminu, pointsm, 3); // defines a matrix of units. The coordinate origin is in the matrix (, oxyx, oxyy) in the center of the dial ); // second-hand rotation angle matrix. rotate (systemtime. wsecond * 6-180); point pointss [] = {point (0, 0), point (m_secwidth, 0), point (0, m_secheight)}; matrix. translate (-m_secwidth/2,-m_secheight/7); // use this matrix to convert pointss matrix. transformpoints (pointss, 3); graph. drawimage (m_pimagehsec, pointss, 3); // hh: mm: SS // this letter Cut the pixel draw in the specified rect from m_pimageclock to the specified position graph. drawimage (m_pimagenum, 0, 0, 14 * (systemtime. whour/10), 23, unitpixel); // This function cut the pixel draw in the specified rect from m_pimageclock to the specified graph. drawimage (m_pimagenum, 20, 0, 14 * (systemtime. whour % 10), 23, unitpixel); // This function cut the pixel draw in the specified rect from m_pimageclock to the specified graph. drawimage (m_pimagenum, 20*140, 23, unitpixel); // This function cut the pixel draw in the specified rect from m_pimageclock to the specified position graph. drawimage (M_pimagenum, 20*3, 0, 14 * (systemtime. wminute/10), 23, unitpixel); // This function cut the pixel draw in the specified rect from m_pimageclock to the specified position graph. drawimage (m_pimagenum, 20 *, 0, 14 * (systemtime. wminute % 10), 23, unitpixel); // This function cut the pixel draw in the specified rect from m_pimageclock to the specified position graph. drawimage (m_pimagenum, 20*140, 23, unitpixel); // This function cut the pixel draw in the specified rect from m_pimageclock to the specified position graph. drawimage (m_pimagenum, 20*6, 0, 14 * (systemtime. wsecond /10), 23, unitpixel); // This function cut the pixel draw in the specified rect from m_pimageclock to the specified position graph. drawimage (m_pimagenum, 20*7, 0, 14 * (systemtime. wsecond % 10), 0, 14, 23, unitpixel); 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 ^ 0x80000); bool Bret = false; Bret = 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; return Bret;} bool cgdipclockdlg: Im Agefromidresource (uint NID, lpctstr STR, image * & pimg) {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);} void cgdipclockdlg: onlbuttondown (uint nflags, cpoint point) {// disable the display of the mobile rectangular form box: systemparametersinfo (spi_setdragfullwindows, true, null, 0); // move the entire window sendmessage (wm_syscommand, 0xf012, 0); // postmessage (wm_nclbuttondown, htcaption, makelparam (point. x, point. y); cdialog: onlbuttondown (nflags, point );} |