In the front, I read many good articles in this site countless master, benefiting is not shallow, first pay tribute to you! Today, there is a small job, I don't dare to enjoy it, I will share it with you, I hope to help some friends.
The problem of transparent forms believe that everyone is already familiar. In the previous previous online magazine, there are several detailed tutorials. Summary is to achieve through the setWindowRGN function. The specific crop frame is generated, relatively simple , The CRGN classes such as ellipse, rounded window provide corresponding way, and if we want to generate the friends in front of your own picture to generate a cutting box, the method mentioned in front of a rectangular cut box, then scan the image, according to The color of the pixel point is or not, the color match is deleted, and the cut box is deducted (generate a new, then xor), I will not be described in detail in this way, and the friends who need it please check the previous Article, I first said that I have encountered the deficiencies:
If my form supports Resize, then in the process of adjusting the size, keep calculating the crop box (to scan the pixels by point, and operate the cut box), the amount of calculation is quite large, especially when the form This is even more likely to cause the flash of the form.
I check the relevant information to get another implementation method, simple and practical, that is, using the setlayeredWindowAttributes function, I believe that many friends have seen Microsoft to the description of him but have used it, to use it, to install the latest SDK, Otherwise there is no defined error. The younger brother is too lazy to download, the following introduction uses the format of the general API call. If you already have the latest SDK, then your program can become more concise!
First introduce this function:
Bool setlayeredWindowAttributes
HWND HWND, // Apply the handle of the target window
ColorRef crKey, // Mask color, can be specified by RGB (R, G, B)
BYTE BALPHA, / / Mask color part of the alpha value, 0 is transparent, 255 is completely opaque
DWORD DWFLAGS / / Transparent way
);
What is going to be said is that this function is only supported in Windows2000 and above. MSDN's description is as follows
Windows NT / 2000 / XP: Included in Windows 2000 and Later.
Windows 95/98 / me: unsupported.
Header: declared in winuser.h; include windows.h.
Library: USE User32.lib.
There is also this function to support the form of the title box. It is just that it is just a customer area. It is well used in the case where we want to make transparent forms. . (I aim to illustrate the idea of such transparent forms and functions, so code is very simple, and there is no necessary error verification mechanism, I hope everyone can understand)
Establish a background picture for drawing on the form, tagged the part to cut out with a color, we call it Maskcolor, my picture is as follows: My maskcolor = 0xff00, that is, RGB (0,255,0 ). Establish a dialog-based project, modify the properties of dialog resources, mainly modify two places. First, specify that there is no titlebar, two is to specify borderstyle to none. This can guarantee that the form is in line with your request to add the picture to the resource, pay the ID = IDB_BACKGROUND, start writing code, huh, huh, look at the code below. Is it a bit big, don't worry, these are mostly the engineering guide automatically generated, I have already added it, and it is not a few lines with yellow. Otherwise, how do I dare to boast this implementation simply? a. First we add two member variables to the form: cbitmap * m_oldbitmap; // Point to the original Bitmap in memory DC
CDC m_dc; // Memory DC for storing background images
b. Do some initialization in the oninitdialog () function of the form: BOOL CTRANSWINDOWDLG :: OnItDialog ()
{
CDIALOG :: OnInitdialog ();
/ / Set the icon of this dialog. When the application main window is not a dialog, the framework will automatically
/ / Do this
Seticon (M_HICON, TRUE); // Set large icon
Seticon (M_HICON, FALSE); // Set small icon
///
// segment add code added
// Implement background diagram and window transparency
// call background picture
CBitmap Bitmap;
Bitmap bitinfo;
Bitmap.LoadBitmap (IDB_BACKGROUND);
/ / Get the size of the picture and adjust the window size to adapt to pictures
Bitmap.getBitmap (& BitInfo);
CRECT RECT;
GetWindowRect (& Re);
Rect.right = Rect.Left BitInfo.Bmwidth;
Rect.bottom = Rect.top bitinfo.bmheight;
MoveWindow (RECT);
// Create and save DC
M_dc.createcompatibledc (getDC ());
m_oldbitmap = m_dc.selectObject (& Bitmap);
// Set the window mask color and mode
// First get the mask color
ColorRef MaskColor = m_dc.getpixel (0,0);
#define lwa_colorKey 0x00000001
#define WS_EX_LAYERED 0X00080000
Typedef Bool (WinApi * lpfnsetlayeredWindowAttributes) (HWND HWND,
ColorRef CRKEY,
BYTE BALPHA,
DWORD DWFLAGS;
LPFNSetlayeredWindowAttributes setlayeredWindowAttribute;
HModule huser32 = getModuleHandle ("User32.dll");
SetlayeredWindowAttributes = (lpfnsetlayeredWindowAttributes) getProcaddress (HUSER32,
"SetlayeredWindowAttributes");
Setwindowlong (getsafehwnd (),
GWL_EXSTYLE,
GetWindowlong (GetsafehWnd (), GWL_EXSTYLE | WS_EX_LAYERED);
SetlayeredWindowAttributes (GetsafehWnd (),
Maskcolor,
255,
LWA_COLORKEY);
Freelibrary (huser32);
Return True; // Returns true unless the focus of the control is set, otherwise returns TRUE
}
As with the comment, we first put the picture load in, then create a DC compatible with the window DC, and bind the picture that I came in to the memory DC, and used the original Bitmap under m_oldbitmap, the user final freed. c. Add code to ONPAINT to draw background images onto the window: void ctranswindowdlg :: onpaint ()
{
IF (Isiconic ())
{
/ / Here is the frame code of the MFC, in order to reduce the space omitted ...
}
Else
{
// Segment clot modified code, used to draw background pictures
// cdialog :: onpaint ();
CDC * PDC = this-> getdc ();
CRECT RECT;
GetWindowRect (& Re);
PDC-> Bitblt (0, 0, Rect.width (), Rect.height (), & M_DC, 0, 0, SRCCopy;
}
}
d. The function we want here is already able to be implemented, but good programmers should never forget to release resources. You are the same, you must not forget to release resources at the end of the program, huh, we can write the destructor We can also put it in the onclose () function, all, I use the latter: add a function and add the release resource code void ctranswindowdlg :: onClose ()
{
// segment add code added
/ / Release Resources
CBITMAP * bitmap = m_dc.selectObject (m_oldbitmap);
m_dc.deletedc ();
Bitmap-> deleteObject ();
CDIALOG :: OnClose ();
}
e. Now, run, really cool, so a few lines of code get a beautiful window. Is it a sense of accomplishment? Ha ha. I have seen it a few times really and savage. I don't find the problem, how do you start when you run? Oh, I painted it when I clear the background, I didn't matter, let us kill it. Add the response function of the WM_ERASEBKGND event, return the original comment to True, then go to see it? How, is it satisfied? Bool CTranswindowdlg :: OneRaseBkGnd (CDC * PDC)
{
// Section of the episode edited by the code
/ / Prevent it from starting to draw
// Return CDialog :: OneRaseBkGnd (PDC);
Return True;
}
f. Ok, I am very satisfied, I have to take a break, but how do I close this window? Worse, do you want me to use Alt F4? For more use, write a double-click incident: void cTranswindowdlg :: ONLBUTTONDBLCLK (uint nflags, cpoint point)
{
///
// segment adding code, double-click the window to close Windows
This-> Postmessage;
///
CDIALOG :: ONLBUTTONDBLCLK (NFLAGS, POINT);
}
Oh, this article aims to illustrate the idea of transparent forms and functions, throwing bricks, believes that there must be a lot of better creativity through this idea, such as adding a resize function, so that the mask is not completely transparent and even getting into hidden. Wait, in the spirit of creativity, waiting for everyone's discovery! In the incompleteness of this article, it is not to be dragged. To add, please refer to the related articles in front of you, and then give all the friends who share the programming experience and the experience to pay attention to everyone, and call everyone to actively take the front. Friends study! I hope that my efforts can give you some help, then I am very pleased, if there is any problem or questions, please email: DuanoldFive@sohu.com, or QQ: 86685028)