Results 1 to 13 of 13

Thread: Using Graphics.Drawline

  1. #1

    Thread Starter
    Junior Member kylek's Avatar
    Join Date
    Oct 2003
    Posts
    21

    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:
    1. Private Sub Panel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint          
    2.      _graphics = e.Graphics      
    3. End Sub
    Then in my function I create a pen, and try to draw my two lines:
    VB Code:
    1. Private Sub DrawBlock(ByVal Cir As Circuit)
    2.           Dim pen As Pen = New Pen(Color.Black, 1)
    3.          
    4.           Dim _Left As Integer = Cir.Left
    5.           Dim _Right As Integer = Cir.Left + Cir.Width
    6.           Dim _Top As Integer = Cir.Top
    7.           Dim _Bottom As Integer = Cir.Top + Cir.Height  
    8.  
    9.           _graphics.DrawLine(pen, _Left, _Top, _Right, _Bottom)
    10.           _graphics.DrawLine(pen, _Left, _Bottom, _Right, _Top)
    11. 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.

  2. #2
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    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:
    1. grph = Me.CreateGraphics
    2.         grph.Clear(Me.BackColor)
    3.  
    4.         Dim pn As New Pen(Color.Black)
    5.  
    6.         grph.DrawLine(pn, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height)
    7.         grph.DrawLine(pn, 0, Me.ClientSize.Height, Me.ClientSize.Width, 0)
    8.  
    9.         grph.Dispose()
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  3. #3

    Thread Starter
    Junior Member kylek's Avatar
    Join Date
    Oct 2003
    Posts
    21
    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

  4. #4

    Thread Starter
    Junior Member kylek's Avatar
    Join Date
    Oct 2003
    Posts
    21
    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:
    1. Public Class Form1
    2.  
    3.     Dim FormBlocks As Blocks
    4.  
    5.     '// Other misc. Code to make the functional form
    6.  
    7.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    8.         Dim graphics As Graphics = e.Graphics
    9.         Dim pen As Pen = New Pen(Color.Black, 1)
    10.  
    11.         Dim workingblock As Blocks.Bock
    12.  
    13.         For Each workingblock In FormBlocks
    14.             graphics.DrawLine(pen, workingblock.Left, workingblock.Top, workingblock.Right, workingblock.Bottom)
    15.             graphics.DrawLine(pen, workingblock.Right, workingblock.Top, workingblock.Left, workingblock.Bottom)
    16.         Next
    17.     End Sub
    18. End Class
    19.  
    20.  
    21. Public Class Blocks
    22.     Inherits System.Collections.CollectionBase
    23.  
    24.     Structure Bock
    25.         Dim Bottom As Integer
    26.         Dim Top As Integer
    27.         Dim Left As Integer
    28.         Dim Right As Integer
    29.     End Structure
    30.  
    31.     '// Other misc. code to make a functional collection
    32.  
    33. 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?

  5. #5
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    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.

  6. #6

    Thread Starter
    Junior Member kylek's Avatar
    Join Date
    Oct 2003
    Posts
    21
    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

  7. #7
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    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.

  8. #8
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    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*

  9. #9

    Thread Starter
    Junior Member kylek's Avatar
    Join Date
    Oct 2003
    Posts
    21
    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.

  10. #10

    Thread Starter
    Junior Member kylek's Avatar
    Join Date
    Oct 2003
    Posts
    21
    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.

  11. #11
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    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:
    1. 'dynamic resize
    2.         PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    3.         'this lets your transparent background show what's beneath
    4.         PictureBox1.BackColor = System.Drawing.Color.Empty
    5.         'go get the file
    6.         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.

  12. #12
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Question 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

  13. #13
    Lively Member
    Join Date
    Oct 2004
    Posts
    68
    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
  •  



Click Here to Expand Forum to Full Width