Let any windows in Windows 2000XP transparent

zhaozj2021-02-08  239

Let any windows in Windows 2000 / XP transparent

Introduction

There are many articles show how to create a transparent window in Windows 2000 or Windows XP by using new system functions, this article shows you a way to make any application window transparent. Even if you don't have the source program for that application.

Using the "Wintrans" program introduced in this article, you only need to drag the "Magic Walker" in the program (the icon in the upper left corner of the program) to make it transparent on the title bar of another program. You can also adjust the transparency by dragging the slider. The interface of "Wintrans" is very similar to the SPY program interface. It demonstrates how to use the Win32 API function to capture a window located below the mouse and get information such as window classes, window titles.

When you need to work in a maximized window while you need to view the status of another program running in the background, you will find the "Wintrans" program is a very practical program.

background

In Windows 2000 and Windows XP, a new function is added in user32.dll, and the name is SetlayeredWindowAttributes. If you want to use this function in your application, you need to set the WS_EX_LAYERED (0x00080000) bit when you create a window, or you can add this bit with a setWindowlong function after the window is created. Once this flag is set, we can pass the setlayeredWindowsattributes function and pass the window's handle to it makes a window or a specific color on the window transparent. The parameters of this function are as follows:

HWND HWND: Handle of the window

ColorRef color: I want to transparent color

BYTE BALPHA: If this value is set to 0, the window will become completely transparent. If set to 255, the window will become completely opaque.

DWORD DWFLAGS: If this flag is set to 1, only colors specified by colors will become transparent. If this flag is set to 2, the entire window will become transparent according to the extent specified by Balpha.

Code explanation

First, add the following member variables in the main dialog class in Wintransdlg.h.

Bool m_btracking; // is placed as true when the mouse is captured

HWnd m_hcurrwnd; // The handle of the window pointing to the window

Hcursor M_HCURSOR; / / Magic Cursor

We also defined a pointer to the setlayeredWindowAttributes function. This function is located in user32.dll.

// Global definition

Typedef Bool (WinApi * LPFN) (HWND HWND, ColorRef Cr,

BYTE BALPHA, DWORD DWFLAGS;

LPFN G_PsetLayeredWindowAttribute;

In the OnInitDialog message response function, we get the address of the SetlayeredWindowAttributes function and save it in the g_psetlayeredwindowattributes variable. At the same time, we also loaded the magic brakes and put its handles in M_HCURSOR.

Bool cwinTransdlg :: oninitdialog ()

{

....

/ / Get the address of the setlayeredWindowAttributes function in user32.dll

HModule huser32 = getModuleHandle (_T ("user32.dll");

g_psetlayeredWindowAttributes = (LPFN) GetProcaddress (HUSER32,

"SetlayeredWindowAttributes");

IF (g_psetlayeredwindowattributes == null) AFXMESSAGEBOX

"Layering Is Not Supported in this Version of Windows",

MB_ICONEXCLAMATION;

/ / Load Magic Cursor

Hinstance HinstResource = AFXFINDRESOURCEHANDLE

MakeintResource (IDC_WAND), RT_Group_cursor;

m_hcursor = :: loadcursor (hinstresource, makeintresource; idc_wand);

...

}

The following defines the response function of WM_LBUTTONDOWN, WM_LBUTTONUP and WM_MOUSEMOVE messages. The response function of WM_LBUTTONDOWN is as follows:

Void CwinTransdlg :: ONLBUTTONDOWN (uint nflags, cpoint point)

{

...

SetCapture (); // Make the mouse message back to this window

m_hcurrwnd = null; // The current no window is set to transparent

m_btracking = true; // Set the mark captured by the mouse

:: setCursor (m_hcursor); // Turn the mouse cursor into a magic cursor

...

}

The response code for processing the mouse move message is as follows

Void CwinTransdlg :: OnMousemove (uint nflags, cpoint points)

{

...

IF (m_btracking)

{

...

/ / Convert the mouse coordinate to the screen coordinate

ClientToscreen (& Point);

...

/ / Get the cursor of the mouse position

m_hcurrwnd = :: windowfrompoint (point);

...

// Display the details of the window, including window classes, title, etc.

...

}

...

}

This makes it possible to make the mouse light into the magic bar cursor as long as you press the mouse button in the main window, and some information in the window under the cursor will appear in the dialog box of the Wintrans program.

When the mouse button is released, the response function of the WM_LBUTTONUP message will be called.

Void CwinTransdlg :: ONLBUTTONUP (UINT NFLAGS, CPOINT)

{

...

// Stop catching mouse

Releasecapture ();

M_BTRACKING = FALSE;

/ / If the window fingered by the mouse cursor is not this application itself

/ / Set its Layer style bits and press the set value of the slider to set the alpha value of the window.

IF (g_psetlayeredwindowattributes && m_hcurrwnd! = m_hwnd)

{

:: setWindowlong (M_HCurrWnd, GWL_EXSTYLE,

GetWindowlong (m_hcurrwnd,

GWL_EXSTYLE) ^ WS_EX_LAYERED);

g_psetlayeredWindowAttributes (m_hcurrwnd, 0,

(Byte) m_slider.getpos (), lwa_alpha);

:: RedRawwindow (M_Hcurrwnd, Null, Null,

RDW_ERASE | RDW_INVALIDATE |

RDW_FRAME | RDW_ALLCHILDREN;

}

...

}

Other places worthy of attention

The current program only works in the title bar or a dialog-based program window in the magic wreath. For example: if you point the magic wreath to the window body of Notepad.

If you want to remove the window transparent effect, you only need to drag the magic wand to the corresponding window again. Because in ONLBUTTONUP, we are switching the setting status of the WS_EX_LAYERED flag bit, and the transparent effect will also switch. The Transwand program cannot work correctly on the command prompts.

This article related source program download:

Original download address (12K) (to be downloaded after the site is available)

Domestic mirror (12K)

转载请注明原文地址:https://www.9cbs.com/read-1895.html

New Post(0)