Create a dialog box with PNG files

Source: Internet
Author: User
Today's software is very popular with distinctive forms, such as program startup screens. The most representative is the startup screens of various products in Adobe Creative Suite, the special form interface adds a lot of colors to your application based on its appearance ~~ It is not difficult to implement a special form in your application. We can use the GDI + technology to easily implement it. The following describes the specific operation steps for the large M. The principle of creating a special-shaped dialog box is to use a PNG file with an alpha channel to re-render the dialog box as the file looks like, so before getting started, we need to first create a PNG file using Photoshop or similar image processing software and save it in PNG-24 mode, which retains alpha channel information within the file. Next, let's start program creation. The following steps are completed in Visual C ++. NET 2003. The steps are also applicable to Visual C ++ 6.0. First, we start with a dialog box-based MFC application. Because we need to apply the GDI + technology here, we need to first connect to the GDI + SDK library in the program, first open atdafx. add the following code to the H file:

 

# Include <gdiplus. h>
Using namespace gdiplus;

 

Right-click the project icon in Solution Explorer, select Properties From the shortcut menu, open the properties page of the project, and select configure as all configurations, set connector> enter the value of the additional dependency to gdiplus. lib to connect the library file of GDI + to the program.

 

Next, we will create a resource for the dialog box to be displayed. you can insert a dialog in the resource manager and delete all the controls in the dialog box, because we do not need it here.

 

Then, right-click the dialog box and choose add class from the shortcut menu.

 

 

Here we create a name for the newly added class, and the content is free of choice, as long as you remember it later, the base class must select cdialog, and click Finish.

 

 

Next we will modify this class to implement the functions we need.

 

First, declare the following content in the Class header file:

 

Cbitmap m_bmp dialog;
CDC dcmemory;
CDC * m_screendc;

 

Void updateview (cstring pngfilename, int width, int height );


Void doupdatedummydialog (cstring pngfilename, cwnd & WND, cbitmap & BMP, byte sourceconstantalpha );

Hbitmap converbmp (hbitmap );

 

It contains three variables and three methods. The specific implementation of the method is as follows:

 

Void cpngdlg: updateview (cstring pngfilename, int width, int height)
{
Hbitmap;
Hbitmap = createcompatiblebitmap (this-> getdc ()-> m_hdc, width, height );
Hbitmap = converbmp (hbitmap );
M_bmp dialog.deleteobject ();
M_bmp dialog.attach (hbitmap );

Doupdatedummydialog (pngfilename, * This, m_bmp dialog, 255 );
}

 

Void cpngdlg: doupdatedummydialog (cstring pngfilename, cwnd & WND, cbitmap & BMP, byte sourceconstantalpha)
{
Cbitmap * poldbitmap = dcmemory. SelectObject (& BMP );
Bitmap BMP Info;
BMP. getbitmap (& BMP info );
Crect rectdlg;
WND. getwindowrect (rectdlg );
Cpoint ptwindowscreenposition (rectdlg. topleft ());
Csize szwindow (BMP info. bmwidth, BMP info. bmheight );
Blendfunction blendpixelfunction = {ac_src_over, 0, sourceconstantalpha, 0x01 };
Cpoint ptsrc (0, 0 );
 
Graphics graphics (dcmemory. m_hdc );

//////////////////////////////////////// //////////////////////////////////

Bitmap * m_bit = new Bitmap (pngfilename. allocsysstring ());
Graphics. drawimage (m_bit, 0, 0 );
Delete m_bit;

//////////////////////////////////////// //////////////////////////////////

Graphics. releasehdc (dcmemory. m_hdc );
 
Hinstance hinst =: loadlibrary ("user32.dll ");
If (hinst)
{
Typedef bool (winapi * myfunc) (hwnd, HDC, point *, size *, HDC, point *, colorref, blendfunction *, DWORD );
Myfunc fun = NULL;

Fun = (myfunc) getprocaddress (hinst, "updatelayeredwindow ");
If (fun)
Fun (WND, m_screendc-> m_hdc, & ptwindowscreenposition, & szwindow, dcmemory, & ptsrc, 0, & blendpixelfunction, 0x02 );

Freelibrary (hinst );
}

Dcmemory. SelectObject (poldbitmap );
}

 

