透明窗口层可点击并始终位于顶部

2024-05-09

这是我尝试实现的一些代码。其目的是创建一个透明、全屏、无边框、可点击且始终位于其他窗口之上的表单层。然后,您可以使用 directx 在其顶部进行绘制,否则保持透明。

不起作用的部分是点击部分和 directx 渲染。当我运行它时,我基本上在所有其他窗口前面有一个不可见的力场,并且必须使用 Alt-Tab 切换到 Visual Studio 才能快速按 ALT F5 并结束调试(因此至少始终在顶部和透明度有效)。我一直试图找出为什么这些部分不起作用,但我的新手 c# 技能让我失败。希望有人能找出原因并提供修改。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Globalization;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Threading;


namespace MinimapSpy
{
public partial class Form1 : Form
{

    private Margins marg;

    //this is used to specify the boundaries of the transparent area
    internal struct Margins
    {
        public int Left, Right, Top, Bottom;
    }

    [DllImport("user32.dll", SetLastError = true)]

    private static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll")]

    static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

    [DllImport("user32.dll")]

    static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);

    public const int GWL_EXSTYLE = -20;

    public const int WS_EX_LAYERED = 0x80000;

    public const int WS_EX_TRANSPARENT = 0x20;

    public const int LWA_ALPHA = 0x2;

    public const int LWA_COLORKEY = 0x1;

    [DllImport("dwmapi.dll")]
    static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMargins);

    private Device device = null;



    public Form1()
    {

        //Make the window's border completely transparant
        SetWindowLong(this.Handle, GWL_EXSTYLE,
                (IntPtr)(GetWindowLong(this.Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED ^ WS_EX_TRANSPARENT));

        //Set the Alpha on the Whole Window to 255 (solid)
        SetLayeredWindowAttributes(this.Handle, 0, 255, LWA_ALPHA);

        //Init DirectX
        //This initializes the DirectX device. It needs to be done once.
        //The alpha channel in the backbuffer is critical.
        PresentParameters presentParameters = new PresentParameters();
        presentParameters.Windowed = true;
        presentParameters.SwapEffect = SwapEffect.Discard;
        presentParameters.BackBufferFormat = Format.A8R8G8B8;

        this.device = new Device(0, DeviceType.Hardware, this.Handle,
        CreateFlags.HardwareVertexProcessing, presentParameters);


        Thread dx = new Thread(new ThreadStart(this.dxThread));
        dx.IsBackground = true;
        dx.Start();  
        InitializeComponent();

    }

   protected override void OnPaint(PaintEventArgs e)
   {
        //Create a margin (the whole form)
      marg.Left = 0;
     marg.Top = 0;
      marg.Right = this.Width;
      marg.Bottom = this.Height;

        //Expand the Aero Glass Effect Border to the WHOLE form.
        // since we have already had the border invisible we now
        // have a completely invisible window - apart from the DirectX
        // renders NOT in black.
     DwmExtendFrameIntoClientArea(this.Handle, ref marg);  

  }
    private void Form1_Load(object sender, EventArgs e)
    {

    }
    private void dxThread()
    {
        while (true)
        {
            //Place your update logic here
            device.Clear(ClearFlags.Target, Color.FromArgb(0, 0, 0, 0), 1.0f, 0);
            device.RenderState.ZBufferEnable = false;
            device.RenderState.Lighting = false;
            device.RenderState.CullMode = Cull.None;
            device.Transform.Projection = Matrix.OrthoOffCenterLH(0, this.Width, this.Height, 0, 0, 1);
            device.BeginScene();

            //Place your rendering logic here

            device.EndScene();
            //device.Present();
        }

        this.device.Dispose();
        Application.Exit();
    }  

}

这是一个精炼的完整示例代码,用于使窗口位于最顶层 - 单击通过 - 透明(= alpha 混合)。该示例制作了一个使用 DirectX 渲染的旋转色轮,或者实际上是使用 XNA 4.0 渲染的,因为我相信 Microsoft 已经停止开发托管的 directx 并且现在更青睐 XNA。

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework.Graphics;

namespace ClickThroughXNA
{
    public partial class Form1 : Form
    {
        // Directx graphics device
        GraphicsDevice dev = null;        
        BasicEffect effect = null;     

        // Wheel vertexes
        VertexPositionColor[] v = new VertexPositionColor[100];

        // Wheel rotation
        float rot = 0;

