1

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

旧版本 VB 中旧 Peek 和 Poke 或 PointSet 和 PointGet 命令的 VB.NET 等效项是什么?

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

4

1 回答 1

1

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

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 方法分别从表格转换为屏幕和屏幕转换为表格坐标。

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

于 2008-12-06T19:44:52.967 回答