Can't find any way to remove graphics after drawing them.

Evan1993

Well-known member
Joined
Apr 2, 2008
Messages
47
Programming Experience
1-3
My program is supposed to draw a red rectangle around itself, wich it does fine. However each time it redraws it leaves behind red marks.

I have tryed Invalidating the desktop (the whole screen), wich only made it worse. (Becuase it made my program redraw, wich left even more marks)

Any suggestions?

Heres my code.
VB.NET:
 Sub UpdateLoc()
        Dim us As Process = Process.GetCurrentProcess()
        Dim struct As New RECT
        GetWindowRect(us.MainWindowHandle, struct)
        FormRect = struct.ToRectangle
    End Sub
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim hdc As IntPtr = GetDC(IntPtr.Zero)
        Dim p As New Pen(Color.Red, 4)
        UpdateLoc()
        Try
            ' Get the graphics object for the desktop window
            Dim g As Graphics = Graphics.FromHdc(hdc)

            Try
                ' Draw a  red rectangle around a program (Currently this one)
                g.DrawRectangle(p, FormRect)
            Finally
                ' Dispose the graphics object, failing to do this will cause system instability
                g.Dispose()
            End Try
        Finally
            ' Release the handle to the desktop window, again, failing to do this is a bad idea.
            ReleaseDC(IntPtr.Zero, hdc)
        End Try
    End Sub
 
Still leaves stuff behind. :(

VB.NET:
 Sub UpdateLoc()
        Dim us As Process = Process.GetCurrentProcess()
        Dim struct As New RECT
        GetWindowRect(us.MainWindowHandle, struct)
        FormRect = struct.ToRectangle

    End Sub
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        'Dim hdc As IntPtr = GetDC(IntPtr.Zero)
        Dim hdc As IntPtr = CreateDC("DISPLAY", "", "", "")
        If CreateDC("DISPLAY", "", "", "") = 0 Then
            Me.Text = "Failed to create the DC"
        End If
        Dim p As New Pen(Color.Red, 4)
        UpdateLoc()

        Try
            ' Get the graphics object for the desktop window
            Dim g As Graphics = Graphics.FromHdc(hdc)
            Try
                ' Draw a  red rectangle around a program (Currently this one)
                g.DrawRectangle(p, FormRect)
            Finally
                ' Dispose the graphics object, failing to do this will cause system instability
                g.Dispose()
            End Try
        Finally
            ' Release the handle to the desktop window, again, failing to do this is a bad idea.
            ' From MSDN: If the function succeeds, the return value is nonzero.
            ' If the function fails, the return value is zero.
            'ReleaseDC(GetDesktopWindow(), hdc)

            If DeleteDC(hdc) = 0 Then
                Me.Text = "Failed to release the DC"
            End If
            DeleteDC(hdc)
        End Try
    End Sub
 
This simply draws a rectangle around the outer edge of the client area in the form with a width of 4
VB.NET:
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim gr As Graphics = Me.CreateGraphics
        gr.DrawRectangle(New Pen(Color.Red, 4), New Rectangle(0, 0, Me.ClientSize.Width, Me.ClientSize.Height))
        gr.Dispose()
    End Sub
 
This simply draws a rectangle around the outer edge of the client area in the form with a width of 4

Won't be able to do what I want.

I want it to be on the outer edge (I.e, around the border and title bar), and I want to be able to select any prossess.

Thanks for your time.
 
Since JuggaloBrotha didn't seem to get what I wanted I took some SS's and updated my code.

Heres what it looks like when you don't move any of the windows.


Heres what it looks like if you move the window all over the screen.



In the end i want it to make a red rectangle around a window when you move your mouse over it.

Right now it just makes a red rectagle around all windows.

Heres my unfinished code...
VB.NET:
  Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim Programs As Process()
        Dim pr As Process
        'Dim hdc As IntPtr = GetDC(IntPtr.Zero)
        Dim hdc As IntPtr = CreateDC("DISPLAY", "", "", "")
        If CreateDC("DISPLAY", "", "", "") = 0 Then
            Me.Text = "Failed to create the DC"
        End If
        Dim p As New Pen(Color.Red, 4)
        '   UpdateLoc()
        Dim g As Graphics = Graphics.FromHdc(hdc)
        Programs = Process.GetProcesses()
        Dim struct As New RECT
        Try
            For Each pr In Programs
                If pr.MainWindowTitle.Length > 0 Then
                    GetWindowRect(pr.MainWindowHandle, struct)
                    FormRect = struct.ToRectangle
                    Try
                        ' Get the graphics object for the desktop window
                        'MessageBox.Show(pr.MainWindowTitle)

                        Try
                            g.DrawRectangle(p, FormRect)
                        Catch x1 As Exception
                            'MessageBox.Show(x1.Message)
                        Finally
                            ' Dispose the graphics object, failing to do this will cause system instability

                        End Try
                    Finally
                        ' Release the handle to the desktop window, again, failing to do this is a bad idea.
                        ' From MSDN: If the function succeeds, the return value is nonzero.
                        ' If the function fails, the return value is zero.
                        'ReleaseDC(GetDesktopWindow(), hdc)

                    End Try
                End If
            Next

        Finally

            If DeleteDC(hdc) = 0 Then
                Me.Text = "Failed to release the DC"
            End If
            DeleteDC(hdc)
            g.Dispose()
        End Try
    End Sub
 
I haven't messed with trying to draw on or over the actual windows desktop...

Seems like what you might need to do is draw on a transparent layer above the window background image? Then it's just a matter of wiping and redrawing the transparent layer.
 
Back
Top