SoFunction
Updated on 2025-04-06

C# WinForm implements mouse penetration function

C# mouse penetration function (WinForm)

During WinForm development, such a scenario will be used to add a watermark logo to the screen, but it will not affect the operation of the screen. The mouse penetration function will be used here.

function

To implement the mouse penetration function, several functions need to be used: SetWindowLong, GetWindowLong, and SetLayeredWindowAttributes.

SetWindowLong

Syntax Rules

LONG SetWindowLongA(
  [in] HWND hWnd,
  [in] int  nIndex,
  [in] LONG dwNewLong
);

describe: Change the properties of the specified window. The function also sets the 32-bit (long) value of the specified offset to additional window memory.

parameter type describe
[in] hWnd HWND The handle of the window, and the indirect handle of the class to which the window belongs.
[in] nIndex int The offset from zero to the value to be set. Valid values ​​are between zero and the number of bytes in the extra window memory, subtracting the size of the integer. To set any other value, specify one of the following values.
[in]dwNewLong LONG Replace value

The value of nIndex is as follows, and the commonly used value isGWL_EXSTYLE=(-20) andGWL_STYLE=(-16)

value meaning
GWL_EXSTYLE-20 Set a new extended window style
GWL_HINSTANCE-6 Set a new application instance handle.
GWL_ID-12 Sets the new identifier for the child window. This window cannot be a top-level window.
GWL_STYLE-16 Set new window style
GWL_USERDATA-21 Sets the user data associated with the window. This data is for use by the application that creates the window. Its value is initially zero.
GWL_WNDPROC-4 Set a new address for the window process. If the window does not belong to the process where the calling thread is located, this property cannot be changed.

Return value:Long

If the function succeeds, the return value is the previous value of the specified 32-bit integer.

If the function fails, the return value is zero. To get more error information, call GetLastError.

If the previous value of the specified 32-bit integer is zero and the function succeeds, the return value is zero, but the function does not clear the last error message. This makes it difficult to determine success or failure. To handle this, you should clear the last error message by calling SetLastError 0 before calling SetWindowLong. The function failure will then be indicated as non-zero by the return value zero and the GetLastError result.

No judgment is made at present

GetWindowLong

Syntax Rules

LONG GetWindowLongA(
  [in] HWND hWnd,
  [in] int  nIndex
);

describe

Retrieve information about the specified window. This function also retrieves 32 bits (DWORD) Specifies the value of the offset to the extra window memory.

parameter type describe
[in] hWnd HWND The handle of the window, and the indirect handle of the class to which the window belongs.
[in] nIndex int The zero-based offset of the value to be retrieved. The range of valid values ​​is zero to the number of bytes of extra window memory minus 4; for example, if 12 or more bytes of extra memory are specified, the value 8 will be the index of the third 32-bit integer. To retrieve any other values, specify one of the following values.

The value of nIndex is as follows, and the commonly used value isGWL_EXSTYLE=(-20) andGWL_STYLE=(-16)

value meaning
GWL_EXSTYLE-20 Search Extended Window Styles
GWL_HINSTANCE-6 Retrieve the handle of the application instance.
GWL_HWNDPARENT-8 Retrieve the handle of the parent window (if any).
GWL_ID-12 Search for the window's identifier.
GWL_STYLE-16 Search window styles
GWL_USERDATA-21 Retrieve user data associated with the window. This data is for use by the application that creates the window. Its value is initially zero.
GWL_WNDPROC-4 Searches the address of the window process, or a handle representing the address of the window process. The window procedure must be called using the CallWindowProc function.

Return value

Type: LONG

If the function succeeds, the return value is the requested value.

If the function fails, the return value is zero. To get more error information, call GetLastError.

If SetWindowLong has not been called before, GetWindowLong returns zero for an extra window or class memory value.

SetLayeredWindowAttributes

Syntax Rules

BOOL SetLayeredWindowAttributes(
  [in] HWND     hwnd, 
  [in] COLORREF crKey,
  [in] BYTE     bAlpha,
  [in] DWORD    dwFlags
);

describe: Set the opacity and transparency color keys of the layered window.

parameter type describe
[in] hWnd HWND Handle to the hierarchical window. Specify WS_EX_LAYERED when creating a window using the CreateWindowEx function, or create a hierarchical window by setting WS_EX_LAYERED by SetWindowLong after creating the window.

Windows 8: Top-level windows and sub-windows support WS_EX_LAYERED style. Previous Windows versions only supported the top-level window WS_EX_LAYERED.
[in] crKey COLORREF COLORREF structure, specifying the transparency color key to use when combining hierarchical windows. All pixels drawn in this color of the window are transparent. To generate COLORREF, use the RGB macro.
[in]bAlpha BYTE The alpha value used to describe the opacity of the hierarchical window. Similar to the SourceConstantAlpha member of the BLENDFUNCTION structure. When bAlpha is 0, the window is completely transparent. When bAlpha is 255, the window is opaque.
[in]dwFlags DWORD Action to perform. This parameter can use one or more of the following values.
value meaning
LWA_ALPHA0x00000002 Use bAlpha to determine the opacity of the hierarchical window.
LWA_COLORKEY0x00000001 Use crKey as the transparency color.

The second is a transparent color to be set, and the third is the transparency to be set. The range of bAlpha is 0-255. If it is 0, it is completely transparent. If it is 255, it is completely opaque. The fourth parameter is whether the setting is transparent according to the transparent color or according to the bAlpha. Or both are set. If LWA_COLORKEY is set in dwFlags, then crKey will work, and all crKey color areas in the window will be transparent. If LWA_ALPHA is set in dwFlags, then bAlpha will work, and the entire window will be transparent according to the value of bAlpha. You can also set these two together to achieve these two effects at the same time.

Function code

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;

namespace demo189_Form Move
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //Define parameter value        private const uint WS_EX_LAYERED = 0x80000;
        private const int WS_EX_TRANSPARENT = 0x20;
        private const int GWL_STYLE = (-16);
        private const int GWL_EXSTYLE = (-20);
        private const int LWA_ALPHA = 0;

        //Change the properties of the specified window        [DllImport("user32", EntryPoint = "SetWindowLong")]
        private static extern uint SetWindowLong(IntPtr hwnd,int nIndex,uint dwNewLong
        );
        //Retrieve information about the specified window        [DllImport("user32", EntryPoint = "GetWindowLong")]
        private static extern uint GetWindowLong(IntPtr hwnd,int nIndex
        );
        //Set the opacity and transparency color keys of the layered window        [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
        private static extern int SetLayeredWindowAttributes(IntPtr hwnd,int crKey,int bAlpha,int dwFlags
        );

        /// <summary> 
        /// Set the form to have a mouse penetration effect        /// </summary> 
        public void SetPenetrate()
        {
             = true;
           //GetWindowLong(, GWL_EXSTYLE);
            SetWindowLong(, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
            SetLayeredWindowAttributes(m_hWnd,0,(BYTE)220,LWA_ALPHA);
           //SetLayeredWindowAttributes(m_hWnd,RGB(255,0,255),(BYTE)220,LWA_ALPHA|LWA_COLORKEY);
            //SetLayeredWindowAttributes(m_hWnd,RGB(0,255,0),0,LWA_COLORKEY);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
             = 0.7;
            SetPenetrate();
        }
    }
}

This is the end of this article about C# WinForm implementing mouse penetration function. For more related C# mouse penetration content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!