|
-
Oct 22nd, 2003, 12:13 PM
#1
Thread Starter
Junior Member
Using Graphics.Drawline
How do I use the Graphics.Drawline function in the middle of another function on my form.
What I want to do is just draw an X across part of my form, but where, and what size changes.
I think my problem is in creating the graphics object. What I did was create a global graphics variable called '_graphics' and in the myBase.Paint event I set that global variable to the forms graphics object:
VB Code:
Private Sub Panel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
_graphics = e.Graphics
End Sub
Then in my function I create a pen, and try to draw my two lines:
VB Code:
Private Sub DrawBlock(ByVal Cir As Circuit)
Dim pen As Pen = New Pen(Color.Black, 1)
Dim _Left As Integer = Cir.Left
Dim _Right As Integer = Cir.Left + Cir.Width
Dim _Top As Integer = Cir.Top
Dim _Bottom As Integer = Cir.Top + Cir.Height
_graphics.DrawLine(pen, _Left, _Top, _Right, _Bottom)
_graphics.DrawLine(pen, _Left, _Bottom, _Right, _Top)
End Sub
When it gets to the first _graphics.DrawLine(...) I get an error stating "Invalid Paramater Used".
Any Ideas? I'm likely just using the graphics class wrong.
-
Oct 23rd, 2003, 10:22 AM
#2
Addicted Member
Place the following code in the paint event of the form. Ensure that you have a form-wide Graphics object declared, named grph. This is not the most efficient way of doing this, but it gets you started.
VB Code:
grph = Me.CreateGraphics
grph.Clear(Me.BackColor)
Dim pn As New Pen(Color.Black)
grph.DrawLine(pn, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height)
grph.DrawLine(pn, 0, Me.ClientSize.Height, Me.ClientSize.Width, 0)
grph.Dispose()
-
Oct 23rd, 2003, 08:33 PM
#3
Thread Starter
Junior Member
That works to draw a single X on the form.
But I dont want to always draw it. and I may want to draw 2, or 3.
Maybe this isn't possible, I think I can work around it if the only time to paint things on the form is during the on paint event, I may be able to do it, I just didn't think this would be a very pretty way to do it.
Thanks though
-
Oct 23rd, 2003, 09:06 PM
#4
Thread Starter
Junior Member
Thinking more about how I can make it work using the on paint event.
I would think that if I created a collection (or array) that would hold my locations for the top, bottom, left and right edges of the X to be drawn. Then in the on paint loop though each and draw.
VB Code:
Public Class Form1
Dim FormBlocks As Blocks
'// Other misc. Code to make the functional form
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim graphics As Graphics = e.Graphics
Dim pen As Pen = New Pen(Color.Black, 1)
Dim workingblock As Blocks.Bock
For Each workingblock In FormBlocks
graphics.DrawLine(pen, workingblock.Left, workingblock.Top, workingblock.Right, workingblock.Bottom)
graphics.DrawLine(pen, workingblock.Right, workingblock.Top, workingblock.Left, workingblock.Bottom)
Next
End Sub
End Class
Public Class Blocks
Inherits System.Collections.CollectionBase
Structure Bock
Dim Bottom As Integer
Dim Top As Integer
Dim Left As Integer
Dim Right As Integer
End Structure
'// Other misc. code to make a functional collection
End Class
I havent tested it, since I am at home and dont have the program to actually try it on. But it seems like that should work.
Any thoughts?
-
Oct 23rd, 2003, 10:29 PM
#5
Lively Member
I think you may have better luck creating pictureboxes (either dynamically or at build time) to hold your various graphics, because won't they persist better than drawing crap directly onto the form? Seems like that would be simpler and more extensible than redrawing all the items from scratch on every paint event.
-
Oct 23rd, 2003, 10:53 PM
#6
Thread Starter
Junior Member
It shouldn't actually ever be more than 4 lines to draw. I dont know much about the actuall process for painting the form, but I dont imagine that doing 4 more lines should be much of an effect.
I dont really have "Various" graphics. Its just that my form (which I don't pretend to be very efficent). is designed to match the output which it produces. So I have groups of text boxes that under certain circumstances need to be disabled, and visually crossed.
The Current Form
Exampe of what I want to be able to do
-
Oct 23rd, 2003, 11:25 PM
#7
Lively Member
Oh I'm not saying your first instinct wouldn't work - it sounds dead certain that it would - but it might actually be more work to get it to behave correctly in situations that a user is going to put you in, e.g. when they resize the form. Manual resize code is a pain, and it's handy to use dock properties - of course you could also make the form unsizable.
You are on the right track with OnPaint, if you draw straight onto the form, because any situation where the form is not visible (e.g. user switches to another form, minimizes, etc.) your graphics will go away. This is why I'm thinking you may be better served if you use one or more pictureboxes, because they redraw themselves automatically.
How are you already getting the sideways text and gridlines? DrawString and DrawLine? I don't think you have to worry too much about efficiency because the amount of drawing you're doing is so small.
-
Oct 23rd, 2003, 11:44 PM
#8
Lively Member
Huh, as I try this myself it actually isn't very helpful to draw inside a PictureBox, you get the same behavior (graphic goes away when form is not visible). I guess because I am drawing straight onto the control and its Image property doesn't actually contain anything. *scratches head*
-
Oct 23rd, 2003, 11:48 PM
#9
Thread Starter
Junior Member
Thanks for the thoughts.
Oh I'm not saying your first instinct wouldn't work - it sounds dead certain that it would - but it might actually be more work to get it to behave correctly in situations that a user is going to put you in, e.g. when they resize the form. Manual resize code is a pain, and it's handy to use dock properties - of course you could also make the form unsizable.
The form border is fixed, so no worries about writing resize code (I know what a pain that can be I built a custom control that does some).
You are on the right track with OnPaint, if you draw straight onto the form, because any situation where the form is not visible (e.g. user switches to another form, minimizes, etc.) your graphics will go away. This is why I'm thinking you may be better served if you use one or more pictureboxes, because they redraw themselves automatically.
Isn't the OnPaint event triggered in such a situation? When the user moves away/hides the form, then brings it back up? Or am I mistaken, and there are situations where OnPaint wont be called causing the form to not draw as expected?
How are you already getting the sideways text and gridlines? DrawString and DrawLine? I don't think you have to worry too much about efficiency because the amount of drawing you're doing is so small.
Ah, now thats a tricky one... Most of the graphical layout is actually an image, just droped on the form also (thus the inefficient part). When I get things actually working, I may remove the image and replace it with code to draw the lines and text. But for now I'm not concerned with it.
-
Oct 23rd, 2003, 11:52 PM
#10
Thread Starter
Junior Member
Originally posted by Crunch
Huh, as I try this myself it actually isn't very helpful to draw inside a PictureBox, you get the same behavior (graphic goes away when form is not visible). I guess because I am drawing straight onto the control and its Image property doesn't actually contain anything. *scratches head*
Oh I see where you were trying to go with this... I thought you meant for me to create an image (like in paint) and change its size on the form.
I think I'll stick with just painting on the form directly.
-
Oct 23rd, 2003, 11:59 PM
#11
Lively Member
Yeah you are probably better off, although if you find an easy way to set the Image property of a picturebox to a graphic drawn at runtime, please post it here, I'm curious 
//I thought you meant for me to create an image (like in paint) and change its size on the form.
Actually I tried this also and that turned out to be pretty simple, although it's definitely a backward way to do it - you create a graphic and save it as .GIF with transparent background, and set its properties as you like - Top, Left, Height and Width to get it where you like, and...
VB Code:
'dynamic resize
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
'this lets your transparent background show what's beneath
PictureBox1.BackColor = System.Drawing.Color.Empty
'go get the file
PictureBox1.Image = System.Drawing.Image.FromFile("foo.gif")
...NOT a way I would really recommend though.
Last edited by Crunch; Oct 24th, 2003 at 12:04 AM.
-
Oct 28th, 2004, 09:41 AM
#12
Hyperactive Member
Persistent graphics on form or picturebox
How can I draw something - once - so it will stay?
I tried using the System.Drawing and CreateGraphics... stuff on the form, looks good, but as soon as anything obscures the form (minimize, taskbar popup, msgbox etc) the drawn graphics dissappear.
I tried drawing to a picturebox rather than the form and it does the same thing. This seems pretty useless!
I realize I can redraw the entire graphic in the Paint event, but that seems just too wierd, the entire program will end up in the Paint event because the graphic is being drawn dynamically based on various conditions.
There has to be a better way.
Thanks, DaveBo
"The wise man doesn't know all the answers, but he knows where to find them."
VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15
-
Oct 28th, 2004, 02:29 PM
#13
Lively Member
I tried drawing to a picturebox rather than the form and it does the same thing. This seems pretty useless!
Why? Besides using DX windows application were not meant for this. Therefore anything YOU need to draw, will be placed in the Paint event.
I realize I can redraw the entire graphic in the Paint event, but that seems just too wierd, the entire program will end up in the Paint event because the graphic is being drawn dynamically based on various conditions.
Welcome to video game programming. Anything that draws dynamically (ie. will change) must be cleared and redrawn so as to not leave traces of the old. This is what the Paint method does. By the time it's called everything is erased, and it's up to you to redraw it again. Think of moving a button on the screen. The form is cleared (or a portion on it) and the button is redrawn at the new location. VB just does this behind the sences for you.
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
|