Implement irregular windows in Windows Programming

Source: Internet
Author: User
Tags transparent color textout

I. Preface

In most Windows applications, the formal positive moment rectangular forms are used, such as the commonly used "Notepad", "mine clearance", and so on. The rectangular form has the advantages of simple programming and concise style. Therefore, it is sufficient to use it in common document applications and simple games. However, in some entertainment game programs, it is a little dull to use. If an irregular form is used to replace the original rectangular form, this type of program will be more interesting. A typical example is Windows Media Player. The new version of media player has a control panel option. When these panels are selected, the player appears in the shape of the selected panel, in this case, the old rectangular interface of the Media Player is much more interesting than that of the previous versions. It is not too difficult to implement irregular forms. After you understand the basic principles, you can also create various interesting irregular forms.

  II. Implementation Principle

All windows forms are in a format called "region". If the size of the form exceeds the range of "region", Windows will automatically crop the form that exceeds the range of "region, make it invisible. Therefore, there are two steps to create an irregular form: the first step is to create an irregular "region". The second step is to place the form in the created "region.

The second step is to call a statement. Call the API function setjavaswrgn In the SDK. The prototype of this function is as follows:

Int setjavaswrgn (hwnd, hrgn, bool bredraw );

Hwnd is the form handle to be set, hrgn is the created "region" handle, and bredraw indicates whether to redraw the form. In MFC, use the int cwnd: setjavaswrgn (hrgn, bool bredraw) member function of the window class cwnd. The parameter meaning of this function is the same as that of the function of the same name in the API.

Compared with step 2, the first step of creating an irregular form is much more complicated, and the more complicated the irregular form is, the more complicated the process of creating its "region" is. Next, we will introduce various methods for creating "Region" in a simple way.

The "region" Object in MFC is implemented by the crgn class. Almost every crgn member function has an sdk api function with the same name.

  3. Create a simple "region"

A simple method for creating a new "region" with the crgn class includes the following member functions: bool crgn: createrectrgn (INT X1, int Y1, int X2, int Y2 ); create the "region" of the rectangle ".

Bool crgn: createellipticrgn (INT X1, int Y1, int X2, int Y2); Create a circular or elliptical "region ".
Bool crgn: createroundrectrgn (INT X1, int Y1, int X2, int Y2, int X3, int Y3); Create a rounded rectangle "region ".
Bool crgn: createpolygonrgn (lppoint lppoints, int ncount, int nmode); Create a polygon "region ".

The following example describes how to create an elliptical form. In the createellipticrgn function that creates the elliptical "region", X1 and Y1 indicate the coordinates in the upper left corner of the rectangle where the elliptic is located, and X2 and Y2 indicate the coordinates in the lower right corner of the rectangle.

The following code is added to the oninitdialog function of the MFC Dialog Box program. The dialog box can be changed to an elliptical form:

Bool ctestdlg: oninitdialog ()
{
Cdialog: oninitdialog ();
...
Crgn RGN;
RGN. createellipticrgn (0, 0, 200,100 );
Setjavaswrgn (RGN, true );
}


Figure 1 elliptical form

  Iv. Create a "region" using the plotting Path Method"

The process for creating a "region" using this method is as follows:

The first step is to draw the shape of the form to be created.
 
Some member functions in the CDC class used in this step are as follows: bool CDC: beginpath ();

After this function is called, the current device environment (DC) starts tracing the plotting process.

Int CDC: setbkmode (INT nbkmode );

Sets the background mode when drawing. In this application, the nbkmode value must be transparent. That is, the background does not change when you set the drawing.

Bool CDC: endpath ();

After this function is called, the current device environment (DC) ends the tracing drawing process.

Call beginpath and setbkmode before drawing. Next, you can call other drawing functions of the CDC, such as Arc, anglearc, lineto, moveTo, roundrect, and textout. Call endpath () after drawing ().

Step 2: Convert the drawn result to "region ".

Use sdk api functions in this step

Hrgn pathtoregion (HDC );

HDC is the handle of the graph DC. The m_hdc member variable in the CDC class can be used to input this parameter. For example, add the following code to a button and click the event to change the current form to the string "hello ".

