Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,902 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,208 people online right now. Registration is fast and FREE... Join Now!




Dont Redraw OnPaint

 
Reply to this topicStart new topic

Dont Redraw OnPaint

s_jsstevens
11 Jan, 2008 - 03:45 PM
Post #1

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 10


My Contributions
found this little snippet of code here, and I have been trying to tweak it a bit. First off, i will tell you this code draws an image wherever I click. But the problem for me is, when I click on different location it removes the previous image.

AKA: If I click 4 different places on the form, I want 4 images on the form.

Thank you in advance for your time


CODE


Partial Public Class MainForm

    Private drawimage As Boolean
    Dim x As Integer = 0
    Dim y As Integer = 0


    Public Sub New()

        Me.InitializeComponent()

    End Sub

    Private Sub MainForm_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        If ((e.X > 100) And (e.Y > 100)) Then
            drawimage = True
        Else
            drawimage = False
        End If
        Me.Invalidate()
        x = e.X
        y = e.Y
    End Sub

    Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs)

        MyBase.OnPaint(e)

        If drawimage Then
            e.Graphics.DrawImage(System.Drawing.Image.FromFile("f:\Dads Work\Project Pictures\cross.bmp"), x, y)
        End If

    End Sub

End Class

User is offlineProfile CardPM
+Quote Post

RodgerB
RE: Dont Redraw OnPaint
11 Jan, 2008 - 09:35 PM
Post #2

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
You could use an array when storing your variables, so the variable isn't overwritten when placing your images. I recommend using a Point array as it is suitable for the way you are using your methods.

When you need to draw, just loop through all the elements and draw the image in that location. Hope that helps. smile.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Dont Redraw OnPaint
11 Jan, 2008 - 09:42 PM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well if you want to draw the image where you click the mouse and not erase the others, don't worry about using the onpaint method. You can draw the image on the form using just the mousedown event. Here is an example...

CODE

Private Sub MainForm_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        Dim formGraphics As Graphics

        ' Get the graphics context for the form
        formGraphics = Me.CreateGraphics()

        ' Draw the image where the mouse is
        formGraphics.DrawImage(System.Drawing.Image.FromFile("f:\Dads Work\Project Pictures\cross.bmp"), e.X, e.Y)

End Sub


We get the drawing graphics object of the form, then use this to draw the image right on the form at the mouse coordinates.

Enjoy!

"At DIC we be form context drawing code ninjas!" decap.gif

This post has been edited by Martyr2: 11 Jan, 2008 - 09:43 PM
User is offlineProfile CardPM
+Quote Post

RodgerB
RE: Dont Redraw OnPaint
11 Jan, 2008 - 10:30 PM
Post #4

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
That would work initially Martyr2, but it would need to be put in the OnPaint event if the form were to be minimized or resized, otherwise the image would disappear, hence why its usually a programmers choice thing when drawing graphics to put it in the OnPaint event because its called by the OS. smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 01:15PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month