views:

359

answers:

1

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

+1  A: 

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.

Eric Rosenberger