【问题标题】:Draw graphics on screen在屏幕上绘制图形
【发布时间】:2019-06-16 18:01:40
【问题描述】:

我正在使用 Visual Basic.Net 并在屏幕上绘制图形。

这是我的代码:

Private Sub Button1_Click(ByVal sender As System.Object, 
                          ByVal e As System.EventArgs) Handles Button1.Click
Dim gr As Graphics = Graphics.FromHwnd(New IntPtr(0))
gr.DrawString("text on screen", 
               New Font(Me.Font.FontFamily, 25, 
               FontStyle.Regular), Brushes.Red, 50, 50)
End Sub

在上面的代码中,文本是在屏幕上绘制的。我的问题是:如何删除绘制到屏幕上的文本?我看到有一个 .Clear 方法,但是,它'清除整个绘图表面并用指定的背景颜色填充它',而不是仅仅删除绘制的文本。

提前致谢。

编辑

我想开发一个潜意识消息应用程序,当用户使用其他应用程序时,它会在屏幕上显示消息。透明表单是不是最好的方法?

【问题讨论】:

  • 这不是合适的绘画方式。无论你画什么,都可以在任何随机时刻被抹去。无法恢复它只是您不应该这样做的另一个例子。创建自己的窗口。它不必是可见的,使用 Form.TransparencyKey 属性。
  • 另外,IntPtr.Zero 不是获取桌面窗口句柄的方式。见GetDesktopWindow()
  • InvalidateRect() 可能适合清除绘制的文本。
  • 所有这些 - @HansPassant 是正确的。带有透明度键或使用 WS_EX_LAYERED、WS_EX_TRANSPARENT 和 UpdateLayeredWindow() 的新窗口
  • 如果你的背景是纯色,你总是可以在同一个地方用背景色写同样的文字。

标签: vb.net graphics


【解决方案1】:

我发现以下代码有效:

    Private WithEvents TextForm As New Form
Private Zipper As New FontFamily("Zipper")

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    With TextForm
        .BackColor = Color.DimGray
        .TransparencyKey = Color.DimGray
        .FormBorderStyle = Windows.Forms.FormBorderStyle.None
        .ShowInTaskbar = False
        .WindowState = FormWindowState.Maximized
        .Opacity = 0
        .Show(Me)
    End With
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Static showText As Boolean
    showText = Not showText
    If showText Then TextForm.Opacity = 0.99 Else TextForm.Opacity = 0
End Sub

Private Sub TextForm_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles TextForm.Paint
    e.Graphics.DrawString("text on screen 12345", New Font(Zipper, 30, FontStyle.Bold), Brushes.Red, 50, 50)
End Sub

【讨论】:

    【解决方案2】:

    获取不同的位图并在单独的位图中绘制每个新事物,然后将新位图与旧位图合并。当您要删除文本时,请重新加载没有文本的旧位图。 在新位图中搜索绘图并保存绘图。

    【讨论】:

      【解决方案3】:

      你可以试试这个:

      Dim Graphics0 as Graphics = Graphics.fromHwnd(0) 'This is the desktop's graphics
      Graphics0.DrawText("Test 1..2..3..",New Font(Arial,10),Brushes.Black,New Point(0,0))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        相关资源
        最近更新 更多