四联光电智能照明论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2203|回复: 0
打印 上一主题 下一主题

C#使用Windows API实现桌面上的遮罩层(鼠标穿透)

[复制链接]
  • TA的每日心情
    开心
    2018-7-4 09:08
  • 97

    主题

    392

    帖子

    6095

    积分

    论坛元老

    Rank: 8Rank: 8

    积分
    6095
    跳转到指定楼层
    楼主
    发表于 2016-11-8 21:27:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

    C#实现实现桌面上的遮罩层(鼠标穿透)主要通过一下几个API函数来实现:GetWindowLong,SetWindowLong,SetLayeredWindowAttributes。其中有一个Windows 消息 WS_EX_TRANSPARENT 比较重要,它实现了鼠标穿透的功能。
    下面来看看完整的实现代码:
    1. using System;    
    2. using System.Drawing;    
    3. using System.Windows.Forms;    
    4. using System.Runtime.InteropServices;    
    5. namespace WindowsApplication40    
    6. {    
    7.   public partia lclass Form1:Form    
    8.   {    
    9.     public Form1()    
    10.     {    
    11.       InitializeComponent();    
    12.     }    
    13.     [DllImport("user32.dll",EntryPoint="GetWindowLong")]    
    14.     public static extern long GetWindowLong(IntPtr hwnd,int nIndex);    
    15.     [DllImport("user32.dll",EntryPoint="SetWindowLong")]    
    16.     public static extern long SetWindowLong(IntPtr hwnd,int nIndex,long dwNewLong);    
    17.     [DllImport("user32",EntryPoint="SetLayeredWindowAttributes")]    
    18.     public static extern int SetLayeredWindowAttributes(IntPtr Handle,int crKey,byte bAlpha,int dwFlags);    
    19.     const int GWL_EXSTYLE=-20;    
    20.     const int WS_EX_TRANSPARENT=0x20;    
    21.     const int WS_EX_LAYERED=0x80000;    
    22.     const int LWA_ALPHA=2;    
    23.    
    24.     private void Form1_Load(objec tsender,EventArgs e)    
    25.     {    
    26.       this.BackColor=Color.Silver;    
    27.       this.TopMost=true;    
    28.       this.FormBorderStyle=FormBorderStyle.None;    
    29.       this.WindowState=FormWindowState.Maximized;    
    30.       SetWindowLong(Handle,GWL_EXSTYLE,GetWindowLong(Handle,GWL_EXSTYLE)|WS_EX_TRANSPARENT|WS_EX_LAYERED);    
    31.       SetLayeredWindowAttributes(Handle,0,128,LWA_ALPHA);    
    32.     }    
    33.   }    
    34. } 
    复制代码

    以上就是介绍C#使用Windows API实现桌面上的遮罩层(鼠标穿透),希望对你有所帮助。
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|Silian Lighting+ ( 蜀ICP备14004521号-1 )

    GMT+8, 2024-5-2 12:12 , Processed in 1.093750 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表