PNG transparent form full introduction (control opaque)

Source: Internet
Author: User
Tags transparent color

http://blog.csdn.net/riklin/article/details/4417247


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:
#include "Gdiplus.h"

using namespace Gdiplus;
#pragma comment (lib, "Gdiplus.lib")////please modify the path to your. lib file
My project is named Test, so add a global variable to the TestApp
ULONG_PTR Gdiplustoken;
Add these two lines in the bool Ctestapp::initinstance ():
Gdiplusstartupinput Gdiplusstartupinput;
Gdiplusstartup (&gdiplustoken, &gdiplusstartupinput, NULL);
Remember that after the thread exits to get rid of GDI +, which is a resource, add this line in int ctestapp::exitinstance ():
Gdiplusshutdown (Gdiplustoken);
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
Blendfunction M_blend;
HDC m_hdcmemory;

Change to hierarchical form: Add the following code to the bool Ctestdlg::oninitdialog () function:
Form style is 0x80000 as a hierarchy form
DWORD Dwexstyle=getwindowlong (M_hwnd,gwl_exstyle);
SetWindowLong (m_hwnd,gwl_exstyle,dwexstyle^0x80000);

To load a PNG image:
Draw a Memory bitmap
HDC hdctemp=getdc ()->m_hdc;
M_hdcmemory=createcompatibledc (hdctemp);
Hbitmap Hbitmap=createcompatiblebitmap (hdctemp,500,500);
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 distorted

Window Transparency Map:
Transparent window processing using Updatelayerwindow
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, 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; Background painting Brush
CBitmapButton M_OK;
CBitmapButton M_cancel;

Set brush: Add a line in bool Conwindow::oninitdialog ():
M_brush.  CreateSolidBrush (RGB (255,0,255)); Background set to Pink


Change to a hierarchy form:
Setwindowslong to set a 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);

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 = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);
Todo:change any attributes of the DC
if (NCTLCOLOR=CTLCOLOR_DLG)
return m_brush;
Return Cdialog::onctlcolor (PDC, PWnd, nCtlColor);

Now you want to link the foreground and background forms, which is the key point:
Set the foreground form to the startup form, without the title bar, and the popup pop-up style. 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.

Combine the windows and keep the linkage:
Add header file in TestDlg.h: #include "OnWindow.h", then define variable Conwindow *pchildwnd;

in void Ctestdlg::onmove (int x, int y) Add the following code to 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:
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 the form: In int ctestdlg::oncreate (lpcreatestruct lpcreatestruct), add the following code:
Create a child form
Pchildwnd=new Conwindow (this);
Pchildwnd->create (Idd_onwindow_dialog);
Pchildwnd->showwindow (Sw_show);

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 ():
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 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);

http://blog.csdn.net/witch_soya/article/details/6889939

PNG transparent form full introduction (control opaque)

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.