首页 > 开发 > 综合 > 正文

C#制作鼠标可以穿透的窗体

2024-07-21 02:25:54
字体:
来源:转载
供稿:网友


把窗体的formborderstyle设为formborderstyle.none


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 = 0x2;

        [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 canpenetrate()
        {
            uint intextemp = getwindowlong(this.handle, gwl_exstyle);
            uint oldgwlex = setwindowlong(this.handle, gwl_exstyle, ws_ex_transparent | ws_ex_layered);
            setlayeredwindowattributes(this.handle, 0, 100, lwa_alpha);
        } 

要时窗体恢复正常,只要执行以下语句:
                this.formborderstyle = formborderstyle.none;


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表