C# 鼠标穿透窗体功能_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C# 鼠标穿透窗体功能

C# 鼠标穿透窗体功能

 2013/10/6 11:43:02  梦琪小生  博客园  我要评论(0)
  • 摘要:通过以下代码,在窗体启动后调用方法SetPenetrate()即可实现窗体的穿透功能。同样该功能需要加载命名空间usingSystem.Runtime.InteropServices;privateconstuintWS_EX_LAYERED=0x80000;privateconstintWS_EX_TRANSPARENT=0x20;privateconstintGWL_STYLE=(-16);privateconstintGWL_EXSTYLE=(-20)
  • 标签:C# 功能 鼠标

通过以下代码,在窗体启动后调用方法SetPenetrate() 即可实现窗体的穿透功能。

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

using System.Runtime.InteropServices;

class="code_img_closed" src="/Upload/Images/2013100611/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('875afe0a-8c8f-46b3-a87f-96cfd649007c',event)" src="/Upload/Images/2013100611/2B1B950FA3DF188F.gif" alt="" />
        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);
        } 
View Code


原文地址:http://bbs.csdn.net/topics/390325341

发表评论
用户名: 匿名