Click here to Skip to main content
15,884,605 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm getting unexpected behavior when I attempt to draw a line from a point to the current mouse position. The line only draws when I move the mouse within 100 pixels of the top of the form and/or 100 pixels within the left side of the form. If I move the mouse out of the form to the top or the right the line will draw to the edge of the form.

I've left out refreshing/invalidating the form and clearing the existing lines to get at the root of the problem. Right now the code is supposed to keep drawing lines as the mouse moves, but it's forming a top-left border and only drawing when my mouse enters this border from below or from the right.

What I have tried:

Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
        MousePosX = e.X
        MousePosY = e.Y
    End Sub

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint

            Dim canvas As Graphics = me.CreateGraphics()
            Dim blackpen1 As Pen = New Pen(Color.Black, 2)
            Dim point1 As Point = New Point(210,210)
            Dim point2 As Point = New Point(MousePosX, MousePosY)

            canvas.DrawLine(blackpen1, point1, point2)

    End Sub


Image of the result:

example — ImgBB[^]
Posted
Updated 1-Dec-22 8:58am
v2
Comments
Richard MacCutchan 1-Dec-22 12:25pm    
I am not sure I understand what you are saying, but you can only draw within the limits of the form.
kamikazehighland 1-Dec-22 12:25pm    
There's also a second border at 10 pixels from the top and the left that triggers lines to be drawn.
kamikazehighland 1-Dec-22 12:28pm    
Let's say a line is supposed to be drawn from a given point on the form to the current mouse position. If I move the mouse to the top 100 pixels or the left 100 pixels a line gets drawn. No lines are being drawn to the right or below the given point. The drawn lines are cut off at 100 pixels from the top/left and are not drawn within that border. Edit - unless I move the mouse out of the form. I've added an example image above.

1 solution

I also don't understand completely what problem you have ... but perhaps you try the following :
VB
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint

            Dim canvas As Graphics = e.Graphics  ' <---- Changes here !
            Dim blackpen1 As Pen = New Pen(Color.Black, 2)
            Dim point1 As Point = New Point(210,210)
            Dim point2 As Point = New Point(MousePosX, MousePosY)

            canvas.DrawLine(blackpen1, point1, point2)

    End Sub


If you use the Graphics-Object out of the Eventargs of the Method you have complete access to the area from the Form.
Perhaps that helps ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900