        public Form1()
        {
            InitializeComponent();

            StartPosition = FormStartPosition.CenterScreen;   
            Size = new System.Drawing.Size(500, 500);
            FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;  // no borders

            TopMost = true;        // make the form always on top                     
            Visible = true;        // Important! if this isn't set, then the form is not shown at all

            // Set the form click-through
            int initialStyle = GetWindowLong(this.Handle, -20);
            SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);

            // Create device presentation parameters
            PresentationParameters p = new PresentationParameters();
            p.IsFullScreen = false;
            p.DeviceWindowHandle = this.Handle;
            p.BackBufferFormat = SurfaceFormat.Vector4;
            p.PresentationInterval = PresentInterval.One;

            // Create XNA graphics device
            dev = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.Reach, p);

            // Init basic effect
            effect = new BasicEffect(dev);

            // Extend aero glass style on form init
            OnResize(null);
        }


        protected override void OnResize(EventArgs e)
        {
            int[] margins = new int[] { 0, 0, Width, Height };

            // Extend aero glass style to whole form
            DwmExtendFrameIntoClientArea(this.Handle, ref margins);  
        }


        protected override void OnPaintBackground(PaintEventArgs e)
        {
            // do nothing here to stop window normal background painting
        }


        protected override void OnPaint(PaintEventArgs e)
        {                
            // Clear device with fully transparent black
            dev.Clear(new Microsoft.Xna.Framework.Color(0, 0, 0, 0.0f));

            // Rotate wheel a bit
            rot+=0.1f;

            // Make the wheel vertexes and colors for vertexes
            for (int i = 0; i < v.Length; i++)
            {                    
                if (i % 3 == 1)
                    v[i].Position = new Microsoft.Xna.Framework.Vector3((float)Math.Sin((i + rot) * (Math.PI * 2f / (float)v.Length)), (float)Math.Cos((i + rot) * (Math.PI * 2f / (float)v.Length)), 0);
                else if (i % 3 == 2)
                    v[i].Position = new Microsoft.Xna.Framework.Vector3((float)Math.Sin((i + 2 + rot) * (Math.PI * 2f / (float)v.Length)), (float)Math.Cos((i + 2 + rot) * (Math.PI * 2f / (float)v.Length)), 0);

                v[i].Color = new Microsoft.Xna.Framework.Color(1 - (i / (float)v.Length), i / (float)v.Length, 0, i / (float)v.Length);
            }

            // Enable position colored vertex rendering
            effect.VertexColorEnabled = true;
            foreach (EffectPass pass in effect.CurrentTechnique.Passes) pass.Apply();

            // Draw the primitives (the wheel)
            dev.DrawUserPrimitives(PrimitiveType.TriangleList, v, 0, v.Length / 3, VertexPositionColor.VertexDeclaration);

            // Present the device contents into form
            dev.Present();

            // Redraw immediatily
            Invalidate();            
        }


        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        [DllImport("dwmapi.dll")]
        static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref int[] pMargins);

    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