Hbitmap cpngdlg: converbmp (hbitmap)
{
HDC;
Word wbitcount = 32;
DWORD dwpalettesize = 0, dwbmbitssize = 0, dwdibsize = 0, dwwritten = 0;
Bitmap bitmap;
Bitmapinfoheader Bi;
Lpbitmapinfoheader lpbi;
Handle hpal, holdpal = NULL;
Hbitmap hdib;

GetObject (hbitmap, sizeof (Bitmap), (lpstr) & Bitmap );
Bi. bisize = sizeof (bitmapinfoheader );
Bi. biwidth = bitmap. bmwidth;
Bi. biheight = bitmap. bmheight;
Bi. biplanes = 1;
Bi. bibitcount = wbitcount;
Bi. bicompression = bi_rgb;
Bi. bisizeimage = 0;
Bi. bixpelspermeter = 0;
Bi. biypelspermeter = 0;
Bi. biclrimportant = 0;
Bi. biclrused = 0;

Dwbmbitssize = (bitmap. bmwidth * wbitcount + 31)/32) * 4 * bitmap. bmheight;

Hpal = getstockobject (default_palette );
If (hpal)
{
HDC =: getdc (null );
Holdpal =: selectpalette (HDC, (hpalette) hpal, false );
Realizepalette (HDC );
}

Hdib = createdibsection (HDC, (bitmapinfo *) & BI, dib_rgb_colors, (lpvoid *) & lpbi, null, 0 );
Getdibits (HDC, hbitmap, 0, (uint) bitmap. bmheight, (lpstr) lpbi + dwpalettesize, (lpbitmapinfo) & BI, dib_rgb_colors );
If (holdpal)
{
: Selectpalette (HDC, (hpalette) holdpal, true );
Realizepalette (HDC );
: Releasedc (null, HDC );
}

Closehandle (hpal );
Closehandle (holdpal );
Deleteobject (hbitmap );

Return hdib;
}

 

Finally, we add the initialization code in the oninitdialog () method of the dialog box:

M_screendc = new CDC ();
M_screendc-> attach (: getdc (null ));
Dcmemory. createcompatibledc (m_screendc );
Modifystyleex (0, 0, x 80000 );

 

Cstring m_apppath;
Char szpathname [max_path];
: Getmodulefilename (null, szpathname, sizeof (szpathname ));
M_apppath = cstring (szpathname). Left (cstring (szpathname). reversefind ('//') + 1 );

Updateview (m_apppath + "test.png", 400,300 );

 

The Code (orange part) shows that we instruct the program to load the test.png file as the appearance file of the dialog box, followed by the width and height of the image. Finally, we can map the wm_nchittest message in the dialog box and return the htcaption value. Then we can drag the dialog box with the mouse. The Code is as follows:

Uint cpngdlg: onnchittest (cpoint point)
{
// Todo: add the message processing program code and/or call the default value here
Return htcaption;
// Return cdialog: onnchittest (point );
}

 

Finally, create a button in the original dialog box of the program and add the Code:

Cpngdlg * m_pngdlg = new cpngdlg ();
M_pngdlg-> domodal ();

The purpose is to click the button to display the new dialog box. OK, so that our program is finished. You can see the effect shown in the figure after compiling and running. In this case, you can drag the dialog box with your mouse to view the effect of the transparent form ~~ Note the following points: 1. this article does not explain in detail how the program runs. If you have any hope, you can leave a message for me. I will explain it in detail. If you need the source code, you can also contact me. 2. because this program needs to call the updatelayeredwindow function in user32.dll, this function is only included in the user32.dll file of Windows 2000 and later versions, therefore, the program we developed can be displayed normally only when it runs on Windows 2000 or a later version (including XP/2003/Vista.

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.