欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 鼠标穿透窗体功能的实现方法

程序员文章站 2024-02-19 23:40:46
同样该功能需要加载命名空间 using system.runtime.interopservices; 复制代码 代码如下:private const uint ws...

同样该功能需要加载命名空间

using system.runtime.interopservices;

复制代码 代码如下:

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;

        [dllimport("user32", entrypoint = "setwindowlong")]
        private static extern uint setwindowlong(
        intptr hwnd,
        int nindex,
        uint dwnewlong
        );

        [dllimport("user32", entrypoint = "getwindowlong")]
        private static extern uint getwindowlong(
        intptr hwnd,
        int nindex
        );

        [dllimport("user32", entrypoint = "setlayeredwindowattributes")]
        private static extern int setlayeredwindowattributes(
        intptr hwnd,
        int crkey,
        int balpha,
        int dwflags
        );

        /// <summary>
        /// 设置窗体具有鼠标穿透效果
        /// </summary>
        public void setpenetrate()
        {
            getwindowlong(this.handle, gwl_exstyle);
            setwindowlong(this.handle, gwl_exstyle, ws_ex_transparent | ws_ex_layered);
            setlayeredwindowattributes(this.handle, 0, 100, lwa_alpha);
        }