Results 1 to 10 of 10

Thread: How to make the form follow the cursor or mouse smoothly?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    22

    How to make the form follow the cursor or mouse smoothly?

    How to make the form follow the cursor or mouse smoothly?

    any ideas.

    i have only this code.

    me.location = mouseposition

    please help me with this...thanks

  2. #2
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: How to make the form follow the cursor or mouse smoothly?

    Hrm .. is this to display some status near the cursor?

    if so you would probably be better off using GDI+ to draw a custom cursor... they can be any size you want!

    For a custom cursor:

    vb Code:
    1. Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    2.         Dim theText = e.X & " x " & e.Y
    3.         Dim textSize As Size = System.Windows.Forms.TextRenderer.MeasureText(theText, System.Drawing.SystemFonts.DefaultFont)
    4.         Dim TheWidth As Integer = 16 + textSize.Width + 1
    5.         Dim TheHeight As Integer = 16 + textSize.Height + 1
    6.         If TheWidth < Cursors.Default.Size.Width Then TheWidth = Cursors.Default.Size.Width
    7.         If TheHeight < Cursors.Default.Size.Height Then TheHeight = Cursors.Default.Size.Height
    8.  
    9.         Using DragDropBitmap As New Bitmap(TheWidth, TheHeight)
    10.             Using g As Graphics = Graphics.FromImage(DragDropBitmap)
    11.                 g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    12.                 'draw custom stuff here under cursor
    13.  
    14.                 'draw origional cursor
    15.                 Cursors.Default.Draw(g, New Rectangle(0, 0, DragDropBitmap.Width, DragDropBitmap.Height))
    16.  
    17.                 'draw custom stuff here over cursor
    18.                 g.TranslateTransform(16, 16)
    19.                 Dim rect As New Rectangle(Point.Empty, textSize)
    20.                 Using BGBrush As New SolidBrush(Color.FromKnownColor(KnownColor.Info))
    21.                     g.FillRectangle(BGBrush, rect)
    22.                 End Using
    23.                 Using BorderPen As New Pen(Color.FromKnownColor(KnownColor.InfoText))
    24.                     g.DrawRectangle(BorderPen, rect)
    25.                 End Using
    26.                 Using TextBrush As New SolidBrush(Color.FromKnownColor(KnownColor.InfoText))
    27.                     g.DrawString(theText, System.Drawing.SystemFonts.DefaultFont, TextBrush, 0, 0)
    28.                 End Using
    29.  
    30.                 'turn bitmap to cursor and set it to the cursor
    31.                 Me.Cursor = CreateCursor(DragDropBitmap, Cursors.Default.HotSpot.X, Cursors.Default.HotSpot.Y)
    32.             End Using
    33.         End Using
    34.     End Sub
    35.  
    36. #Region "Cursor Stuff"
    37.  
    38.     Private Structure IconInfo
    39.         Public fIcon As Boolean
    40.         Public xHotspot As Int32
    41.         Public yHotspot As Int32
    42.         Public hbmMask As IntPtr
    43.         Public hbmColor As IntPtr
    44.     End Structure
    45.  
    46.     <System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="CreateIconIndirect")> _
    47.     Private Shared Function CreateIconIndirect(ByVal iconInfo As IntPtr) As IntPtr
    48.     End Function
    49.  
    50.     <System.Runtime.InteropServices.DllImport("user32.dll", CharSet:=Runtime.InteropServices.CharSet.Auto)> _
    51.     Public Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
    52.     End Function
    53.  
    54.     <System.Runtime.InteropServices.DllImport("gdi32.dll")> _
    55.     Private Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
    56.     End Function
    57.  
    58.  
    59.     Public Shared Function CreateCursor(ByVal bmp As Bitmap, Optional ByVal xHotspot As Int32 = 0, Optional ByVal yHotspot As Int32 = 0) As System.Windows.Forms.Cursor
    60.         'Setup the Cursors IconInfo
    61.         Dim tmp As New IconInfo
    62.         tmp.xHotspot = xHotspot
    63.         tmp.yHotspot = yHotspot
    64.         tmp.fIcon = False
    65.         tmp.hbmMask = bmp.GetHbitmap()
    66.         tmp.hbmColor = bmp.GetHbitmap()
    67.  
    68.         'Create the Pointer for the Cursor Icon
    69.         Dim pnt As IntPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(tmp))
    70.         System.Runtime.InteropServices.Marshal.StructureToPtr(tmp, pnt, True)
    71.         Dim curPtr As IntPtr = CreateIconIndirect(pnt)
    72.  
    73.         'Clean Up
    74.         DestroyIcon(pnt)
    75.         DeleteObject(tmp.hbmMask)
    76.         DeleteObject(tmp.hbmColor)
    77.  
    78.         Return New System.Windows.Forms.Cursor(curPtr)
    79.     End Function
    80.  
    81. #End Region

    Kris

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    22

    Re: How to make the form follow the cursor or mouse smoothly?

    ...thanks for answering.

    hehe. i'm really new with this.

    I tried to put the code in my project but it doest move at all?

    What could be wrong?



    by the way im using visual studio 2010.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    22

    Thumbs up Re: How to make the form follow the cursor or mouse smoothly?

    Quote Originally Posted by i00 View Post
    Hrm .. is this to display some status near the cursor?

    if so you would probably be better off using GDI+ to draw a custom cursor... they can be any size you want!

    For a custom cursor:
    nope... i just want to make the form1 follow the cursor smoothly.

    This is what i want to do:

    when i load the form it will follow the cursor or the mouse smoothly.
    and then when i move away the cursor/mouse from the form it will just follow it

    ... is this possible? thanks

  5. #5
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: How to make the form follow the cursor or mouse smoothly?

    Look up Global mouse hooks
    like the article: http://www.codeproject.com/KB/system...yboardlib.aspx

    on the mouse move event in the hook set the forms location to that point + an offset

    Kris

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    22

    Re: How to make the form follow the cursor or mouse smoothly?

    Can you give me an example on how to use the code on the link to make what i want to do?

    thanks...
    Last edited by mychael14; Oct 8th, 2011 at 08:48 PM.

  7. #7
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: How to make the form follow the cursor or mouse smoothly?

    Global mouse hooks are hardly necessary for this trivial task. There are ways to do this with WndProc,SendMessage and timers. Look in the code bank for moving border less forms.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  8. #8
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: How to make the form follow the cursor or mouse smoothly?

    Quote Originally Posted by BlindSniper View Post
    Global mouse hooks are hardly necessary for this trivial task. There are ways to do this with WndProc,SendMessage and timers. Look in the code bank for moving border less forms.
    You can get a WndProc to capture when the mouse moves globally ?... i dont think so... but i could be wrong .. .you then go onto say timers... isn't that a bit of a contradiction? - i mean if you have a timer it will not be smooth as you would only be updating the forms position every x ticks rather than when the mouse actually moves?

    Kris

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    22

    Re: How to make the form follow the cursor or mouse smoothly?

    ...hey guys can you give me an example for this?

  10. #10
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: How to make the form follow the cursor or mouse smoothly?

    Quote Originally Posted by i00 View Post
    You can get a WndProc to capture when the mouse moves globally ?... i dont think so... but i could be wrong .. .you then go onto say timers... isn't that a bit of a contradiction? - i mean if you have a timer it will not be smooth as you would only be updating the forms position every x ticks rather than when the mouse actually moves?

    Kris
    No,no
    It IS possible with wndProc and I said you can use timers, although they are hardly ideal.

    a C# one http://www.vbforums.com/showthread.p...orderless+form
    vb.net
    http://www.vbforums.com/showthread.php?p=3509289
    http://www.vbforums.com/showthread.p...orderless+form
    http://www.vbforums.com/showthread.p...orderless+form
    http://www.vbforums.com/showthread.p...orderless+form
    Personally I like NickThissen's code bank entry the best.
    and about 300 more search results I refused to go through and note how all of them do not use global mouse hooks.
    Last edited by BlindSniper; Oct 10th, 2011 at 06:07 AM.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

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