In the past two days, the research on transparent forms has been slightly reduced. Most of the articles on the Internet are about to make the form transparent, so there is no following. In fact, it is not difficult to make the form transparent. It is difficult to put controls on the transparent form. Today, I will write a piece of transparent form until the control is not transparent.
Cut a picture to seduce you. If you are not interested, you don't have to look at the following. If you are interested, continue to look at it. ^_^!
Check it out. This is an XP system and. NET is not installed. My Photoshop is not very good at learning. I have made some glass slice too transparent. If you have a good art and add this technology, it will certainly be like a fish. Let's talk about the production process in detail below:
Step 1: Use GDI + in vc6: You have to get a library of GDI + for XP from the Internet, which is about 500 K. If you cannot find it, contact QQ. I will send the source program of this window to you. Decompress the package and copy all the files in the subdirectories to your project directory. Add the following code to stdafx. h:
# Include "gdiplus. H" // modify the path of your header file
Using namespace gdiplus;
# Pragma comment (Lib, "gdiplus. lib") // modify the path of your. Lib file.
My project name is test, so add the global variable to testapp.
Ulong_ptr gdiplustoken;
Add the following two lines to bool ctestapp: initinstance:
Gdiplusstartupinput;
Gdiplusstartup (& gdiplustoken, & gdiplusstartupinput, null );
Remember to discard GDI + after the thread exits. It occupies a lot of resources and adds this line to int ctestapp: exitinstance:
Gdiplusshutdown (gdiplustoken );
Everything is ready and the preparation window is ready.
2. Create PNG images: this is not a programmer's business. It is an artist's business. However, currently, I am the only one in art technology, so I just want to teach you how to do it with PNG.
First open Photoshop (PS), open a background image, use the rounded rectangle tool on the background image to draw a rectangle, and then use the layer style to bring up the following green glass slices:
What? How can I call it out? What do you think I teach you? If I write the PS process, I can simply write a book. Understanding it, it takes a lot of time to write tutorials, so it can save time.
Remove the background and save the glass as a PNG image. You do not need to set any parameters. PNG automatically uses this method to blend transparent with the background!
In the same way, create a green button. Remember to use a dominant color when making the interface. Here I use green as the dominant color. Now the network is popular with blue. Text cannot use RGB colors, so that users are prone to visual fatigue. I just want to try the green glass, but it doesn't look very nice. Please try it later.
The button does not need to be saved as PNG, because I do not want to make it transparent. As for this article on "transparent controls", you have searched a lot on the Internet.
Continue! There are also three other buttons, the effect of "OK", the effect of "cancel" pick up and press, here I will not be able.
All art is done and code is written.
3. before writing code, let's talk about the procedure: first set the dialog box to a hierarchical form with setwindowlong, and then use GDI + to display the image. After the display is successful, use the updatelayeredwindow function for transparent processing.
Now the problem arises. You will find that the controls you previously painted in the window are not displayed. What should you do? I covered another dialog box in this window and set it to the style of the startup form. All messages are processed on this foreground form.
The problem arises again. The foreground form is covered and the window behind it is invisible again. What should I do? I tried again and added a transparent color to the foreground form. Here I use pink, because it is the least pink in the computer, because it is very dazzling. The disadvantage of using this method is that your control cannot be pink.
Finally, change the two buttons to the bitmap button.
Now let's take a look at the detailed production process:
Define member variables: defined in testdlg. h
Blendfunction m_blend;
HDC m_hdcmemory;
Change to hierarchical form: Add the following code to the bool ctestdlg: oninitdialog () function:
// The form style is 0x80000, which is a hierarchical form
DWORD dwexstyle = getwindowlong (m_hwnd, gwl_exstyle );
Setwindowlong (m_hwnd, gwl_exstyle, dwexstyle ^ 0 x80000 );
Attach a PNG Image:
// Draw a memory bitmap
HDC hdctemp = getdc ()-> m_hdc;
M_hdcmemory = createcompatibledc (hdctemp );
Hbitmap = createcompatiblebitmap (hdctemp, 500,500 );
SelectObject (m_hdcmemory, hbitmap );
// Use GDI + to load PNG Images
HDC hdcscreen =: getdc (m_hwnd );
Rect CTS;
Getwindowrect (& RDBMS );
Point ptwinpos = {RDBMS. Left, RDBMS. Top };
Graphics graph (m_hdcmemory); // class in GDI +
Image image (L "bk.png", true); // class in GDI +
Graph. drawimage (& image, 267,154,); // the two parameters must be set to the same size as the image. Otherwise, the image will be distorted.
Window transparent texture:
// Use updatelayerwindow for transparent window processing
Hmodule hfuncinst = loadlibrary ("user32.dll ");
Typedef bool (winapi * myfunc) (hwnd, HDC, point *, size *, HDC, point *, colorref, blendfunction *, DWORD );
Myfunc updatelayeredwindow;
Updatelayeredwindow = (myfunc) getprocaddress (hfuncinst, "updatelayeredwindow ");
Size sizewindow ={ 267,154 };
Point ptsrc = {0, 0 };
Updatelayeredwindow (m_hwnd, hdcscreen, & ptwinpos, & sizewindow, m_hdcmemory, & ptsrc, 0, & m_blend, 2 );
After using the above Code, run the program and you will find that your window is transparent. perform the following control processing:
Iv. Foreground form
In the create dialog box, draw the control. Here I name it conwindow. Just start it and don't laugh at me ^_^.
The reader wondered why there were no two label controls, "user name and password? Sorry, because my glass is too transparent, these words are very difficult to see on the glass, so I simply used PS to draw the bottom and directly draw the foreground, this is the effect. I am sorry, I am so sorry to miss it.
Define member variables: defined in onwindow. h:
Cbrush m_brush; // specifies the background painter.
Cbitmapbutton m_ OK;
Cbitmapbutton m_cancel;
Set paint brush: Add a row in bool conwindow: oninitdialog:
M_brush.createsolidbrush (RGB (255,); // set the background to pink.
Change to hierarchical form:
// Setwindowslong sets the form as a hierarchical form
DWORD dwexstyle = getwindowlong (m_hwnd, gwl_exstyle );
Setwindowlong (m_hwnd, gwl_exstyle, dwexstyle | 0x80000 );
Set transparent color:
// Use setlayeredwindowattributes to set the transparent color to 0, which is simpler than updatelayeredwindow.
Hmodule hinst = loadlibrary ("user32.dll ");
Typedef bool (winapi * myfunc) (hwnd, colorref, byte, DWORD );
Myfunc setlayeredwindowattributes = NULL;
Setlayeredwindowattributes = (myfunc) getprocaddress (hinst, "setlayeredwindowattributes ");
Setlayeredwindowattributes (this-> getsafehwnd (), 0xff00ff, 0, 1 );
Freelibrary (hinst );
Do not forget to paint the form foreground Pink: Add code to the Message ing function of hbrush conwindow: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor:
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
// Todo: change any attributes of the DC here
If (nctlcolor = ctlcolor_dlg)
Return m_brush;
Return cdialog: onctlcolor (PDC, pwnd, nctlcolor );
Now we need to associate the foreground form with the background form, which is the key point:
Set the foreground form to a startup form without a title bar. The style is Popup. What I have to say here is: I once wanted to set the foreground form to child, and I found that the foreground form was "Transparent" again, so I couldn't see anything, so depressed, so we had to use the onmove message to design form synchronization. If you have a good friend who is familiar with the form mechanism, I hope to help me use a better solution.
Combine windows and maintain linkage:
Add the header file # include "onwindow. H" to testdlg. h, and then define the variable conwindow * pchildwnd;
Add the following code to void ctestdlg: onmove (int x, int y). Here is the ctestdlg transparent window. You can modify the coordinates of a rectangle by yourself. The key is to align it with the background:
Cdialog: onmove (x, y );
// Todo: add your message handler code here
Crect rcwindow; // example of using the movewindow Function
Getwindowrect (rcwindow );
Rcwindow. Bottom-= 10;
Rcwindow. Left + = 10;
Rcwindow. Right-= 10;
Rcwindow. Top + = 20;
Pchildwnd-> movewindow (& rcwindow );
When creating a form: Add the following code to int ctestdlg: oncreate (maid:
// Create a subform
Pchildwnd = new conwindow (this );
Pchildwnd-> Create (idd_onwindow_dialog );
Pchildwnd-> showwindow (sw_show );
Do you see no title bars on the form? Move the mouse over the top of the window and move the window as usual. Do you know why? Although the window is transparent, any control in the background window exists, but it is not displayed, and it can still respond to events. Do not believe that you should put a button on the background window to try. Good, right? Saves some code.
5. Finally, let's deal with the bitmap button: The cbitmapbutton: loadbitmaps method of vc6 cannot directly attach 16-bit true color buttons, so I will use acdsee32 to convert two true color buttons to 256 colors, you can load it directly. I believe you didn't see it either?
After processing it into 256 colors, write the code in bool conwindow: oninitdialog:
// Load button bitmap
M_ OK .loadbitmaps (idb_ok1, idb_ok2 );
M_cancel.loadbitmaps (idb_cancel1, idb_cancel2 );
M_ OK .subclassdlgitem (idok, this );
M_cancel.subclassdlgitem (idcancel, this );
Now you can only close yourself by pressing the button. Send a message to the parent window: Add code to both void conwindow: onok () and void conwindow: oncancel:
Hwnd = getparent ()-> m_hwnd;
: Sendmessage (hwnd, wm_close, 0, 0 );
Http://blog.csdn.net/Riklin/article/details/4417247