I. Preamble
In the vast majority of Windows applications, their forms are rectangular forms that use normal moments, such as those we used, "Notepad," "Minesweeper," and so on. The rectangular form has the advantage of simple programming and simplicity, so it is enough to use in plain document applications and simple mini-games. However, in some entertainment game programs used in a slightly more rigid, when the use of irregular forms instead of the original rectangular form, will make such programs more interesting. Typical example is the Windows generation Media Player, which has a Control Panel option for the new version of Media Player, and the player appears with the selected panel shape, which is more than the previous version of media The old rectangular interface of the player is much more vivid and interesting. It's not too hard to implement irregular forms, and after you know the fundamentals, you can create a variety of interesting irregular forms.
Second, the realization principle
All Windows Forms are located in a range called region, and Windows automatically crops the form beyond the region range so that it is not visible if the size of the form is larger than the area. Therefore, there are two steps to creating an irregular form: the first step is to create an irregular "region". The second step is to place the form in the "region" you created.
The second step is simply to invoke a single statement. Call the API function SetWindowRgn in the SDK, the function prototype is as follows:
int SETWINDOWRGN (HWND hwnd, HRGN HRGN, BOOL Bredraw);
Where HWND is the form handle to be set, Hrgn is the "region" handle that has already been created, and Bredraw represents whether you want to redraw the form. In MFC, the member function of the window class CWnd is used int cwnd::setwindowrgn (HRGN HRGN, BOOL Bredraw), and the function has the same parameter meaning as the same name function in the API.
Relative to the second step, the first step in creating an irregular form is much more complex, and the more complex the irregular form, the more complex the process of creating its "region". Next we will introduce a variety of ways to create "region".
In MFC, the "region" object is implemented by the CRgn class. Almost every member function of CRGN has a corresponding SDK API function with the same name.
Iii. creation of simple "region"
Class CRgn a simple way to create a new "region" has the following member functions:
1.BOOL crgn::createrectrgn (int x1, int y1, int x2, int y2); Create a "region" of the rectangle.
2.BOOL crgn::createellipticrgn (int x1, int y1, int x2, int y2); Create a circle or ellipse "region".
3.BOOL crgn::createroundrectrgn (int x1, int y1, int x2, int y2, int x3, int y3); Creates a rounded rectangle "region".
4.BOOL Crgn::createpolygonrgn (lppoint lpPoints, int ncount, int nmode); Create a polygon "region".
Here is an example of creating an oval form, which describes how to create an ellipse form. In the CREATEELLIPTICRGN function that creates the ellipse "region", x1,y1 refers to the upper-left coordinate of the rectangle in which the ellipse is located, and X2,y2 refers to the lower-right coordinate of the rectangle.
The following code is added to the OnInitDialog function of the MFC dialog box program to turn the dialog box into an oval form:
BOOL Ctestdlg::oninitdialog () { cdialog::oninitdialog (); ... CRGN Rgn; Rgn. CreateEllipticRgn (0,0,max,+); SetWindowRgn (rgn,true);}
Figure One oval form
Four, the drawing path method creates "region"
The process for creating a "region" using this method is as follows:
The first step is to draw the form shape you want to create.
Some of the member functions in the CDC class are used in this step as follows:
BOOL Cdc::beginpath ();
After the function is called, the current device environment (DC) begins the process of tracing the drawing.
int int nbkmode);
Set the background mode for the drawing, in which Nbkmode must have a value of transparent. That is, the background does not change when you set the drawing.
BOOL Cdc::endpath ();
This function is called after the current device environment (DC) ends the process of tracing the drawing.
Call Beginpath before starting the drawing, and then call SetBkMode. Next, you can call the other graphing functions of the CDC, such as Arc,anglearc,lineto,moveto,roundrect,,textout and so on. The drawing is finished calling Endpath ().
The second step is to turn the plotted results into "region".
Use the SDK API functions in this step
HRGN pathtoregion (hdc hdc);
HDC is a handle to the drawing DC, and the M_HDC member variable in the CDC class can be passed in by this parameter. example, add the following code to a button click event to change the current form to the shape of the string "Hello"
voidctestdlg::ontest () {HRGN wndrgn; CCLIENTDC DC ( This); CFont Mfont; if(dc.m_hdc!=NULL) {VERIFY (Mfont.createfont ( $, -,0,0, Fw_heavy, TRUE, FALSE,0, Ansi_charset, Out_default_precis, Clip_default_precis, default_quality, DEF Ault_pitch| Ff_swiss,"Song Body")); //Start record form outline pathDC. Beginpath (); //set the background to transparent mode, this sentence is necessary. DC. SetBkMode (TRANSPARENT); CFont*Poldfont; Poldfont= DC. SelectObject (&Mfont); dc. TextOut (0,0,"Hello"); //End Record Form outline pathDC. SelectObject (Poldfont); dc. Endpath (); //convert the recorded path to the form outline handleWndrgn =::P athtoregion (DC.M_HDC); //give the form a specified outline shape This-SetWindowRgn (Wndrgn, TRUE); }}
CClientDC is a derived class for the CDC, so the class has member variables and member functions for all CDC classes.
Figure two Hello-shape forms
create "region" based on the image
This method is more complex to create irregular forms. First, prepare a picture with the shape of the target form, set the transparent color to the part of the middle of the picture that does not belong to the shape of the form, and mark it in the same color, such as blue RGB (0,0,255). The program loads the picture first. Then scan each pixel of the picture one by one, such as this pixel is not a transparent color, then create a "region" with a pixel in the corresponding location and then combine these small "region" to form a "region" of any shape. A member function to CRgn is used here:
1.
int
CRgn::CombineRgn( CRgn* pRgn1, CRgn* pRgn2,
int
nCombineMode );
The PRGN1,PRGN2 is the two "region" to be merged, the Ncombinemode is the way of merging, this application takes the Rgn_or, namely two "region" all merges the place to repeat the part. The code is implemented as follows:
voidsetupregion (CDC*PDC,//DC pointer to formCBitmap &cbitmap,//Bitmap object containing form shapeCOLORREF Transcolor//Transparent Color) {CDC MEMDC; //Create a temporary DC that is compatible with the incoming DCMemdc.createcompatibledc (PDC); CBitmap*poldmembmp=NULL; //to select a bitmap into a temporary DCPoldmembmp=memdc.selectobject (&CBitmap); CRGN Wndrgn; //Create a total form region with an initial area of 0Wndrgn.createrectrgn (0,0,0,0); BITMAP bit; Cbitmap.getbitmap (&bit);//to get the bitmap parameters, use the length and width of the position map inty; for(y=0; y<=bit.bmheight; y++.) {CRgn rgntemp;//Save temporary region intIX =0; Do { //skips the transparent color to find the next non-transparent color point. while(ix <= bit.bmwidth && Memdc.getpixel (ix, y) = =transcolor) IX++; //remember this starting point intILEFTX =IX; //looking for the next transparent color point while(ix <= bit.bmwidth && Memdc.getpixel (ix, y)! =Transcolor)++IX; //Create a temporary "region" that contains a starting point and a height of 1 pixels between the pointsRgntemp.createrectrgn (Ileftx, y, IX, y+1); //Merge to the main "region".Wndrgn.combinergn (&wndrgn, &rgntemp, Rgn_or); //Delete temporary "region" or the next time it is created and an errorRgntemp.deleteobject (); } while(IX GetWindow (); PWnd-SetWindowRgn (wndrgn,true); PWnd-SetForegroundWindow (); }
In the irregular form created by the preceding code, the bitmap is drawn in the OnEraseBkgnd event, and the form is identical to the bitmap shape.
Figure three forms created from a transparent color in bitmaps and bitmaps
Vi. Summary
Three ways to create "region", the first is the simplest, if the desired form shape is simple geometry, this method is most appropriate; the second is slightly more complicated, but the form is more complex; The third method creates any form shape that is drawn in the picture, but the implementation is the most complex.
Note: The writing of this article has been referred to the article "irregular form of different forms".
Programmatic implementation of irregular forms in "go" Windows