-
Sep 24th, 2020, 10:54 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Why does my screen blank ?
Hi,
I have a tool which I programmed several years ago to produce a text file of coordinates to draw a regular polygon. The tool draws these Points around the horizontal centre of the form in order that the largest diameter can be achieved and outputs these Points to a text file.
I am using this tool today as part of a special tool for a specific job, I call the polygon tool, read-in the (x,y) Points and then draw a line from the same origin Point on the screen to each Point given in the text file to form a 'star'.
The difference is that in the new tool, the form's background colour and the pen colour are to be chosen before drawing.
I had a bit of trouble detecting a miss-spelt or unknown colour, just converting the String to a Color doesn't automatically cause an error, it just gives 'Black', but found a way around that... I think !
I run the tool, the 'star' is drawn, so far, so good, then the screen immediately goes blank... I can't find why.
I have also found that the blank form, (blank except for the 'Exit' button, all other controls are 'Hidden') is not the colour given in the design page (Lavender) but the form's default colour 'Control'.
Code:
Private Sub MakeDrawing()
Dim back, ink As Color, pen1 As Pen
Dim gr As Graphics = CreateGraphics()
Dim memo As Color = Me.BackColor
Try
ink = Color.FromName(TextBox2.Text)
Me.BackColor = ink ' This is to detect colour error.
back = Color.FromName(TextBox1.Text)
Me.BackColor = back ' This is to detect colour error.
Catch ex As Exception
Me.BackColor = memo
StartUp()
Label3.Show() ' Tell the user that there's a colour error.
Exit Sub
End Try
Me.BackColor = Nothing ' This is required else no drawing shown.
gr.Clear(back)
pen1 = New Pen(ink, 1)
For i = 0 To cords.Count - 1
gr.DrawLine(pen1, centre, cords(i))
Next
End Sub
I must be doing something wrong, or not doing something I ought to be doing... But what ?
Poppa
PS.
Typical text file:
Radius : 200
Centre : 432
1: 561, 585 Green start
2: 629, 467
3: 605, 332
4: 500, 244
5: 364, 244
6: 259, 332
7: 235, 467
8: 303, 585
9: 432, 632 Red finish
Pop.
Last edited by Poppa Mintin; Sep 24th, 2020 at 11:00 AM.
Along with the sunshine there has to be a little rain sometime.
-
Sep 24th, 2020, 11:06 AM
#2
Re: Why does my screen blank ?
NEVER call CreateGraphics. Draw on the Paint event of the relevant control.
-
Sep 24th, 2020, 11:13 AM
#3
Re: Why does my screen blank ?
Originally Posted by Poppa Mintin
I had a bit of trouble detecting a miss-spelt or unknown colour, just converting the String to a Color doesn't automatically cause an error, it just gives 'Black'
vb.net Code:
Dim names = {"Red", "Rid"}
For Each s In names
If [Enum].IsDefined(GetType(KnownColor), s) Then
Console.WriteLine($"There is a known colour named '{s}'.")
Else
Console.WriteLine($"There is NOT a known colour named '{s}'.")
End If
Next
-
Sep 24th, 2020, 05:35 PM
#4
Thread Starter
PowerPoster
Re: Why does my screen blank ?
Thanks John,
I've not addressed the colour problem yet, that's working for now.
I've amended my method to what I think you mean. There is an improvement, the back colour remains as I set it, and the screen no longer blanks out, that's successful.
The lines though are not so good. using a high number (360) of long lines, and watching very carefully, I can see that all the lines are drawn, and with only six lines, stepping through one at a time I can see the instructions being issued.
Trouble is... When complete only the last line to be drawn remains visible.
Code:
' Global variables.
Dim back, ink As Color
Dim centre, spot As Point
Dim cords As New List(Of Point)
Dim ok As Boolean
Dim pen1 As Pen
' ________________________
Private Sub MakeDrawing()
Dim back, ink As Color
Dim memo As Color = Me.BackColor
Try
ink = Color.FromName(TextBox2.Text)
Me.BackColor = ink ' This is to detect colour error.
back = Color.FromName(TextBox1.Text)
Me.BackColor = back
Catch ex As Exception
Me.BackColor = memo
StartUp()
Label3.Show() ' Inform user there's a problem.
Exit Sub
End Try
pen1 = New Pen(ink, 1)
For i = 0 To cords.Count - 1
spot = cords(i)
ok = True
Me.Refresh()
Next
End Sub
Private Sub Me_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If Not ok Then Exit Sub
ok = False
e.Graphics.DrawLine(pen1, centre, spot)
End Sub
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Sep 24th, 2020, 06:09 PM
#5
Re: Why does my screen blank ?
You're only passing one point at a time...
Code:
Private Sub MakeDrawing()
Dim back, ink As Color
Dim memo As Color = Me.BackColor
Try
ink = Color.FromName(TextBox2.Text)
Me.BackColor = ink ' This is to detect colour error.
back = Color.FromName(TextBox1.Text)
Me.BackColor = back
Catch ex As Exception
Me.BackColor = memo
StartUp()
Label3.Show() ' Inform user there's a problem.
Exit Sub
End Try
pen1 = New Pen(ink, 1)
For i = 0 To cords.Count - 1
spot = cords(i)
ok = True
Me.Refresh()
Next
End Sub
Private Sub Me_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
'this is what happens
'clear drawing area
If Not ok Then Exit Sub
ok = False
'draw one line
e.Graphics.DrawLine(pen1, centre, spot)
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 24th, 2020, 06:11 PM
#6
Re: Why does my screen blank ?
Why not use [graphics].DrawLines, and pass in the whole cords array?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 24th, 2020, 06:27 PM
#7
Thread Starter
PowerPoster
Re: Why does my screen blank ?
Originally Posted by .paul.
Why not use [graphics].DrawLines, and pass in the whole cords array?
Thanks .Paul, isn't that what John was so forceful in saying don't use it ?
I've tried DrawLines but can't find how to do that without using CreateGraphics.
Even so, the lines are being drawn, why are they not visible ? Surely drawing one line doesn't erase previously drawn lines.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Sep 24th, 2020, 06:47 PM
#8
Re: Why does my screen blank ?
Not CreateGraphics. In the Paint Event you’re using DrawLine. There is an alternative DrawLines, which draws multiple lines...
They’re not visible because all you’re seeing is the last line. Every time you Refresh, the Paint Event is called, which erases all drawing on the surface (in this case your Form), then any drawing specified in the event is drawn
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 24th, 2020, 08:00 PM
#9
Re: Why does my screen blank ?
Originally Posted by Poppa Mintin
I've tried DrawLines but can't find how to do that without using CreateGraphics.
What's that got to do with it? CreateGraphics creates a Graphics object. The Paint event handler provides a Graphics object. Why can you do something with one of those but you can't do the same with the other?
Originally Posted by Poppa Mintin
the lines are being drawn, why are they not visible ? Surely drawing one line doesn't erase previously drawn lines.
No, drawing a line doesn't erase previously drawn lines. Calling Refresh does though. When the Paint event is raised, the area of the control being repainted that was invalidated gets erased. When you call Refresh, you are invalidating the entire control, so everything you previously drew on the control gets erased.
GDI+ drawing is easy but you need to understand the basic principles involved. The Paint event is raised often so it is never safe to draw on a control anywhere else. You need to declare one or more fields to store the data that represents your entire drawing. In the Paint event handler, you read that data and you use the Graphics object provided to draw what it represents. Any time the drawing needs to change, you modify the data stored in that field(s) and then call Invalidate. That will queue a Paint event so your drawing will be updated.
It is preferable to call Invalidate with an argument that represents only the area of the control that has or may have changed. That will ensure that the minimum number of pixels get repainted. It is that repainting that is the slow part of the operation and causes flickering when lots of Paint events are raised. When you call Refresh, it calls Invalidate without an argument, so the entire control will be repainted, and then it calls Update, which forces the Paint event to be raised immediately rather than waiting until any currently executing event handler completes. I suggest that you follow the CodeBank link in my signature below and check out my Very Simple Drawing Program thread for an example.
-
Sep 25th, 2020, 04:08 AM
#10
Re: Why does my screen blank ?
Originally Posted by .paul.
Why not use [graphics].DrawLines, and pass in the whole cords array?
Graphics.DrawLines draws a polyline, i.e a series of lines joined end to end. Poppa apparently wants to draw separate lines radiating from a centre, so Graphics.DrawLine is needed. BB
-
Sep 25th, 2020, 04:31 AM
#11
Re: Why does my screen blank ?
Originally Posted by boops boops
Graphics.DrawLines draws a polyline, i.e a series of lines joined end to end. Poppa apparently wants to draw separate lines radiating from a centre, so Graphics.DrawLine is needed. BB
Even DrawLine, for multiple lines should be completed in one execution of the Paint Event...
Code:
Private Sub Me_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
For i = 0 To cords.Count - 1
e.Graphics.DrawLine(pen1, centre, cords(i))
Next
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 25th, 2020, 08:06 AM
#12
Thread Starter
PowerPoster
Re: Why does my screen blank ?
Originally Posted by .paul.
Even DrawLine, for multiple lines should be completed in one execution of the Paint Event...
Code:
Private Sub Me_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
For i = 0 To cords.Count - 1
e.Graphics.DrawLine(pen1, centre, cords(i))
Next
End Sub
Yes Paul, thank you, that's what I discovered works.
I've finished up with this...
Code:
Private Sub MakeDrawing()
Dim back, ink As Color
Dim memo As Color = Me.BackColor
Try
ink = Color.FromName(TextBox2.Text)
Me.BackColor = ink ' This is to detect colour error.
pen1 = New Pen(ink, 1)
back = Color.FromName(TextBox1.Text)
Me.BackColor = back ' Also to detect colour error.
PictureBox1.BackColor = back
Catch ex As Exception
Me.BackColor = memo
StartUp()
Label3.Show()
Exit Sub
End Try
PictureBox1.Show()
End Sub
Private Sub DrawAll(sender As System.Object, e _
As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
For i = 0 To cords.Count - 1
e.Graphics.DrawLine(pen1, centre, cords(i))
Next
ok = True
End Sub
I've left the question of colour detection for now, I'll make that a separate thread.
Everything else works as I'd hoped, to reduce the problem with using the Form's Paint event I've added a PictureBox and drawn the lines in there. I've had to adjust the coordinates to account for the difference between the original centre location and the centre of the 'box of course, but that all adds to the fun.
Making the form's background colour the same as that of the 'box gives the same effect as if the 'box wasn't even there ('box no border).
Thanks for your help guys.
I shall mark this thread as resolved.
Poppa
Along with the sunshine there has to be a little rain sometime.
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
|