Use wtl to implement irregular windows

Source: Internet
Author: User

The irregular window is also called the "special-shaped" window. Now the game's starter interface is like the perfect "Xian", "pocket westward journey", and "jian Xia" in Jinshan, very beautiful. This article describes how to use wtl to implement a "special window ".

 

1. Create a dialog project with Wizard and set the "border" attribute of the window to none (that is, there is no title bar );

2. Load bitmap:

Bool cmaindlg: loadbitmap (uint nidresource) // nidresource: The resource ID of the bitmap.
{
// Load bitmap
M_bmbitmap = new cbitmap;
If (! M_bmbitmap-> loadbitmap (nidresource ))
Return false;

Bitmap bm;
M_bmbitmap-> getbitmap (& BM );
Int W = BM. bmwidth;
Int H = BM. bmheight;

 
M_bbitmapcreated = true;
M_bbitmapexists = true;

Return true;
}

 

3. Set the drawing area:

Void cmaindlg: make‑wrgn ()
{
// Window rect-client rect
Crect rcwnd;
Getwindowrect (rcwnd );

Crgn RGN;
RGN. createrectrgn (rcwnd. Left, rcwnd. Top, rcwnd. Right, rcwnd. Bottom );

Crect rcclient;
Getclientrect (rcclient );
Clienttoscreen (rcclient );

Crgn rgnclient;
Rgnclient. createrectrgn (rcclient. Left, rcclient. Top, rcclient. Right,
Rcclient. Bottom );


RGN. combinergn (hrgn (RGN), hrgn (rgnclient), rgn_xor );

// Obtain the bitmap DC
CDC dcimage;
Dcimage. createcompatibledc (null );

Hbitmap holdbmp = dcimage. selectbitmap (* m_bmbitmap );

// Obtain the width and height of the bitmap.

Bitmap bm;
M_bmbitmap-> getbitmap (& BM );

// Obtain the window width and height.
Crect RC;
Getclientrect (RC );

// Obtain the minimum width and height.
Int width = min (BM. bmwidth, RC. Width ());
Int Height = min (BM. bmheight, RC. Height ());

// RLE (run-length)Algorithm: Row_start is the first opaque pixel found. Once the transparent pixel is found, the always-line area is created. Then row_start becomes the next opaque pixel.
Int row_start;

// Traverse all rows
For (INT y = 0; y {
// Start from scratch

Row_start = 0;

Int X;
// Traverse all columns
For (x = 0; x <width; X ++)
{
// If the pixel is transparent
If (dcimage. getpixel (x, y) = m_coltrans) // m_coltrans: transparent (the color part in the bitmap is transparent)
{
// If no opaque pixel is found, continue searching

If (row_start = x) row_start ++;
Else
{

// You have found the start and end (x) of an opaque line segment and added it to the area to be drawn.
Crgn rgnadd;
Rgnadd. createrectrgn (row_start, Y, X, Y + 1 );
RGN. combinergn (hrgn (RGN), hrgn (rgnadd), rgn_or );
Row_start = x + 1;
}
}
}

// If the last pixel is still opaque, create another region.
If (row_start! = X)
{
Crgn rgnadd;
Rgnadd. createrectrgn (row_start, Y, X, Y + 1 );
RGN. combinergn (hrgn (RGN), hrgn (rgnadd), rgn_or );
}
}

Setjavaswrgn (RGN, true );
}

 

3. process Background Erasure
Message_handler (wm_erasebkgnd, onerasebkgnd)

 

Lresult cmaindlg: onerasebkgnd (uint umsg, wparam, lparam, bool & bhandled)
{
HDC = (HDC) wparam;

// Obtain the customer region of the window.

Crect RC;
Getclientrect (RC );

// Obtain the bitmap DC
CDC dcimage;
Dcimage. createcompatibledc (HDC );
Hbitmap holdbmp = dcimage. selectbitmap (* m_bmbitmap );

// Obtain the bitmap width and height.
Bitmap bm;
M_bmbitmap-> getbitmap (& BM );

// Use the minimum width and height

Int width = min (BM. bmwidth, RC. Width ());
Int Height = min (BM. bmheight, RC. Height ());

// Draw a bitmap as the window background
Bitblt (HDC, 0, 0, RC. Width (), RC. Height (), dcimage, 0, 0, srccopy );

Dcimage. selectbitmap (holdbmp );
Dcimage. deletedc ();


Return true;
}

 

4. left-click processing (used to drag the window)
Message_handler (wm_lbuttondown, onlbuttondown)

 

Lresult cmaindlg: onlbuttondown (uint umsg, wparam, lparam, bool & bhandled)
{
Cpoint Pt (lparam );

// Fool the window so that the mouse clicks the title bar
Postmessage (wm_nclbuttondown, htcaption, makelparam (Pt. X, Pt. y ));

Return 0;
}

Shows the effect:

 

 

 

Note: the background of a randomly selected image is not pure black, so there will be burrs on the edge. You can solve this problem by clicking Photoshop.

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.