Window transparency in Windows 2000/XP

Source: Internet
Author: User
Tags bool transparent color

Objective

Many articles demonstrate the use of Windows 2000/XP hierarchy features to achieve window transparency. This article allows any window to be transparent, even if you do not have the source code for the program.

Using the "Wintrans" program, you can select any running program, use the left mouse button to drag the bar in the upper left-hand corner and press it on the title bar of the program, then let go, then the program can become transparent. You can adjust the slider position to control transparency. "Wintrans" has a very like SPY interface, but also can demonstrate the following uses of Win32 APIs: positioning the window with the mouse pointer, for example, such as class name, caption and other window information.

Usage

In Windows 2000/XP, User32.dll adds a new function setlayeredwindowattributes. To use this function, we must set the window style ws_ex_layered (0x00080000) in the build window or using the SetWindowLong function. Once this style is set, we can call the function to transparently window. The parameters required for the function are as follows:

HWND hwnd: Window Handle

COLORREF col: Transparent Color

BYTE Balpha: = 0: Entire window transparent, = 255 completely opaque

DWORD dwflags: = 1: Only the color col transparent, = 2: The window is transparently processed according to the BALPHA variable.

Code

First, define the member variable (WinTransDlg.h) of the dialog box.

bool m_btracking;  Set to True when mouse is caught
HWND m_hcurrwnd; The handle to the mouse window
hcursor m_hcursor//bar cursor handle
also defines a pointer to the SetLayeredWindowAttributes function. The function is defined in User32.dll. //global variable
typedef BOOL (WINAPI *LPFN) (HWND hwnd, COLORREF CR,
BYTE Balpha, DWORD dwflags);
Lpfn G_ Psetlayeredwindowattributes;
Gets a pointer to the SetLayeredWindowAttributes function in the OnInitDialog event and saves it in the global variable g_psetlayeredwindowattributes. BOOL Cwintransdlg::oninitdialog ()
{
....
//Gets the pointer to the function setlayeredwindowattributes in User32.dll
hmodule hUser32 = GetModuleHandle (_t. DLL));   
G_psetlayeredwindowattributes = (LPFN) GetProcAddress (HUser32,
"setlayeredwindowattributes");
if (g_psetlayeredwindowattributes = = NULL)
AfxMessageBox (
"Layering is not supported in" version of Windows,
Mb_iconexclamation);
//Mount stick cursor
hinstance Hinstresource = AfxFindResourceHandle (
Makeintresource (idc_wand), Rt_group_cursoR);
M_hcursor =:: LoadCursor (Hinstresource, Makeintresource (Idc_wand));
...
}
and then define the trigger functions for event wm_lbuttondown, Wm_lbuttonup, and Wm_mousemove. The M_lbuttondown event code is as follows: void Cwintransdlg::onlbuttondown (UINT nflags, CPoint point)
{
...   
SetCapture (); Mouse capture is set to the specified window.   When the mouse button is pressed, this window will receive all mouse input for the current application or the entire system
M_hcurrwnd = NULL;//No window is now transparent
M_btracking = true; Set the track flag
:: SetCursor (M_hcursor);//change the cursor to a bar
}
wm_mousemove event handler: void Cwintransdlg::onmousem Ove (UINT nflags, CPoint point)
{
...
if (m_btracking)
{
...     
//Get mouse position
ClientToScreen (&point);
...     
//Get the window handle under the mouse
M_hcurrwnd =:: Windowfrompoint (point);
...
//Displays information about the window's class, title, etc. ...
...
}
...
}

Once you click in the window with the left mouse button and do not release, the mouse pointer will become a bar, and the window information will be displayed on the Wintrans window. When the left mouse button is released, the event Wm_lbuttonup handler function is invoked.

void CWinTransDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
  ...
  //释放鼠标捕获
  ReleaseCapture();
  m_bTracking = false;
   //如果鼠标下面的窗口不是本程序WinTrans,我们就要设置层次样式并且通过设置滑动条来实现透明化。

  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);
  }
  ...
}

Related Article

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.