-4

Hi i am wondering how to use DrawString outside the form so I am can see the coordinates of my mouse even when the form is closed.

Can anyone help?

daniel
  • 1
  • 1
  • 6
  • just *what* are you hoping to draw the mouse position on? `Console.WriteLine` might work for debug purposes. – Ňɏssa Pøngjǣrdenlarp Oct 08 '14 at 17:52
  • You'll still need a window to draw on. Trying to draw on the screen is pretty much nothing but a PITA. http://stackoverflow.com/questions/9342570/draw-on-screen-with-gdi-or-gdi-similar-to-inspect You should just create a small form to track the information. Forms can be borderless and transparent to mimic the effect. – TyCobb Oct 08 '14 at 17:56

1 Answers1

1

You always need a form to draw on. The trick is to make everything about the form except your text invisible. This can be done by using the forms Backcolor and TransparencyKey properties. Take this form as an example.

Public Class Form1
    Dim WithEvents timer As New Timer With {.Interval = 500}
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.BackColor = Color.Pink
        Me.TransparencyKey = Color.Pink
        Me.TopMost = True
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        timer.Start()
    End Sub

    Private Sub timer_Tick(sender As Object, e As EventArgs) Handles timer.Tick
        Using g As Graphics = Me.CreateGraphics
            g.Clear(Color.Pink)
            g.DrawString(MousePosition.ToString, New Font("Arial", 20), Brushes.Red, New PointF(10, 10))
        End Using
    End Sub
End Class

Every color on the form that has the TransparencyKey color is transparent. This makes only the text you draw on the form visible. Set the TopMost property to keep the form from dropping behind other applications.

Jens
  • 6,275
  • 2
  • 25
  • 51