zoukankan      html  css  js  c++  java
  • C# 鼠标穿透窗体功能

    通过以下代码,在窗体启动后调用方法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
            );
      /// <summary>
      /// 设置窗体具有鼠标穿透效果
      /// </summary>
      /// <param name="flag">true穿透,false不穿透</param>
      public void SetPenetrate(bool flag = true)
      {
        uint style = GetWindowLong(this.Handle, GWL_EXSTYLE);
        if (flag)
          SetWindowLong(this.Handle, GWL_EXSTYLE, style | WS_EX_TRANSPARENT | WS_EX_LAYERED);
        else
          SetWindowLong(this.Handle, GWL_EXSTYLE, style & ~(WS_EX_TRANSPARENT | WS_EX_LAYERED));
        SetLayeredWindowAttributes(this.Handle, 0, 100, LWA_ALPHA);
      }


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

  • 相关阅读:
    Codeforces Round #579 (Div. 3)
    2019牛客暑期多校训练营(第一场)ABBA
    Comet OJ
    Codeforces Round #562 (Div. 2)
    Codeforces Round #569 (Div. 2)
    寒假集训总结
    线段树做题总结
    Git学习
    unity开发vuforia血的教训
    (学习13)最短圆排列问题
  • 原文地址:https://www.cnblogs.com/mqxs/p/3353358.html
Copyright © 2011-2022 走看看