none
画出鼠标轨迹的问题 RRS feed

  • 问题

  • Public Class Form1
       
        Private Structure POINTAPI
            Public x As Single
            Public y As Single
        End Structure
        Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Long
        Dim p As POINTAPI
        
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            GetCursorPos(p)
        End Sub
    
        Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
    
        End Sub
    
        Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            Dim g As Graphics = PictureBox1.CreateGraphics
            g.DrawEllipse(Pens.Red, p.x, p.y, 1, 1)
            g.FillEllipse(Brushes.Black, p.x, p.y, 1, 1)
            g.Dispose()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Enabled = True
            Timer1.Interval = 1
        End Sub
    End Class
    
    哪里有问题呢  说死PictureBox都是一片空白啊。。。

    Visual Basic 初学者 望关照!

    2012年7月19日 16:29

答案

  • 你好,

    请参考一下这个例子http://www.codeproject.com/Articles/4634/A-Simple-VB-NET-Freehand-Drawing-method ,将form1.designer.vb中的重复代码注释掉就可以测试了:

    'I got frustrated trying to find a Simple VB.NET freehand drawing code
    'So I made my own :)
    
    '*** Acknowlegements  ***
    'Ideas for this code came from the MicroSoft "Scribble" sample code, 
    'Christian Graus's excellent arcticle on a C++ code called "Doodle" 
    'and the MicroSoft website.
    
    'Note that this is a VERY pedantic freehand drawing code, so be kind in you comments. :)
    'It uses a graphics path to follow the users mouse movements
    'The path is then painted in the window.
    
    'By John Buettner
    '26 July 2003
    '******************************************************
    'This code is for informational purposes
    'It is property of the code writer, but may be modified and
    'used for any purpose private or commercial, However,
    'Users of this code must agree not to copyright or infringe
    'upon the original coders rights to this code.
    '******************************************************
    
    Imports System
    Imports System.Drawing
    Imports System.Windows.Forms
    
    
    Public Class Form1
        'Namespace myPaint
    
        Inherits System.Windows.Forms.Form  ' Of course ;)
    
        Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath() 'declare a new Graphic path to follow the mouse movement
    
        '*** below I declare some values for an Alpha  and other user selected variables
        'these will be used as I expand this program for a higher level use.
    
        Dim myAlpha As Integer = 100 ' declare a Alpha variable
        Dim myUserColor As New Color() 'this is a color the user selects
        Dim myPenWidth As Single = 5 'set pen width variable
    
        '**************************************************************
    
    
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.Label1 = New System.Windows.Forms.Label()
            Me.PictureBox1 = New System.Windows.Forms.PictureBox()
            Me.SuspendLayout()
            '
            'Label1
            '
            Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            Me.Label1.Location = New System.Drawing.Point(104, 40)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(472, 32)
            Me.Label1.TabIndex = 1
            Me.Label1.Text = "Below is a Graphics path freehand drawing space"
            '
            'PictureBox1
            '
            Me.PictureBox1.BackColor = System.Drawing.Color.Ivory
            Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
            Me.PictureBox1.Location = New System.Drawing.Point(112, 96)
            Me.PictureBox1.Name = "PictureBox1"
            Me.PictureBox1.Size = New System.Drawing.Size(448, 272)
            Me.PictureBox1.TabIndex = 2
            Me.PictureBox1.TabStop = False
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
            Me.BackColor = System.Drawing.Color.BlanchedAlmond
            Me.ClientSize = New System.Drawing.Size(696, 456)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.PictureBox1, Me.Label1})
            Me.Name = "Form1"
            Me.Text = "Johns' Free Hand Doodle"
            Me.ResumeLayout(False)
    
        End Sub
    
    #End Region
    
    
    
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
    
            If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down  
    
                mousePath.StartFigure()    ' The L mouse is down so we need to start a new line in mousePath
    
            End If
    
        End Sub
    
    
        Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    
            If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down  
    
                Try
                    mousePath.AddLine(e.X, e.Y, e.X, e.Y)    'Add mouse coordiantes to mousePath
    
                Catch
                    MsgBox("No way, Hose!")
                End Try
    
            End If
    
            PictureBox1.Invalidate() 'Repaint the PictureBox using the PictureBox1 Paint event
    
        End Sub
    
    
    
        Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
            ' Here is where we do the actual paintings
    
            Try ' error trapping
    
                myUserColor = (System.Drawing.Color.Black) 'You can remove this line and add a user selected color to
                'change the value of myUserColor
    
                myAlpha = 100  ' This will give the color a Alpha effect, you can set this to 255 if you want a full color
    
                '*********************** NOTE  ***********************************************
                'The line below set the pen up with the ability to add user selected Alpha, Color and Penwidth
                ' A simpler, but less flexible solution would be to replace the line with the following code:
                'Dim CurrentPen = New Pen(System.Drawing.Color.Black, myPenWidth)
                '************  End Note  ***************************
    
                Dim CurrentPen = New Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 'Set up the pen
    
                e.Graphics.DrawPath(CurrentPen, mousePath)  'draw the path! :)
    
            Catch
                ' MsgBox("Not happening!")
            End Try
    
        End Sub
    
        'End Namespace
    
    End Class 'Form1
    
    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us

    2012年7月20日 8:32
    版主
  • 另外我又找到一个不错的例子,虽然不是在picturebox里面画图,但是可以参考下:http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/9a878da8-2bae-4290-a37c-788f14301f3e 

    Shanks Zen
    MSDN Community Support | Feedback to us

    2012年8月1日 9:39
    版主

