透明覆盖

Transparent Overlay

我正在尝试制作一个可以在屏幕中间覆盖透明图像的应用程序。我的目标是为没有十字准线的游戏制作十字准线。我的想法是检测活动的 window 标题是否与游戏名称匹配,如果匹配则显示覆盖的十字准线。我将如何制作屏幕叠加层?这是我当前的代码:

Private Function GetCaption() As String
    Dim Caption As New System.Text.StringBuilder(256)
    Dim hWnd As IntPtr = GetForegroundWindow()
    GetWindowText(hWnd, Caption, Caption.Capacity)
    Return Caption.ToString()
End Function

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    If GetCaption() = "Game NameOf" Then
        'Display Crosshair
    End If 
End sub

此方法适用于大多数游戏,但仅适用于 window 模式!

  1. 在您的表单上放置一个图片框并最大化您的图片框
  2. 禁用您的 Windowboarders:

    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None

  3. 将透明键设置为黑色。第

    Me.TransparencyKey = Color.Black

  4. 将背景颜色设置为黑色:

    Me.PictureBox1.BackColor = Color.Black

  5. 将您的 window 设置为前景:

    Me.TopMost = true

  6. 最大化你的window:

    Me.WindowState = FormWindowState.Maximized

现在您可以在 Timer1_Tick 或 Form1_Paint 事件中在 Picturebox 上绘图。非黑色的所有内容都将绘制到您的桌面。

Dim g As = GraphicsPictureBox1.CreateGraphics
...
g.DrawLine(Pens.Red, 10, 10, 200, 200)

重要提示:

要通过您的 window 传递来自鼠标和键盘的输入,您必须在 .net 创建表单时添加 WS_EX_TRANSPARENT 标志。这可以通过 overriding CreateParams proterty:

来完成
Const  WS_EX_TRANSPARENT As Long = &H20
...
Protected Overrides ReadOnly Property  CreateParams() As System.Windows.Forms.CreateParams
Get 
    Dim  SecPerm As New SecurityPermission(SecurityPermissionFlag.UnmanagedCode)
    SecPerm.Demand()
    Dim  cp As CreateParams = MyBase.CreateParams
    cp.ExStyle = cp.ExStyle Or WS_EX_TRANSPARENT
    Return cp
End Get
End Property

希望能帮到你。