招生热线
0755-86191118 0755-86191118
我的位置: 首页 > 学习专区 > .NET技术 > C# 鼠标穿透窗体功能

C# 鼠标穿透窗体功能

2014-05-13 10:52:59
来源:
[导读]   通过以下代码,在窗体启动后调用方法SetPenetrate() 即可实现窗体的穿透功能。  同样该功能需要加载命名空间  using System Runt
  
通过以下代码,在窗体启动后调用方法SetPenetrate() 即可实现窗体的穿透功能。

 

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

  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

  );

  ///

 

  /// 设置窗体具有鼠标穿透效果

  ///

 

  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);

  }

评论