none
Drawing Rectangles RRS feed

  • Question

  •  

    Ok, im so frustrated with drawing in Vb .net that i dont want links or anything. Im trying to draw a single point with vb .net.

    Dim test As System.IntPtr

    Dim asdf As System.Drawing.Graphics = System.Drawing.Graphics.FromHwndInternal(test)

    Dim pen1 As New System.Drawing.Pen(Color.Black)

    Dim rect As New System.Drawing.Rectangle(50, 50, 100, 100)

    asdf.DrawRectangle(Pens.Black, rect)

     

    thats my code. whats wrong?

    Friday, March 14, 2008 1:13 AM

Answers

  • When you draw something, the first thing to decide is what are you going to draw on. You can draw on the surface of a Form, or on the surface of any other Control, or on a Bitmap. Next you have to decide how to get a Graphics object.

    Form/Control

    The standard way is to override the form or control's Paint event and use the Graphics object provided by the event arguments. You can also simply handle the Paint event. (see Bob Powell on override or handles.)

    Code Snippet
    Public Class Form1

        
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            
    MyBase.OnPaint(e)
            e.Graphics.DrawRectangle(Pens.Black, 40, 40, 40, 40)
        
    End Sub

    End
    Class

    Bitmap

    Ask the Bitmap for the Graphics object:

    Code Snippet
    Public Class Form1

        
    Sub New()
            InitializeComponent()
            
    Dim bm As New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
            
    Using g As Graphics = Graphics.FromImage(bm)
                g.Clear(Color.White)
                g.DrawRectangle(Pens.Black, 30, 30, 40, 40)
            
    End Using
            Me.BackgroundImage = bm
        
    End Sub

    End
    Class



    Another way is to use the CreateGraphics method, but this does not work with the event system, and so you will find that all the drawing you do disappears when windows needs to repaint the Form or whatever. You can use this if you don't need the drawing to be persistant. Say you are animating something. I don't know if that is good practice though.

    Code Snippet
    Public Class Form1

        
    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
            
    ' Drawing is NOT PERSISTANT.
            Using g As Graphics = Me.CreateGraphics
                g.DrawRectangle(Pens.Black, 40, 40, 40, 40)
            
    End Using
        End Sub

    End
    Class

    You are doing something weird with IntPtrs to get the Graphics object. You create a new IntPtr, which is a value type and so it gets assigned a default value - IntPtr.Zero. You then create a Graphics object from this IntPtr. This is interesting as when you use IntPtr.Zero you get a Graphics object that will draw on the surface of your screen (it is like using the GetDC platform method to get the desktop DC) - it paints over all the forms and desktop stuff behind it. So, all the drawing that you do with that Graphics object will be straight onto the screen, and it will not be persistant (when windows repaints that area it will vanish).

    Above I used the Using keyword instead of Dim, Using will call Dispose on the Graphics object as execution leaves the Using...End Using code block. This frees up the memory that was used by the Graphics obejct.

    There's plenty of good drawing advice on http://www.bobpowell.net/

    Friday, March 14, 2008 6:11 PM