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:confused:
Printable View
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:confused:
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:
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove Dim theText = e.X & " x " & e.Y Dim textSize As Size = System.Windows.Forms.TextRenderer.MeasureText(theText, System.Drawing.SystemFonts.DefaultFont) Dim TheWidth As Integer = 16 + textSize.Width + 1 Dim TheHeight As Integer = 16 + textSize.Height + 1 If TheWidth < Cursors.Default.Size.Width Then TheWidth = Cursors.Default.Size.Width If TheHeight < Cursors.Default.Size.Height Then TheHeight = Cursors.Default.Size.Height Using DragDropBitmap As New Bitmap(TheWidth, TheHeight) Using g As Graphics = Graphics.FromImage(DragDropBitmap) g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality 'draw custom stuff here under cursor 'draw origional cursor Cursors.Default.Draw(g, New Rectangle(0, 0, DragDropBitmap.Width, DragDropBitmap.Height)) 'draw custom stuff here over cursor g.TranslateTransform(16, 16) Dim rect As New Rectangle(Point.Empty, textSize) Using BGBrush As New SolidBrush(Color.FromKnownColor(KnownColor.Info)) g.FillRectangle(BGBrush, rect) End Using Using BorderPen As New Pen(Color.FromKnownColor(KnownColor.InfoText)) g.DrawRectangle(BorderPen, rect) End Using Using TextBrush As New SolidBrush(Color.FromKnownColor(KnownColor.InfoText)) g.DrawString(theText, System.Drawing.SystemFonts.DefaultFont, TextBrush, 0, 0) End Using 'turn bitmap to cursor and set it to the cursor Me.Cursor = CreateCursor(DragDropBitmap, Cursors.Default.HotSpot.X, Cursors.Default.HotSpot.Y) End Using End Using End Sub #Region "Cursor Stuff" Private Structure IconInfo Public fIcon As Boolean Public xHotspot As Int32 Public yHotspot As Int32 Public hbmMask As IntPtr Public hbmColor As IntPtr End Structure <System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="CreateIconIndirect")> _ Private Shared Function CreateIconIndirect(ByVal iconInfo As IntPtr) As IntPtr End Function <System.Runtime.InteropServices.DllImport("user32.dll", CharSet:=Runtime.InteropServices.CharSet.Auto)> _ Public Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean End Function <System.Runtime.InteropServices.DllImport("gdi32.dll")> _ Private Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean End Function 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 'Setup the Cursors IconInfo Dim tmp As New IconInfo tmp.xHotspot = xHotspot tmp.yHotspot = yHotspot tmp.fIcon = False tmp.hbmMask = bmp.GetHbitmap() tmp.hbmColor = bmp.GetHbitmap() 'Create the Pointer for the Cursor Icon Dim pnt As IntPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(tmp)) System.Runtime.InteropServices.Marshal.StructureToPtr(tmp, pnt, True) Dim curPtr As IntPtr = CreateIconIndirect(pnt) 'Clean Up DestroyIcon(pnt) DeleteObject(tmp.hbmMask) DeleteObject(tmp.hbmColor) Return New System.Windows.Forms.Cursor(curPtr) End Function #End Region
Kris
...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.
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 :wave:
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
Can you give me an example on how to use the code on the link to make what i want to do?
thanks... :bigyello:
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
...hey guys can you give me an example for this?
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.