Drawing a point on the screen

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

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

Or, alternatively, is there a way to use the label as a Cursor object so that it doesn't delete the content of my image when I move it. I can't just make the cursor icon because the text in the label has to change as the cursor moves

+1


source to share


1 answer


You cannot use a label as a cursor as such, but you can add a Label component to your form and move it in sync with the cursor using a message filter, for example:

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 (Label1 in this example) won't overwrite anything in your form, it will just sit on top. Just make sure the label is in front of all other form components so that it doesn't slide behind anything else. Then you can simply set the shortcut text to whatever you need when needed.


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

To get and set arbitrary pixels on the screen, you can use the Windows GDI functions GetPixel and SetPixel. 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 them like this:

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

      

and

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

      

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

Please note that any pixels you set will be overwritten as soon as they are recorded on the replication itself. Note that they will read and write outside of your form, so be careful.

+1


source







All Articles