透明窗口层可点击并始终位于顶部 的相关文章

  • ASP.NET MVC - 临时要求除一页之外的整个站点授权的简单方法

    我正在建立一个混合了公共页面和会员专用页面的网站 登录系统按原样工作正常 不过 我想启动一个封闭的 仅限邀请的预览 并暂时要求访问者登录才能执行除欢迎页面之外的所有操作 目前我有 Authorize 某些操作方法的属性 我也可以向其他操作方
  • 等待运算符错误

    我的代码有问题 我怎么解决这个问题 这个问题出现在await操作符中 public MyModel HttpClient client new HttpClient HttpResponseMessage response await cl
  • 为什么 fgets 接受 int 而不是 size_t?

    功能如strcpy malloc strlen 和其他各种接受他们的参数或返回值作为size t代替int or an unsigned int出于显而易见的原因 一些文件功能 例如fread and fwrite use size t以及
  • 带有嵌入 Flash 视频的 PDF 示例?

    有谁知道我在哪里可以查看嵌入 Flash 视频的 PDF 示例 我知道问这个问题很愚蠢 因为你会认为任何面向技术的用户都应该能够使用谷歌找到一个 但我真的找不到 我的另一个问题是 使用 C 中的 API 将 Flash 视频嵌入 PDF 文
  • 将视频上传/保存到数据库或文件系统

    我以前从未尝试过保存视频 所以我对此了解不多 我知道如果视频很小 我可以转换为字节数组并保存到数据库 但是为了提高效率 我想了解如何将任何上传的视频保存到我的服务器文件中 然后只保存该文件的文件路径我的数据库表中的视频 我完全不知道如何开始
  • 如何在Unity Inspector中创建多维数组?

    如何在 Unity Inspector 中创建枚举多维数组并使其可序列化 以便我可以从不同的脚本调用它 public enum colors red blue green yellow cyan white purple public in
  • 如何生成可变参数包?

    给定不相关的输入是否可以生成非类型参数包 我的意思是 我想改变这一点 template
  • 在 Linq 查询中使用动态列名称

    foreach Dimension dimensions in Enum GetValues typeof Dimension var r new ReferenceTable dimensions referenceItems List
  • 如何将输出重定向到 boost 日志?

    我有一个使用boost log的C 程序 我加载了用户提供的动态链接库 我想将 stderr 重定向到 boost 日志 以便用户的库随时执行以下操作 std cerr lt lt Some stuff 它产生相同的结果 BOOST LOG
  • ef core 在更新数据库期间不使用 ASPNETCORE_ENVIRONMENT

    我使用 Visual Studio 通过一定的迁移来更新我的所有环境 使用下面的命令效果很好 update database Migration initMigrationProduct c ProductContext Environme
  • 当分配返回 0 时,具有空异常规范的运算符 new 调用构造函数

    我有以下声明 void operator new size t s PersistentMemory m throw return m gt allocatePersistentMemory s 我正在测试启动时的内存耗尽 这会导致m gt
  • 如何将 Boost Spirit 自动规则与 AST 结合使用?

    编辑 当我想在另一个规则上使用它时 我扩展了 sehe 的示例以显示问题 http liveworkspace org code 22lxL7 http liveworkspace org code 22lxL7 17 我正在尝试提高 Bo
  • WCF 服务中的缓冲区大小

    我们有一个 WCF 服务 它执行某些存储过程并将结果返回给 silverlight 客户端 某些存储过程最多返回 80K 行 下面给出的是 web config 中服务的设置
  • 快速将文本附加到文本框

    我有一个BackgroundWorker正在发布消息的线程 使用BeginInvoke在 GUI 中的文本框中 方法 write debug text 在文本框中显示文本使用AppendText并将文本写入Console 外观上是这样的Ba
  • 在链表程序中使用模板时重载 C++ 中的 << 运算符

    我正在尝试实现一个链接列表 但是当我尝试重载 include
  • Membership.ValidateUser() 的目的是什么

    我一直在学习有关MembershipProvider类 我认为Membership ValidateUser 方法应该用于登录用户 然而我刚刚了解到有一个FormsAuthentication Authenticate 目的是什么Valid
  • 调用泛型类的方法

    这是上下文 我尝试编写一个映射器来动态地将域模型对象转换为 ViewModel 对象 我遇到的问题是 当我尝试通过反射调用泛型类的方法时 出现此错误 System InvalidOperationException 无法对 Contains
  • 标准 .NET 库是否依赖于任何非托管 DLL?

    只是出于好奇 NET 框架本身在访问标准库时是否依赖于任何非托管 DLL 例如 我调用方法 A 并且 在幕后 方法 A 或该方法 A 内的任何其他方法对非托管 DLL 执行 PInvoke 是的 Net 库大量使用非托管函数 库可以调用两种
  • 为什么 INT64_MIN 的定义不同?为什么他们的行为不同?

    The stdint h我公司的标题是 define INT64 MIN 9223372036854775808LL 但在我项目的一些代码中 一位程序员写道 undef INT64 MIN define INT64 MIN 92233720
  • 从 C/C++ 程序进行 Ping

    我想编写一个 C 或 C 程序 给定一个 IP 地址 对其进行 Ping 然后根据 Ping 是否成功执行进一步的操作 这个怎么做 尽情享受Ping 页面 http www ping127001 com pingpage htm 其中有一个

