Display windows in Windows 2000/XP
Translated by abhinaba: XiaoJin, Nanjing zhongcui Food Co., Ltd.
Source: http://www.codeproject.com/dialog/WinTrans1.asp
Preface
ManyArticleDemonstrate window transparency by using hierarchical features of Windows 2000/XP. This article uses this feature to make any window transparent, even if you do notProgramOfSource code.
With the "wintrans" program, you can select any program that is running, drag the rod in the upper-left corner with the left mouse button, press it on the title bar of the program, and then release it, then the program becomes transparent. You can adjust the slider position to control transparency. "Wintrans" has a very spy interface. It can also demonstrate the following usage of Win32 APIs: use the mouse pointer to locate the window and obtain window information such as class name and title.
Usage
In Windows 2000/XP, user32.dll adds a new function setlayeredwindowattributes. To use this function, you must set the window style ws_ex_layered (0x00080000) in the generated window or the setwindowlong function ). Once this style is set, we can call this function to make the window transparent. Parameters required for this function are as follows:
- Hwnd: Window handle
- Colorref Col: transparent color
- Byte balpha: = 0: The entire window is transparent, = 255 is completely opaque
- DWORD dwflags: = 1: Color Col only transparent, = 2: The window is transparent according to the balpha variable.
Code
First, define the member variable (wintransdlg. h) in the dialog box ).
Bool m_btracking; // set it to truehwnd m_hcurrwnd when the mouse is captured; // hcursor m_hcursor handle of the window in which the mouse is located; // a rod-shaped cursor handle
Define a pointer to the setlayeredwindowattributes function. This function is defined in user32.dll.
// Global variable typedef bool (winapi * lpfn) (hwnd, colorref Cr, byte balpha, DWORD dwflags); lpfn g_psetlayeredwindowattributes;
In the oninitdialog event, get the pointer of the setlayeredwindowattributes function and save it in the global variable g_psetlayeredwindowattributes.
Bool cwintransdlg: oninitdialog (){.... // obtain the pointer hmodule huser32 = getmodulehandle (_ T ("user32.dll"); iterator = (lpfn) getprocaddress (huser32, "setlayeredwindowattributes") of the function handler in user32.dll "); if (outputs = NULL) afxmessagebox ("layering is not supported in this version of Windows", mb_iconexclamation); // load the hinstance hinstresource = afxfindresourcehandle (makeintresource (idc_wand ), rt_group_cursor); m_hcursor =: loadcursor (hinstresource, makeintresource (idc_wand ));...}
The code for defining the trigger function of the wm_lbuttondown, wm_lbuttonup, and wm_mousemove events is as follows:
Void cwintransdlg: onlbuttondown (uint nflags, cpoint point) {... setcapture (); // set the Mouse capture to the specified window. When the mouse button is pressed, the window will receive all the mouse input m_hcurrwnd = NULL for // The current application or the entire system; // No window transparency yet m_btracking = true; // set the track flag: setcursor (m_hcursor); // change the cursor to a rod}
Wm_mousemove event handler:
Void cwintransdlg: onmousemove (uint nflags, cpoint point ){... if (m_btracking ){... // obtain the clienttoscreen (& Point );... // obtain the window handle m_hcurrwnd =: windowfrompoint (point );... // display the class and title of the window... ...}...}
Once you click in the window with the left mouse button and do not release it, the pointer of the mouse changes to a great shape, and the window information will be displayed in the wintrans window. When the left mouse button is released, the event wm_lbuttonup processing function is called.
void cwintransdlg: onlbuttonup (uint nflags, cpoint point ){... // release the Mouse capture releasecapture (); m_btracking = false; // if the window below the mouse is not wintrans of this program, we need to set the level style and set the slider to make it transparent. If (g_psetlayeredwindowattributes & m_hcurrwnd! = M_hwnd) {: setwindowlong (m_hcurrwnd, gwl_exstyle, getwindowlong (rows, gwl_exstyle) ^ rows); trim (rows, 0, (byte) m_slider.getpos (), lwa_alpha );:: redrawwindow (m_hcurrwnd, null, null, rdw_erase | rdw_invalidate | rdw_frame | rdw_allchildren );}...}