在屏幕上绘制一个点

发布于 2024-07-10 10:55:14 字数 197 浏览 5 评论 0原文

我需要读取屏幕上计算出的点的颜色并绘制。

VB.NET 中相当于旧版 VB 中的旧 Peek 和 Poke、PointSet 和 PointGet 命令的命令是什么?

或者,有没有一种方法可以使用标签作为 Cursor 对象,这样当我移动它时它就不会删除我的图片框内容。 我不能只制作一个光标图标,因为标签中的文本必须随着我移动光标而改变。

I need to read the color of and draw to a calculated point on the screen.

What is the VB.NET equivalent of the old Peek and Poke, or PointSet and PointGet, commands in older versions of VB?

Or in the alternative, is there a way to use a label as a Cursor object, so that it does not erase my picturebox contents as I move it around. I can't just make a Cursor Icon, because the text in the label has to change as I move the cursor.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2024-07-17 10:55:14

您本身不能使用标签作为光标,但您可以将 Label 组件添加到表单中,并使用消息过滤器与光标同步移动它,如下所示:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        Application.AddMessageFilter(New LabelMoveFilter)

    End Sub

    Private Class LabelMoveFilter
        Implements IMessageFilter

        Public Function PreFilterMessage(ByRef m As Message) As Boolean _
            Implements IMessageFilter.PreFilterMessage

            'If the message is &H200 (WM_MOUSEMOVE), reposition the label to
            'where the cursor has moved to

            If m.Msg = &H200 Then
                Form1.Label1.Location = Form1.PointToClient(Cursor.Position)
            End If

            'Return false so that the message is passed on to the form

            Return False

        End Function

    End Class

End Class

Label 组件(在本例中为 Label1)将不要覆盖表单上的任何内容,它只会位于最上面。 只需确保标签位于表单上所有其他组件的前面,这样它就不会滑到其他任何组件的后面。 然后,您可以在适当的时候将标签的文本设置为您需要的任何内容。


编辑:要回答问题的其他部分...

要获取和设置屏幕上的任意像素,您可以使用 Windows GDI GetPixel 和 SetPixel 函数。 像这样导入它们:

Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer) As Integer
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer, ByVal crColor As Integer) As Integer

像这样调用它们:

color = ColorTranslator.FromOle(GetPixel(GetDC(0), x, y))

然后

SetPixel(GetDC(0), x, y, ColorTranslator.ToOle(color))

其中 x 和 y 是屏幕(不是表单)坐标,颜色是要读取/设置的颜色。 您可以使用 Cursor.Position.X 和 Cursor.Position.Y 获取光标的 X/Y(如果这是您想要的 X 和 Y)。 您可以使用 PointToScreen 和 PointToClient 方法分别将窗体坐标转换为屏幕坐标和将屏幕坐标转换为窗体坐标。

请注意,一旦您设置的任何像素重新绘制,它们就会被覆盖。 请注意,这些也会在您的表单之外读/写,所以要小心。

You can't use a label as a cursor per se, but you can add a Label component to your form and move it around in sync with the cursor with a message filter, like so:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        Application.AddMessageFilter(New LabelMoveFilter)

    End Sub

    Private Class LabelMoveFilter
        Implements IMessageFilter

        Public Function PreFilterMessage(ByRef m As Message) As Boolean _
            Implements IMessageFilter.PreFilterMessage

            'If the message is &H200 (WM_MOUSEMOVE), reposition the label to
            'where the cursor has moved to

            If m.Msg = &H200 Then
                Form1.Label1.Location = Form1.PointToClient(Cursor.Position)
            End If

            'Return false so that the message is passed on to the form

            Return False

        End Function

    End Class

End Class

The Label component (in this example Label1) will not overwrite anything on your form, it will just sit on top. Just make sure that the Label is in front of all other components on the form so that it doesn't slide behind anything else. Then you can just set the text of the label to anything you need whenever appropriate.


Edit: To answer the other part of your question...

To get and set arbitrary pixels on the screen, you can use the Windows GDI GetPixel and SetPixel functions. Import them like this:

Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer) As Integer
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer, ByVal crColor As Integer) As Integer

then call these like:

color = ColorTranslator.FromOle(GetPixel(GetDC(0), x, y))

and

SetPixel(GetDC(0), x, y, ColorTranslator.ToOle(color))

Where x and y are screen (not form) coordinates and color is the Color to read/set. You can get the X/Y of the cursor using Cursor.Position.X and Cursor.Position.Y, if that is the X and Y you want. You can use the PointToScreen and PointToClient methods to convert from form to screen and screen to form coordinates, respectively.

Note that any pixels you set will get overwritten as soon as whatever they are written on repaints itself. And note that these will read/write outside of your form too, so be careful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文