全部回复

  • 你好,

    请参考一下这个例子http://www.codeproject.com/Articles/4634/A-Simple-VB-NET-Freehand-Drawing-method ,将form1.designer.vb中的重复代码注释掉就可以测试了:

    'I got frustrated trying to find a Simple VB.NET freehand drawing code
    'So I made my own :)
    
    '*** Acknowlegements  ***
    'Ideas for this code came from the MicroSoft "Scribble" sample code, 
    'Christian Graus's excellent arcticle on a C++ code called "Doodle" 
    'and the MicroSoft website.
    
    'Note that this is a VERY pedantic freehand drawing code, so be kind in you comments. :)
    'It uses a graphics path to follow the users mouse movements
    'The path is then painted in the window.
    
    'By John Buettner
    '26 July 2003
    '******************************************************
    'This code is for informational purposes
    'It is property of the code writer, but may be modified and
    'used for any purpose private or commercial, However,
    'Users of this code must agree not to copyright or infringe
    'upon the original coders rights to this code.
    '******************************************************
    
    Imports System
    Imports System.Drawing
    Imports System.Windows.Forms
    
    
    Public Class Form1
        'Namespace myPaint
    
        Inherits System.Windows.Forms.Form  ' Of course ;)
    
        Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath() 'declare a new Graphic path to follow the mouse movement
    
        '*** below I declare some values for an Alpha  and other user selected variables
        'these will be used as I expand this program for a higher level use.
    
        Dim myAlpha As Integer = 100 ' declare a Alpha variable
        Dim myUserColor As New Color() 'this is a color the user selects
        Dim myPenWidth As Single = 5 'set pen width variable
    
        '**************************************************************
    
    
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.Label1 = New System.Windows.Forms.Label()
            Me.PictureBox1 = New System.Windows.Forms.PictureBox()
            Me.SuspendLayout()
            '
            'Label1
            '
            Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            Me.Label1.Location = New System.Drawing.Point(104, 40)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(472, 32)
            Me.Label1.TabIndex = 1
            Me.Label1.Text = "Below is a Graphics path freehand drawing space"
            '
            'PictureBox1
            '
            Me.PictureBox1.BackColor = System.Drawing.Color.Ivory
            Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
            Me.PictureBox1.Location = New System.Drawing.Point(112, 96)
            Me.PictureBox1.Name = "PictureBox1"
            Me.PictureBox1.Size = New System.Drawing.Size(448, 272)
            Me.PictureBox1.TabIndex = 2
            Me.PictureBox1.TabStop = False
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
            Me.BackColor = System.Drawing.Color.BlanchedAlmond
            Me.ClientSize = New System.Drawing.Size(696, 456)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.PictureBox1, Me.Label1})
            Me.Name = "Form1"
            Me.Text = "Johns' Free Hand Doodle"
            Me.ResumeLayout(False)
    
        End Sub
    
    #End Region
    
    
    
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
    
            If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down  
    
                mousePath.StartFigure()    ' The L mouse is down so we need to start a new line in mousePath
    
            End If
    
        End Sub
    
    
        Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    
            If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down  
    
                Try
                    mousePath.AddLine(e.X, e.Y, e.X, e.Y)    'Add mouse coordiantes to mousePath
    
                Catch
                    MsgBox("No way, Hose!")
                End Try
    
            End If
    
            PictureBox1.Invalidate() 'Repaint the PictureBox using the PictureBox1 Paint event
    
        End Sub
    
    
    
        Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
            ' Here is where we do the actual paintings
    
            Try ' error trapping
    
                myUserColor = (System.Drawing.Color.Black) 'You can remove this line and add a user selected color to
                'change the value of myUserColor
    
                myAlpha = 100  ' This will give the color a Alpha effect, you can set this to 255 if you want a full color
    
                '*********************** NOTE  ***********************************************
                'The line below set the pen up with the ability to add user selected Alpha, Color and Penwidth
                ' A simpler, but less flexible solution would be to replace the line with the following code:
                'Dim CurrentPen = New Pen(System.Drawing.Color.Black, myPenWidth)
                '************  End Note  ***************************
    
                Dim CurrentPen = New Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 'Set up the pen
    
                e.Graphics.DrawPath(CurrentPen, mousePath)  'draw the path! :)
    
            Catch
                ' MsgBox("Not happening!")
            End Try
    
        End Sub
    
        'End Namespace
    
    End Class 'Form1
    
    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us

    2012年7月20日 8:32
    版主
  • 您这个代码如果改变

    myPenWidth这个量那么原来画的线也会变化 有没有神马比链表高效率的方法让原来线条不变后画的线条

    改变粗细呢?


    Visual Basic 初学者 望关照!

    2012年7月22日 13:08
  • 你可以在改变图形线条的时候将原来的轨迹保存起来,然后显示在picturebox里面,这样的话原来的轨迹就不会改变了。

    Shanks Zen
    MSDN Community Support | Feedback to us

    2012年7月23日 5:06
    版主
  • 怎么弄啊?求指教。。。

    Visual Basic 初学者 望关照!

    2012年7月24日 11:46
  • 你好,

    在这个例子的引用链接下面的讨论中,有这样一段代码,你可以试一下:

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
                Dim Layer1 As New Bitmap (Your Picturebox size) 'Change to your PictureBox Size
                PictureBox1.DrawToBitmap(Layer1, New Rectangle(0, 0, Layer1.Width, Layer1.Height))
                PictureBox1.Image = Layer1
                mousePath.Reset()
          End Sub
     
    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us

    2012年7月26日 8:12
    版主
  • 另外我又找到一个不错的例子,虽然不是在picturebox里面画图,但是可以参考下:http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/9a878da8-2bae-4290-a37c-788f14301f3e 

    Shanks Zen
    MSDN Community Support | Feedback to us

    2012年8月1日 9:39
    版主
  • 这个太难了 不过好像容易实现填充、、、、

    Visual Basic 初学者 望关照!

    2012年8月1日 11:58