MFC how to draw irregular forms

Source: Internet
Author: User
Tags bool

The example in this article describes how MFC draws irregular forms. Share to everyone for your reference. The specific analysis is as follows:

Implementation process:

1, first create an MFC application based on DLG, named: Trybgdlg, and set the Dlg property to: Title Bar:false, other settings unchanged

2, the production of two images, a black-and-white image, is based on the appearance of the player to make, where the white area is to retain the final display on the desktop area. Add these two images to the project, the first ID number is set to Idb_interface, and the second ID number is set to: Idb_mask

3. Add a function in the Ctrybgdlg class:

Function Description: CBitmap is the mask position variable to be passed in, this refers to the object created by Idb_mask, Transcolor is the RGB value to be set as a transparent phase

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the

4. Add the following code to the bool Ctrybgdlg::oninitdialog () function:

?

1 2 3 CBitmap bmp; Bmp. LOADBITMAPW (Idb_mask); This->setupregion (THIS->GETWINDOWDC (), Bmp,rgb (0,0,0));

5, add the response to the WM_ERASEBKGND message and add the following code in the bool Ctrybgdlg::onerasebkgnd (cdc* PDC) function

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 BOOL ctrybgdlg::onerasebkgnd (cdc* PDC) {//TODO: Add Message Handler code here and/or invoke default value CRect rect; this->getwindowrect (&rect); CDC MEMDC; CBitmap bmp; CBitmap *poldbmp=null; Bmp. LOADBITMAPW (Idb_interface); Memdc.createcompatibledc (PDC); Poldbmp=memdc.selectobject (&bmp); Pdc->bitblt (0,0,rect. Width (), Rect. Height (), &memdc,0,0,srccopy); if (poldbmp) {memdc.selectobject (poldbmp);} return true; Return Cdialog::onerasebkgnd (PDC); }

This enables the creation of irregular forms, as shown at the beginning of the created view.

6, generally we also want to implement the form of the operation, the implementation of the following methods:

Add a response to the WM_NCHITTEST message and add the following code to the generated Lresult Ctrybgdlg::onnchittest (CPoint point) function:

?

1 2 3 4 5 6 7 8 9 Lresult Ctrybgdlg::onnchittest (CPoint point) {//TODO: Add Message Handler code here and/or invoke default value CRect RC; GetClientRect (&RC); ClientToScreen (&RC); Return RC. PtInRect (point)? Htcaption:cdialog::onnchittest (point); Return Cdialog::onnchittest (point); }

This is fully implemented, with the creation of irregular forms and the response to form-holding messages.

The following detailed explanation of the specific implementation principle and part of the Code analysis:

General principle: The principle of this program is to use the idb_mask image to calculate the outline of the form to be set, and then use the SetWindowRgn () function to change it. Finally, in response to the WM_ERASEBKGND message when the form is redrawn, paste the form background picture idb_interface onto the form.

The principle of using idb_mask image to calculate form contour:

The code that calculates the outline of the form is mainly implemented by the setupregion () function, which, considering the irregularity of the form, should be described by a mask bitmap, and for this example the white area is the contour area of the irregular form to be preserved. This code first creates an empty area with Crrgn.createrectrgn (0, 0, 0, 0), and then enumerates the pixel information for the idb_mask image, calculates the area that is not transparent in each column, and then merges with Crrgn, So the final crrgn is the area to be set.

The core code is:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 CRgn crrgn, crrgntmp;//Create an empty area crrgn.createrectrgn (0, 0, 0, 0); int IX = 0;int IY = 0; for (IY = 0; IY < bit.bmheight; iy++) {doing {//skip over transparent pixels the at start of lines.//In a phase of the unit, found in this one-element column, the first A point IX that is not to be set as a transparent phase. And then find the beginning of this IX, in this one phase Solezheng finally with his near the last one is not transparent point. They are then merged into the Crrgn. while (ix <= bit.bmwidth && Memdc.getpixel (ix, IY) = = Transcolor) ix++;//in IY and Iy+1, the first x-coordinate int Soliezang with a point not set to transparent FTX = ix;//Save the coordinates of this point while (ix <= bit.bmwidth && Memdc.getpixel (ix, IY)!= Transcolor) ++ix;//This is to find the most imminent opaque x-coordinate in IX Crrgntmp.createrectrgn (ILEFTX, IY, IX, iy+1); The four dots are now just found. Opaque area/merge area crrgn.combinergn (&crrgn, & Crrgntmp, Rgn_or); Remember eventually to manually delete the Crrgntmp object Crrgntmp.deleteobject (); }while (IX < bit.bmwidth); If IX does not reach the end of the picture, and the line is not yet enumerated, then the next round//Enumeration IX = 0 is on the iy and iy+1 line; }

I hope this article will help you with your MFC programming.

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.