I'm attempting to make a program that will simply draw a guide (consisting of two perpendicular lines) on another program's window. I tried using a form with a transparency key and TopMost set to true, but then whenever the user inadvertently clicked the guide, it would un-focus the application. This happened a lot, and was a great source of annoyance... So I did some research, and the only lead I could find was a suggestion to "Draw directly on the other application's window". I can't find anything that tells me how to do this, or if its even possible... So I need to know one of three things:

1) Is it possible?
2) How do you do it?
or 3) is there some way to make a form completely un-focusable?


The effect I'm trying to get is so that it looks like someone physically drew the line on the monitor itself.

Recommended Answers

All 3 Replies

Hey Xcelled, I worked out some code for you. I tried to explain most of it in the comments, but the user is just clicking a button on one form (in this case Form1) and the lines are being drawn on another form (in this case Form2)

Hope this code helps, if you can explain further what you mean by making a form completely un-focusable I'll see what I can do to help you with that too.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim thepen As New Pen(Color.Blue)                       '* This can be set to any color you want
        '* The first set of coordinates (20, 20) are the start point of the line
        '* The second set of coordinates (20, 50) are the end point of the line
        Form2.CreateGraphics.DrawLine(thepen, 20, 10, 20, 50)     '* Draws one line on the Form2
        Form2.CreateGraphics.DrawLine(thepen, 30, 10, 30, 50)     '* Draws the second line on Form2
        ''return the current form as a drawing surface
    End Sub
End Class

See if this helps. (code originated from here)

Public Class Form1

    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Integer) As Integer
    Private Declare Function InvalidateRect Lib "user32" (ByVal hwnd As IntPtr, ByVal lpRect As IntPtr, ByVal bErase As Boolean) As Boolean
    Private WithEvents myTimer As New Timer With {.Interval = 100, .Enabled = True}

    Private Sub drawOnDesktop()
        Dim HDC As Integer = GetDC(0)
        Dim myGraphics As Graphics = Graphics.FromHdc(HDC)
        Dim myPen As New Pen(Color.Chartreuse, 30) '// set pen color and width.
        myGraphics.DrawLine(myPen, 150, 255, 650, 255) '// (preset pen, startLocation.X, startLocation.Y, endLocation.X, endLocation.Y)
        myGraphics.DrawLine(myPen, 150, 355, 650, 355) '// (preset pen, startLocation.X, startLocation.Y, endLocation.X, endLocation.Y)
        myGraphics.Dispose()
    End Sub

    Private Sub myTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles myTimer.Tick
        Me.drawOnDesktop()
    End Sub

    Private Sub clearDesktopDrawing()
        InvalidateRect(IntPtr.Zero, IntPtr.Zero, False)
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.clearDesktopDrawing()
    End Sub
End Class

Thanks for your replies. Altarium, that's not quite what I'm looking for; the other application isn't mine.

codeorder, your suggestion looks promising, but i want to draw on a program, not the desktop. So I would just replace the Desktop Handle finder with one for the program, and use that instead?

Also, will I have to re-draw the guide whenever the program refreshes itself?

Thanks for the lead, I'm checking into it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.