Void ctestdlg: ontest ()
{
Hrgn wndrgn;
Cclientdc DC (this );
Cfont mfont;

If (DC. m_hdc! = NULL)
{
Verify (mfont. createfont (200, 50, 0, 0, fw_heavy, true, false, 0, ansi_charset, out_default_precis,
Clip_default_precis, default_quality, default_pitch | ff_swiss, ""));

// Start recording the outline path of the form
DC. beginpath ();

// Set the background to transparent mode, which is mandatory.
DC. setbkmode (transparent );

Cfont * poldfont;
Poldfont = Dc. SelectObject (& mfont );
DC. textout (0, 0, "hello ");

// End Record Form profile path
DC. SelectObject (poldfont );
DC. endpath ();

// Convert the recorded path to the form profile handle
Wndrgn =: pathtoregion (DC. m_hdc );

// Assign the specified profile shape to the form
This-> setjavaswrgn (wndrgn, true );
}
}

Cclientdc is the derived class of CDC. Therefore, this class has all member variables and member functions of the CDC class.


Figure 2 Hello form

  5. Create "region" based on Images"

This method is complex for creating irregular forms. First, prepare an image containing the shape of the target form, and set the transparent color to mark the portion of the image that does not belong to the shape of the form in the middle, for example, blue RGB (255 ). after running the program, load the image first. Scan each pixel of the image one by one. For example, this pixel is not transparent, create a "region" with only one pixel in the corresponding location and then combine these small "region" to form a "Region" in any shape ". here we will use a member function of crgn: int crgn: combinergn (crgn * prgn1, crgn * prgn2, int ncombinemode );

Prgn1 and prgn2 are the two "region" to be merged, and ncombinemode is the merge method. In this application, the "rgn_or" or "two" region "are all merged to the duplicate part. The code is implemented as follows:

Void setupregion (
CDC * PDC, // dc pointer of the form
Cbitmap & cbitmap, // bitmap object containing the form shape
Colorref transcolor // transparent color
)
{
CDC memdc;
// Create a temporary DC compatible with the incoming DC
Memdc. createcompatibledc (PDC );

Cbitmap * poldmembmp = NULL;
// Select the bitmap to the temporary DC
Poldmembmp = memdc. SelectObject (& cbitmap );

Crgn wndrgn;
// Create the overall form area. The initial region is 0.
Wndrgn. createrectrgn (0, 0, 0 );

Bitmap bit;
Cbitmap. getbitmap (& bit); // obtain the bitmap parameter. The length and width of the bitmap must be used here.

Int y;
For (y = 0; y <= bit. bmheight; y ++)
{
Crgn rgntemp; // Save the temporary Region

Int IX = 0;
Do
{
// Skip the transparent color to find the next non-transparent color.
While (IX <= bit. bmwidth & memdc. getpixel (IX, y) = transcolor)
IX ++;
// Remember the Starting Point
Int ileftx = IX;

// Find the next transparent dot
While (IX <= bit. bmwidth & memdc. getpixel (IX, Y )! = Transcolor)
++ IX;

// Create a temporary "region" with a height of 1 pixel between the start point and the focus"
Rgntemp. createrectrgn (ileftx, Y, IX, Y + 1 );

// Merge to the master "region ".
Wndrgn. combinergn (& wndrgn, & rgntemp, rgn_or );

// Delete the temporary "region". Otherwise, an error occurs during the next creation.
Rgntemp. deleteobject ();
} While (IX getwindow ();
Pwnd-> setjavaswrgn (wndrgn, true );
Pwnd-> setforegroundwindow ();
}

In the irregular form created by the above code, you can draw the bitmap in the onerasebkgnd event to obtain the form with the same shape as the bitmap.


Figure 3 the form created based on the transparent color in the bitmap and bitmap

  Vi. Summary

Three methods for creating "region": the first method is the simplest. This method is most suitable if the required form shape is a simple ry. The second method is slightly more complex, however, more forms are created. The third method can create any form shape drawn in the image, but the implementation complexity is also the highest.

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.