Mouse Cursor not showing up over Windows Form

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Can anyone with more experience with User Controls see what Ive done wrong here? The mouse cursor disappears as soon as the user enters the area of the Form although MouseMove, MouseEnter and MouseLeave event handlers are firing correctly. I
want an overridden custom cursor to appear on the form with a reticle http://r4pymw.bay.livefilestore.com/y1pW9eEs1uRHBz-1xr3WKRJtxsnVdGp7lLGz8EBMcbUHhNsC_7i8-CWos4-2_mD-_mzBaC3Uv74yLtII4JNDXWC_KBlMW9FxQsK/reticle%20but%20it%20doesnt%20move%20and%20the%20mouse%20cursor%20does%20not%20show%20up%20when%20entering%20the%20form%20boundaries.jpg?psid=1
such as occurs here in this image as per the code listed below and in a http://cid-1bffe163b94b63d6.office.live.com/self.aspx/.Public/GraphEditorOnMouseMoveTest.zip
VS2010 solution on SkyDrive . Thank you for any help.
<pre lang="x-vbnet Option Explicit On
Option Strict On

Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Resources
Imports System.Globalization

Public Class GraphEditor

Dim Panel_Main As New PictureBox
Dim LeftMargin, TopMargin, GraphWidth, GraphHeight As Integer
Dim _ReticlePath As System.Drawing.Drawing2D.GraphicsPath
Dim _ReticlePen As Pen
Dim _ReticleOrigin As PointF
Dim TranslatedReticleDisplayXY As String
Dim mTextCursorLocation As String = "<cells X dB Y>"
Dim mYMax As Integer = 256
Dim mYMin As Integer = -256


Public Sub New()
InitializeComponent()
_ReticlePath = New System.Drawing.Drawing2D.GraphicsPath
_ReticlePen = New Pen(Color.Purple)
ReticleSize = New Size(48, 48)
_ReticleThickness = 10
_HideCursor = True
DoubleBuffered = True
End Sub

Private Sub GraphEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MinimumSize = New Size(400, 250)
Me.Controls.Add(Panel_Main)
AddHandler Panel_Main.Paint, AddressOf Me.Panel_Main_paint
AddHandler Panel_Main.MouseMove, AddressOf Me.GraphEditor_MouseMove
AddHandler Panel_Main.MouseEnter, AddressOf Me.GraphEditor_MouseEnter
AddHandler Panel_Main.MouseLeave, AddressOf Me.GraphEditor_MouseLeave
Panel_Main.Refresh()
End Sub


Private Sub GraphEditor_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
_ReticlePosition = e.Location
Dim mX As Double = e.X
Dim mY As Double = e.Y
Save mouse position
mX -= LeftMargin
mY -= TopMargin
Get scaling factor
Dim ScaleX As Double = 1
Dim ScaleY As Double = 1
Adjust by scale
mX /= ScaleX
mY /= ScaleY
Invert y axis
mY = mYMax - mY + mYMin
If mX > 0 And mX < 215 And mY > 0 And mY < 100 Then
TranslatedReticleDisplayXY = Math.Round(mX) & " " & Math.Round(mY)
mTextCursorLocation = Math.Round(mX) & " " & Math.Round(mY)
Else
TranslatedReticleDisplayXY = " " & " " & " "
mTextCursorLocation = " " & " " & " "
End If
MyBase.OnMouseMove(e)
Invalidate()
End Sub

Private Sub GraphEditor_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)
MyBase.OnMouseEnter(e)
If _HideCursor Then
Cursor.Hide()
End If
End Sub

Private Sub GraphEditor_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs)
MyBase.OnMouseLeave(e)
Cursor.Show()
End Sub

Private Sub Panel_Main_paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

Dim GraphAreaBrush_color As New Color
Dim Datapen_color As New Color

GraphAreaBrush_color = Color.White
Datapen_color = Color.Blue

Dim GraphAreaBrush As New SolidBrush(GraphAreaBrush_color)
Dim Datapen As New Pen(Datapen_color, 2)

Dim Gridbrush As New SolidBrush(Color.Black)
Dim TextBrush As New SolidBrush(Color.Black)

Set the size of the panel container
Panel_Main.Width = Me.Width
Panel_Main.Height = Me.Height

LeftMargin = 10
GraphWidth = 400
TopMargin = 10
GraphHeight = 400

Fill the Graph Paper
e.Graphics.FillRectangle(GraphAreaBrush, LeftMargin, TopMargin, GraphWidth, GraphHeight)
Dim LocationCoords As String = (_ReticlePosition.X - _ReticleOrigin.X).ToString() + " " + (ReticlePosition.Y - _ReticleOrigin.Y).ToString()
e.Graphics.DrawString(TranslatedReticleDisplayXY, Me.Font, TextBrush, _ReticlePosition.X, _ReticlePosition.Y)
e.Graphics.TranslateTransform(_ReticlePosition.X - _ReticleOrigin.X, _ReticlePosition.Y - _ReticleOrigin.Y)
e.Graphics.DrawPath(_ReticlePen, _ReticlePath)
e.Graphics.ResetTransform()

End Sub


Private Sub UserControl_ClientSizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ClientSizeChanged
Me.Refresh()
End Sub

Private _HideCursor As Boolean
Public Property HideCursor() As Boolean
Get
Return _HideCursor
End Get
Set(ByVal value As Boolean)
_HideCursor = value
End Set
End Property

Private _ReticlePosition As PointF
Public ReadOnly Property ReticlePosition As PointF
Get
Return _ReticlePosition
End Get
End Property

Private _ReticleSize As SizeF
Public Property ReticleSize() As SizeF
Get
Return _ReticleSize
End Get
Set(ByVal value As SizeF)
If Not _ReticleSize = value Then
_ReticleSize = value
BuildReticlePath()
Invalidate()
End If
End Set
End Property

Private _ReticleThickness As Integer
Public Property ReticleThickness() As Integer
Get
Return _ReticleThickness
End Get
Set(ByVal value As Integer)
_ReticleThickness = value
BuildReticlePen()
End Set
End Property

Private Sub BuildReticlePath()

If _ReticlePath IsNot Nothing Then
_ReticlePath.Dispose()
End If
_ReticlePath = New System.Drawing.Drawing2D.GraphicsPath
Dim halfWidth As Single = _ReticleSize.Width / 2.0!
Dim halfHeight As Single = _ReticleSize.Height / 2.0!
_ReticleOrigin = New PointF(halfWidth, halfHeight)
_ReticlePath.AddLine(halfWidth, 0, halfWidth, _ReticleSize.Height)
_ReticlePath.CloseFigure()
_ReticlePath.AddLine(0, halfHeight, _ReticleSize.Width, halfHeight)
Refresh()
End Sub

Private Sub BuildReticlePen()
If _ReticlePen IsNot Nothing Then
_ReticlePen.Dispose()
End If
_ReticlePen = New Pen(Color.Purple, _ReticleThickness)
End Sub


End Class
[/code]

View the full article
 
Back
Top