Transferred from: http://blog.csdn.net/Riklin/article/details/4417247
The code changes slightly.
These two days in the study of transparent forms, and finally slightly smaller. Most articles on the Internet are introduced to make the form transparent without the following. In fact, form transparency is not difficult, it is difficult to put controls on transparent forms. Today I will make the form transparent until the control is opaque how to make a piece to write it.
First of all, if you are not interested, you will not need to look at the following, if you are interested, keep looking down ^_^!
Watch out, this is the XP system, not installed. Net. My Photoshop is not very good, the glass is too much, if you have good art, coupled with this technology, will certainly go to the duck. Here is a detailed talk about its production process:
Step One: Use GDI + in VC6: You have to get a GDI + for XP library from the Internet, about 500K. If you can not find the words, find me qq to it, I will put this window of the source program sent to you. After extracting it, copy all files including subdirectories into your project directory. Add the following code to the stdafx.h:
" Gdiplus.h " /// usingnamespace#pragma//
My project is named Test, so add a global variable to the TestApp
Add these two lines in the bool Ctestapp::initinstance ():
12
Remember that after the thread exits to get rid of GDI +, which is a resource, add this line in int ctestapp::exitinstance ():
Everything is ready to go and start making the window.
Second, the production of PNG images: This is not a programmer's thing, is the art of the matter, but at present arts and crafts are my one person, so simply even PNG together to teach you to do it.
First Open Photoshop (PS), open a background map, use the Rounded Rectangle tool on the background map to draw a moment, and then use the layer style to bring up the following green glass:
What the? How did it come out? You really think I'm going to teach you everything? If I even wrote the PS process, I would write a book. To be considerate, it takes a lot of time to write tutorials, so you can save yourself.
Remove the background, save the glass as a PNG image, do not need to set any parameters, PNG is automatically use this with the background of transparent, powerful bar ^_^!
The same way, make the green button, remember to do the interface, the general use of a main tone, where I casually use the next green as the main color, the network is now popular blue. The text can not be used RGB color, so that users easily produce visual fatigue. I do this interface just want to try green glass is not good-looking, the result is not very good-looking, and later have a chance to get a blue glass to try it.
The button does not need to be saved as PNG because I am not ready to make it transparent. As for this "transparent control" article, you search the Internet a lot.
Go ahead! Also do the other 3 buttons, "OK" the Press effect, "Cancel" pick up and press the effect, here I will not.
Fine art is all done, start writing code.
Three, before writing the code, I first say the operation: first use SetWindowLong to set the dialog box to a hierarchical form, and then use GDI + to display the picture. The display succeeds and then is transparently treated with the Updatelayeredwindow function.
Now that the problem comes up, you will find that the controls you originally painted on the window are not displayed. I'm on this window. Another dialog box is set to the style of the startup form, and all messages are processed on this foreground form.
The question comes again, the foreground form covers up, the back window is invisible again, how to do? I also thought of a way to add a transparent color to the foreground form, where I was pink because it was the least pink in the computer because it was very dazzling. The disadvantage of using this method is that your control cannot have pink.
Finally, two buttons can be changed to bitmap buttons.
Now let's look at the detailed production process:
Define member variables: defined in TestDlg.h
Change to hierarchical form: Add the following code to the bool Ctestdlg::oninitdialog () function:
M_blend.blendop =0;//theonlyBlendOpdefinedinWindows2000M_blend.blendflags =0;//nothingelseisspecial ...M_blend.alphaformat =1;// ...M_blend.sourceconstantalpha =255;//Ac_src_alpha//form style is 0x80000 as a hierarchy formDWORD dwexstyle=GetWindowLong (M_hwnd,gwl_exstyle); SetWindowLong (M_hwnd,gwl_exstyle,dwexstyle^0x80000); To load a PNG image://draw a memory bitmapHDC HDCTEMP=GETDC ()m_hDC; M_hdcmemory=CreateCompatibleDC (hdctemp); Hbitmap Hbitmap=createcompatiblebitmap (Hdctemp, -, -); SelectObject (M_HDCMEMORY,HBITMAP); //loading PNG images with GDI +HDC hdcscreen=:: GetDC (M_hwnd); RECT RCT; GetWindowRect (&RCT); Point Ptwinpos={rct.left,rct.top}; Graphics graph (m_hdcmemory); //classes in GDI +Image Image (L"Bk.png", TRUE);//classes in GDI +Graph. DrawImage (&image,0,0,267,154);//the next two parameters should be set to the same size as the picture, otherwise it will be distortedwindow Transparency Map://transparent window processing using UpdatelayerwindowHmodule 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, you will find that your window is already transparent, the following control processing:
Iv. foreground Form
New dialog, draw a control, I am here named Conwindow, casually up, do not laugh at my ^_^.
The reader is oddly, why is there no user name, password, two label controls on this? Sorry, because my glass is too clear, these words in the glass has been very difficult to see clearly, so I simply use PS to describe the bottom, direct painting to the foreground, it became this effect, suddenly gifted you a bit, sorry, go to draw it ^_^.
Define member variables: defined in OnWindow.h:
CBrush M_brush; //
Set brush: Add a line in bool Conwindow::oninitdialog ():
M_brush. CreateSolidBrush (RGB (255,0,255));//background set to pinkchange to a hierarchy form://Setwindowslong to set a form as a hierarchical formDWORD 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);
Don't forget to paint the foreground of the form pink: Add code to the Hbrush conwindow::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nctlcolor) message map function:
Hbrush HBR =//if(nctlcolor=ctlcolor_dlg) return return cdialog::onctlcolor (PDC, pWnd, nCtlColor);
Now you want to link the foreground form to the background form, which is the key point:
set the foreground form as a startup form, no title bar, The style is popup pop-up. Write here, I have to say is: I wanted to set the foreground form to child, found that the foreground form was "transparent" off, nothing to see, depressed ah, so had to use OnMove message to design the form synchronization. If you are familiar with the form mechanism of the master friend, want to help me with a better solution.
Combo window, and keep linkage:
Add header file to TestDlg.h: #include " OnWindow.h ", then define the variable Conwindow *pchildwnd;
in void ctestdlg::onmove (int x, int y) Add the following code, see Clearly, here is the Ctestdlg transparent window. The coordinate operation of the rectangle you can modify yourself, the key to be aligned with the background:
CRect Rcwindow; // GetWindowRect (Rcwindow); Rcwindow.bottom-=; rcwindow.left+ = ; Rcwindow.right=ten; rcwindow.top+=; Pchildwnd
When creating the form: In int ctestdlg::oncreate (lpcreatestruct lpcreatestruct), add the following code:
pchildwnd= New Conwindow (this), Pchildwnd -Create (idd_onwindow_ DIALOG); Pchildwnd
You now see no title bar on the form? You move the mouse to the top of the window, you can still moving the window, you know why? Because the window is transparent, but any control of the background window is present, but does not display, it can still respond to events, do not believe you put a button on the background window to try. Isn't that good? Some code is also omitted.
Five, finally, we deal with the bitmap button: VC6 of the Cbitmapbutton::loadbitmaps method can not be directly affixed to the 16-bit true Color button, so I will two true Color button with Acdsee32 converted to 256 colors, you can directly loaded, I believe you did not see it?
After processing into 256 colors, write the code in BOOL Conwindow::oninitdialog ():
this
Now press the button to just close yourself and send a message to the parent window: Add code in void Conwindow::onok () and void Conwindow::oncancel ():
HWND hwnd=getparent (),m_hwnd;::sendmessage (hwnd,wm_close,0,0);
PNG transparent form full introduction (control opaque)