Designing the wizard form with Visual C + +

Source: Internet
Author: User
Tags transparent color resource

The Windows user interface is increasingly becoming an industry standard as Microsoft relies on the tremendous success that Windows has made on the operating system. Unified interface to the vast number of users of the application of software learning and use has brought great convenience. But every day face the same face, last will inevitably produce some boredom, want to develop some "deviant" application, if you can change Windows Uniform "standard" interface, will give users a sense of freshness. The standard Windows application window is typically a light gray rectangular appearance with a title bar, so the "Alien" dialog box/window is mainly color and shape on the foot. This example implements a "wizard" window, and the interface effect after the program is compiled is as shown in Figure one:

Figure one, transparent "wizard" form superimposed on Visual C + + development tools

One, the realization method

In general, the implementation of special-shaped form is mainly done in two aspects of the work, one is to change the background color, the second is to change the window shape. Changing the window background color is the simplest way to change the appearance of a Windows application, and there are generally two ways to create and manage Windows. When you are working with a WM_CTLCOLOR message, when you need to repaint a window or dialog (or a child control of a dialog), Windows sends a message to the application Wm_ctlcolor, the application processes the WM_CTLCOLOR message, and returns a brush handle to paint the background of the form. Another is in response to Windows WM_ERASEBKGND message, Windows sends a WM_ERASEBKGND message to the window to erase the background, you can use VC + + ClassWizard overloads the default handler for this message to erase the background (actually with a brush) and returns True to prevent Windows from erasing windows. For changing the form's shape, you can limit the painting and mouse messages to a specified area of the window by using the new SDK function, SetWindowRgn (), so that the window becomes the specified irregular shape (area shape). Area is a powerful mechanism in Windows GDI where the area is a piece of space on the device, can be any shape, and a complex area can be grouped by small areas. Windows contains zone creation functions CreateRectRgn (), Createpolyrgn (), CreatePolygonRgn (), CreateRoundRectRgn (), and CreateEllipticRgn (). by Combinergn () to combine the region, the complex shape of the region can be obtained and the window shape of the complex shape is obtained.

Through the above method although can get "abnormity" window, but feel the color monotonous, the shape is not enough "cool", can get Cooler "alien" window? The answer is yes. Here's how to create an "alien" window with bitmaps and masks. The "Wizard" effect implemented by this example is achieved by this method.

Using a bitmap to create an irregular form is based on the color of the pixel for the "button image" processing, all the color pixel regions are not specified area combinations. By using this technique, you are actually implementing a bitmap background for a dialog box/window and transparently handling the specified color area. The following is an example of a form with a transparent bitmap as the background:

First of all, using drawing software such as Photoshop to edit a picture of the program background and the corresponding mask picture of the picture, save in BMP format, the next step is to use the Visual C + + resource Editor to introduce the background picture and mask picture file, set its ID idb_show and idb_ MASK. It should be explained that although the resource editor for the Visual C + + integrated development environment can only edit bitmaps that are no more than 16 colors, we can store them in true color without ignoring the warnings of Visual C + +.

With the above work, the rest of the core work is based on the background bitmap and mask bitmap to determine the final display of the bitmap area, that is, the "deduction" of the area will be displayed in a transparent effect. The following code implements this functionality:

/////////////////////////////////////////////////////////////////////////////
Get form Rectangle
CRect Rectwnd;
This->getwindowrect (Rectwnd);
Reading the Mask bitmap resource
CBitmap Mybitmap,*poldbitmap;
Mybitmap.loadbitmap (Nmaskid);
To create a "memory consistent" device
CDC MEMDC;
Memdc.createcompatibledc (PDC);
Select a drawing device
Poldbitmap = Memdc.selectobject (&mybitmap);
Create the initial area of a form
CRgn rgnwnd,rgntemp;
Rgnwnd.createrectrgn (0,0,rectwnd.width (), Rectwnd.height ());
int nwidth,nheight;
COLORREF color;
The following two-layer cycle is to check the background bitmap pixel color, transparent area processing;
When the pixel color is the specified transparent value, the point is trimmed out of the area.
for (nwidth = 0;nwidth <= rectwnd.width () -1;nwidth++)
{
for (nheight = 0;nheight <= rectwnd.height (); nheight++)
{
color = Memdc.getpixel (nwidth,nheight);
When the pixel is white, remove the point
if (color = = RGB (255,255,255))
{
Pixel color for the specified transparent color, create a transparent "micro-region"
Rgntemp.createrectrgn (nwidth,nheight,nwidth+1,nheight+1);
"Snap", "deduct" the transparent "micro-region" from the complete area
Rgnwnd.combinergn (&rgnwnd,&rgntemp,rgn_xor);
Remove the transparent "micro-zone" that you just created, freeing system resources
Rgntemp.deleteobject ();
}
}
}
Memdc.selectobject (POLDBITMAP);
SetWindowRgn ((HRGN) rgnwnd,true); Use the display area of the final setting window as the specified range

In order to finally display the transparency effect of the form, you also need to reset the system default background erase operation, that is, add WM_ERASEBKGND message processing, in which the background bitmap display function, this step can be achieved with the help of ClassWizard.

Finally, it should be noted that in order to enable the abnormal form of the application can be normal to remove and exit, you need to deal with the form of Wm_nchittest, WM_CHAR, and other information, this part of the book in the other examples have been introduced, here is no longer to repeat, the interest of the reader friend can refer to

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.