VC + + play cool hover windows 2---irregular form implementation (general)

Source: Internet
Author: User

??

Goal
Irregular forms are also called shaped forms, like the bird-shaped form of thunder. This article is to implement such a form.
Planning & Solutions
Continue to expand as per the previous article. We have several scenarios to implement irregular forms. Thunder is to use a picture as the shape of the form, that

We also want to consider the type of picture. Now is the PNG picture of the world, with a transparent background; for BMP pictures, the program must be processed

A moment.
For regular graphics such as circles, you just need setwindowrgn to get it done. For complex graphics, you need to do it with a little bit of skill. Below with

Example to demonstrate irregular forms of round and complex graphics.
For SetWindowRgn, see the official documentation.

Practice
First, round form
Another class CRgn is used here, which, as the main parameter of SetWindowRgn, is initialized by the CreateEllipticRgn method.

Out of the circular area. Specific code such as:

    CRGN Rgn;    Rgn. CreateEllipticRgn (0,0,96,96);    SetWindowRgn (rgn,true);

Next, encapsulate a method to load the PNG image:

void Showpicture () {    CImage img;        HRESULT result = img. Load (_t ("prime.png"));     Enhanced transparency for    (int i = 0; i < img. GetWidth (); i++)      {for          (int j = 0; J < img. GetHeight (); J + +)          {              unsigned char* puccolor = reinterpret_cast<unsigned char *> (img. Getpixeladdress (i, J));              Puccolor[0] = puccolor[0] * puccolor[3]/255;              PUCCOLOR[1] = puccolor[1] * puccolor[3]/255;              PUCCOLOR[2] = puccolor[2] * puccolor[3]/255;          }      }         CDC *PDC = GETWINDOWDC ();    int NX = img. GetWidth ();    int NY = img. GetHeight ();    Img. Draw (pdc->m_hdc,0,0);     ReleaseDC (PDC);}

Ii. Complex Graphics form
The section "Creating a region from an image" in the article "Programmatic implementation of irregular forms in Windows" is referenced in Vckbase's articles.

Tip: Choose a color that is not in the picture as the background color (blue in the article), and the program will scan every pixel of the picture, if not

Background color, then create the region of this pixel in this position, then merge all such region to form a picture, as if

The background color is removed as the background is transparent.
Here again, a merge region API Combinergn:

int Combinergn (    crgn* pRgn1,    crgn* pRgn2,    int ncombinemode  );

Merge mode we choose to merge completely:
Rgn_or combines both regions in their entirety (union).
Examples are as follows:

void Cafoatwindowmessagedlg::setupregion (CDC *PDC,//dc of this window CBitmap &cbitmap, COLORREF Transcolo    R//the Color need remove) {CDC MEMDC;    Temporary DC Memdc.createcompatibledc (PDC);    CBitmap *poldmembmp=null;       Load bmp File Poldmembmp=memdc.selectobject (&AMP;CBITMAP); CRGN Wndrgn;     Window Region//window Region init wndrgn.createrectrgn (0,0,0,0);       BITMAP bit;    Cbitmap.getbitmap (&bit);//get the value of BMP file int y;        for (y=0;y<=bit.bmheight; y++) {CRgn rgntemp,//temporary region int iX = 0; Do {//skip Transcolor while (ix <= bit.bmwidth && Memdc.getpixel (ix, y) = = Trans            Color) ix++;            Record this point int ileftx = IX;                Skip None Transcolor while (ix <= bit.bmwidth && Memdc.getpixel (ix, y)! = Transcolor)            ++ix; Create Temporary "Region" Rgntemp.createrectrgn (Ileftx, y, IX, y+1);            Combine "region".                    Wndrgn.combinergn (&wndrgn, &rgntemp, rgn_or);        Delete temporary "region" Rgntemp.deleteobject ();        }while (IX <bit.bmwidth);    IX = 0;        } if (poldmembmp) memdc.selectobject (poldmembmp);    CWnd * pWnd = Pdc->getwindow ();        Pwnd->setwindowrgn (wndrgn,true);    Pwnd->setforegroundwindow (); }


SOURCE Here!
By contrast, the usual method produces aliasing, how to eliminate aliasing? Lead me to the following, using GDI + to implement the perfect irregular form.









VC + + play cool hover windows 2---irregular form implementation (general)

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.