Results 1 to 12 of 12

Thread: Drawing lines

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    103

    Question Drawing lines

    This might be a dumb question, but I have been searching and have not found an answer – please help.

    I am writing a VB.net application. The first thing I do, in code, is place a number of buttons on the form. The next thing I want to do is draw some lines on the form. Based on that which I have seen, this should be (?) done in the form’s paint event (using DrawLine). The problem I am having is that the lines are being drawn behind the buttons and I want them in front of the buttons. Please offer some suggestions as to how to make this happen. Thanks.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Drawing lines

    You’d have to draw the section of the line that crosses the Button as a line on the Button in the Button’s Paint event

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Drawing lines

    Alternatively, draw directly on the screen. In your Form_Paint event…

    Code:
    Dim gr as Graphics = Graphics.FromHWnd(IntPtr.Zero)
    ‘ then use gr to draw your line relative to the Form

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Drawing lines

    Another alternative, that I’ve never tried and can’t test at the moment, is to create a NativeWindow Class initialised with the screen handle (IntPtr.Zero). Hypothetically you could then override the OnPaint event and draw your lines there. The problem with drawing to the screen is that you might end up with more lines than you intended due to the screen not being repainted when you need it to. This NativeWindow approach might be the solution to that problem…

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Drawing lines

    Quote Originally Posted by .paul. View Post
    You’d have to draw the section of the line that crosses the Button as a line on the Button in the Button’s Paint event
    If you want to go that way, check this out.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    103

    Re: Drawing lines

    Thanks for the replies

    Jmcilhinney – I tried the code in the referenced article, but the lines drawn were still obscured by the buttons

    .paul – Tried drawing directly on the screen, and the lines kinda appeared – but not where they belonged and outside of the form. I want to try the Native Window idea, but was unable to find anything to show me how to do that.

    If it matters at all, I can post my code – all 150 lines of it…

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Drawing lines

    Quote Originally Posted by groston View Post
    Jmcilhinney – I tried the code in the referenced article, but the lines drawn were still obscured by the buttons
    I just tested the code from that thread and it worked exactly as expected, so you almost certainly did something wrong. As you haven't shown us what you actually did, we can't tell you what's wrong with it. Here's what I did:

    1. Created a new WinForms App project. I tested against both .NET Framework 4.8.1 and.NET 6.
    2. Added two Buttons, two Labels and two TextBoxes to the form and positioned them so they were along the diagonal of the form.
    3. Copied the code form post #1 of that CodeBank thread and pasted it over the code of the form.
    4. Ran the project.

    The form displayed with a line along the diagonal over the top of the Buttons and Labels. The TextBoxes did not show the line, because of the way WinForms paints them.

    For the record, this is exactly the sort of thing you should be doing when specific things don't work the way you expect. Create test projects that isolate the relevant functionality so you can mess around and mess up as much as you want without affecting or being affected by the rest of your project. This is an integral part of software development.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Drawing lines

    Quote Originally Posted by groston View Post
    .paul – Tried drawing directly on the screen, and the lines kinda appeared – but not where they belonged and outside of the form
    The lines are in screen coordinates, not form coordinates. As I said you need to draw them relative to your Form…

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Drawing lines

    Code:
    Dim l as integer = Me.PointToScreen(Point.Empty).X + where you want to draw X
    Dim t as integer = Me.PointToScreen(Point.Empty).Y + where you want to draw Y

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Drawing lines

    Quote Originally Posted by .paul. View Post
    The lines are in screen coordinates, not form coordinates. As I said you need to draw them relative to your Form…
    For this purpose, you can use the same PointToScreen that I use in my CodeBank thread. For instance, this:
    vb.net Code:
    1. Dim topLeft = PointToScreen(Point.Empty)
    will give you the screen coordinates of the top-left corner of the form's client area.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    103

    Re: Drawing lines

    My bad - sorry. I missed the AddHandler line in the first part. Once I added that, it worked as expected. Thanks!

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Drawing lines

    Quote Originally Posted by groston View Post
    My bad - sorry. I missed the AddHandler line in the first part. Once I added that, it worked as expected. Thanks!
    As I said in that CodeBank thread, you can register the event handler any way you want. That code will attach it to every control on the form at the time but you can also attach it to specific controls. For instance, assuming that you have already created the event handler for the form, you can select all the Buttons in the designer, open the Properties window, click the Events button and then select that method for the Paint event. That will add just those Buttons to the Handles clause of the method.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width