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

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




Image Following mouse

 
Reply to this topicStart new topic

Image Following mouse

s_jsstevens
11 Dec, 2007 - 11:13 AM
Post #1

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 10


My Contributions
I have a short little program that has an image follow my mouse cursor. But the problem is that the image keeps drawing itself over and over. I only want 1 image following my mouse.

CODE
Public Class Form1

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Dim myBitmap As System.Drawing.Bitmap
        Dim myGraphics As Graphics

        mybitmap = New System.Drawing.Bitmap("f:\Dads Work\Project Pictures\turn2.bmp")
        myGraphics = Graphics.FromHwnd(ActiveForm().Handle)
        myGraphics.DrawImage(myBitmap, e.X, e.Y, 25, 25)
    End Sub
End Class


This post has been edited by s_jsstevens: 11 Dec, 2007 - 11:14 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Image Following Mouse
11 Dec, 2007 - 12:29 PM
Post #2

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
I would go about this in a slightly different way. First of all, I would setup the bitmap variable as a private form level variable and in the forms onload event, load the graphic into it. I would then throw on a panel onto the form and set it to invisible initially. In the mousemove event I would set the left and top properties of the panel to the mouse coordinates and lastly I would setup an Paint method for the panel which will draw the picture onto the panel.

The code would look something like this...

CODE

Public Class Form1
    ' Private member variable to hold our graphic (instead of reloading it each time, more efficient)
    Private myBitmap As System.Drawing.Bitmap

    ' Upon loading of the form, load up our bitmap
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myBitmap = New System.Drawing.Bitmap("C:\mypic.jpg")
    End Sub


    ' When we move across the form, set our invisible panel to visible and move the panel using its left
    ' and top properties, but about 3 pixels off so that it won't fail to move when we attempt to move the mouse
    ' backwards onto the panel.
    Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        mypanel.Visible = True
        mypanel.Left = e.X + 3
        mypanel.Top = e.Y + 3
    End Sub

    ' Using our graphics object of the panel, we paint on the picture into the top left corner and size it accordingly
    Private Sub mypanel_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles mypanel.Paint
        e.Graphics.DrawImage(myBitmap, 1, 1, 25, 25)
    End Sub
End Class


As you can see it is not too hard and can easily be extended in the future for changing the picture on the fly and even during movement of the mouse. You could have a rainbow of pictures if you like. My panel is named "mypanel" and as you can see we move the panel with the picture on it around.

Hope this works for you and gets what you were want to go.

Enjoy!

"At DIC we be picture panel moving code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

s_jsstevens
RE: Image Following Mouse
11 Dec, 2007 - 02:30 PM
Post #3

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 10


My Contributions
Thank you very much It works beautifully. Another question if i may. How would i for example:
If I were to click a button that the image would dissapear and no longer follow my mouse.

Thanks again
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Image Following Mouse
11 Dec, 2007 - 02:40 PM
Post #4

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
In your mousemove event, you are going to need to build in a boolean flag that can be set to true and false. The button will then set it to true and the mousemove will make the panel follow or false and the mousemove will not execute the code. For example...

CODE

Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        mypanel.Visible = True

        if followmouse then
                mypanel.Left = e.X + 3
                mypanel.Top = e.Y + 3
        end if
End Sub


You could define followmouse as a private form level variable. Your button will then toggle the value between true and false. smile.gif


User is offlineProfile CardPM
+Quote Post

s_jsstevens
RE: Image Following Mouse
11 Dec, 2007 - 03:01 PM
Post #5

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 10


My Contributions
Thank you very much again biggrin.gif That is very simple. You rock biggrin.gif
User is offlineProfile CardPM
+Quote Post

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

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