随机推荐

  • 当响应标头具有位置字段时,RestSharp 返回空值

    我的休息要求 RestSharp RestClient uplClient new RestSharp RestClient RestSharp RestRequest request new RestSharp RestRequest I
  • 夏令时期间如何使用TimeZoneInfo获取当地时间?

    我正在尝试使用DateTimeOffset传达任何时区的特定时刻 我不知道如何使用TimeZoneInfo处理夏令时 var dt DateTime UtcNow Console WriteLine dt ToLocalTime var t
  • 使用 linq 和 sum 聚合列表

    我正在尝试合并一个我正在序列化的可枚举列表 我的数据看起来像这样 Internet explorer 10 Internet explorer 15 Mozille firefox 10 我的样子是这样的 Internet explorer
  • 当通过 basho rebar 从命令行运行 Erlang 应用程序时,如何设置 Erlang 节点名称

    我已经使用 basho rebar 编译了我的 Erlang 应用程序 它生成了一个独立的 escript 可执行文件 我从命令行运行它 如下所示 myapp myconfig config 我的问题是如何确定运行我的应用程序的 Erlan
  • Android:每秒更新蓝牙 RSSI

    我试图每秒显示蓝牙信号强度 RSSI Timer 来自检测到的设备 但我无法调用onRecive 多次因为接收器生命周期 http developer android com reference android content Broadc
  • 使用信号时出现 django TransactionManagementError

    我有一个与 django 的用户和 UserInfo 一对一的字段 我想订阅用户模型上的 post save 回调函数 以便我也可以保存 UserInfo receiver post save sender User def saveUse
  • Azure 应用服务 API 应用程序的访问安全性

    我们有一个基于 2 层的系统 即后端层和前端层 现在两者都是通过 WebAPI 进行通信的 Azure 网站 我不想将后端 WebAPI 移至 API 应用程序 问题是 是否可以以这种方式配置 API 应用程序的安全性 那就是only可从配
  • 是否可以使用 PHP 检测用户来自哪个操作系统? (Mac 或 Windows)

    比方说 我想回应 你正在使用 Windows 或 您正在使用 Macintosh 具体取决于用户的操作系统 这可能吗 通过分析 SERVER HTTP USER AGENT 可以说出什么system and browser 用户是claim
  • 在 ExpressJS 中通过管道传送远程文件

    我想读取远程图像并显示它 我可以保存文件 但无法正确显示代码 理想情况下 我只想直接传递文件而不进行处理 不确定是否需要 tmp 文件步骤 此代码不显示任何内容 没有错误 我也尝试了 res pipe response var url ht
  • Selenium - 保存网站,包括所有图像、css、dom

    我想使用 firefox 或 chrome 访问带有 selenium 的页面 当页面加载时 我想从页面下载所有图像 css dom 我想存储每张图像 就像我在其中找到它们一样 chrome gt Tools gt Development
  • 我的用户脚本如何根据链接的文本获取链接?

    给定目标页面上的 HTML dd class ddTit a href http abc xxx com 54781 html target blank special text words a dd 我怎样才能根据 获取url特殊文字词
  • T v{} 初始化

    我正在阅读 C 11 标准 但不知道是否 T x 是值初始化或默认初始化 自动存储 它does说得很清楚 10 其初始化器为一组空括号 即 的对象应进行值初始化 And that 11 如果没有为对象指定初始化器 则该对象被默认初始化 但我
  • Rails - 使用delayed_job异步发送所有电子邮件

    我在用着延迟作业 https github com collectiveidea delayed job我对此非常满意 尤其是workless https github com lostboy workless扩大 但我想这样设置ALL我的
  • 黄瓜与 Micronaut

    我正在尝试将 Cucumber 与 Micronaut 一起使用 但当我尝试将其与 Cucumber 一起使用时 MicronautTest 注释根本不起作用 未注入 theApple 请参阅下面的代码 如果我在没有黄瓜的情况下运行它就可以
  • Laravel 5.5 中的主从配置

    如何配置 Laravel 5 5 主从 MySQL 复制 我想分别在master和slave上进行写操作和读操作 可选 有没有办法在理想条件下进行连接池和打开连接的最大 最小数量 只需改变你的config database php文件包含读
  • 使用列表中的数据框:删除变量,添加新变量

    定义一个列表dats有两个数据框 df1 and df2 dats lt list df1 data frame a sample 1 3 b sample 11 13 df2 data frame a sample 1 3 b sampl
  • 如何在 NHibernate 中自动生成 ID

    如何让 NHibernate 自动生成表的唯一 ID ID 可以是任意的long值 只要每个值仅使用一次 我当前的映射如下所示
  • 如何动态查找连接组件

    使用不相交集数据结构可以很容易地得到图的连通分量 而且 它只是支持增量连接组件 http www boost org doc libs 1 46 1 libs graph doc incremental components html 然而
  • excel中的多轴折线图

    我正在寻找类似于下图中的多轴折线图 这是由 amcharts 制作的 JavaScript 图表 excel有没有可以绘制图表的选项 请注意 有 3 个 Y 轴和 3 个折线图 可让您比较数据 是否有可能获得超过 3 个数据点 每个数据点在
  • 透明窗口层可点击并始终位于顶部

    这是我尝试实现的一些代码 其目的是创建一个透明 全屏 无边框 可点击且始终位于其他窗口之上的表单层 然后 您可以使用 directx 在其顶部进行绘制 否则保持透明 不起作用的部分是点击部分和 directx 渲染 当我运行它时 我基本上在