?
Solved

vb.net graphics draw line above all controls

Posted on 2009-04-01
4
Medium Priority
?
2,683 Views
Last Modified: 2012-05-06
I have a form and want to draw a diagnol line accross it. This is how I'm drawing the line.

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

g.DrawLine(Pens.Red, ClientRectangle.X, ClientRectangle.Y, ClientRectangle.X + ClientRectangle.Width, ClientRectangle.Y + ClientRectangle.Height)

end sub

I also have a few controls sited on the form. The line draws on the forms surface only and is hidden behind each of the controls. How can I draw lines (or graphic object in general) that draws at the highest z order so that they appear to be on top of everything. Any easy way to do this?
0
Comment
Question by:glenn_r
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

Expert Comment

by:oobayly
ID: 24042148
If you store the screen coordinates for the top-left & bottom-right corners of the form and hook the paint event for each control, you can draw the line across all the controls.

Currently doesn't draw on textboxes, comboboxes, listboxes and scrollbars though, but it is a first attempt.
    Private corners As Point()
 
    Private Sub HookPaint(ByVal control As Control)
        ' Hook the paint event for the control, and do for an child controls
        AddHandler control.Paint, AddressOf control_Paint
        For Each child As Control In control.Controls
            HookPaint(child)
        Next
    End Sub
 
    Private Sub control_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
        ' Convert screen point to the control's coordinates
        Dim local As Point() = New Point() { _
            DirectCast(sender, Control).PointToClient(corners(0)), _
            DirectCast(sender, Control).PointToClient(corners(1))}
 
        e.Graphics.DrawLine(Pens.Black, local(0), local(1))
    End Sub
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        ' Convert the form's client corners to screen cordinates
        corners = New Point() { _
            Me.PointToScreen(Me.ClientRectangle.Location), _
            Me.PointToScreen(New Point(Me.ClientRectangle.Right, Me.ClientRectangle.Bottom))}
        HookPaint(Me)
    End Sub

Open in new window

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1500 total points
ID: 24043528
You can draw on the desktop hDC...but this method is NOT peristent.  It may fit your requirements, though, just depends what you're doing.
Public Class Form1
 
    Private Declare Function GetDesktopWindow Lib "user32" () As IntPtr
    Public Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hWnd As IntPtr) As Integer
    Public Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hWnd As IntPtr, ByVal hDc As Integer) As Integer
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim deskWnd As IntPtr = GetDesktopWindow()
        Dim hDc As Integer = GetWindowDC(deskWnd)
        Dim g As Graphics = Graphics.FromHdc(hDc)
        g.DrawLine(Pens.Red, Me.Location.X, Me.Location.Y, Me.Location.X + Me.Size.Width, Me.Location.Y + Me.Size.Height)
        g.Dispose()
        ReleaseDC(deskWnd, hDc)
    End Sub
 
End Class

Open in new window

DesktopDC.jpg
0
 

Author Comment

by:glenn_r
ID: 24100146
oobayly I like your solution. The fact that your working within the confines of .net interests me. How do you think we can get the remaining controls (textboxes, scrollbars, etc. painted with the line?

Thanks,
Glenn
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24100418
To be quite honest, I'm not too sure. I also reckon it'd be a a great deal of work. Subclassing the controls isn't really an option as there may any amount of types of controls on the form.

Personally, I'd go with Idle_Mind's solution. Using win32 calls isn't a major issue. and it does draw across all the child controls.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question