-
Jan 27th, 2023, 04:46 PM
#1
Thread Starter
Lively Member
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.
-
Jan 27th, 2023, 07:28 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 27th, 2023, 07:36 PM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 27th, 2023, 07:55 PM
#4
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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 27th, 2023, 09:05 PM
#5
Re: Drawing lines
Originally Posted by .paul.
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.
-
Jan 28th, 2023, 10:54 PM
#6
Thread Starter
Lively Member
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…
-
Jan 28th, 2023, 11:06 PM
#7
Re: Drawing lines
Originally Posted by groston
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.
Last edited by jmcilhinney; Jan 28th, 2023 at 11:54 PM.
-
Jan 28th, 2023, 11:50 PM
#8
Re: Drawing lines
Originally Posted by groston
.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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 28th, 2023, 11:54 PM
#9
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 28th, 2023, 11:59 PM
#10
Re: Drawing lines
Originally Posted by .paul.
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:
Dim topLeft = PointToScreen(Point.Empty)
will give you the screen coordinates of the top-left corner of the form's client area.
-
Jan 29th, 2023, 12:08 AM
#11
Thread Starter
Lively Member
Re: Drawing lines
My bad - sorry. I missed the AddHandler line in the first part. Once I added that, it worked as expected. Thanks!
-
Jan 29th, 2023, 12:15 AM
#12
Re: Drawing lines
Originally Posted by groston
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|