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 = Windows.Forms.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 = Windows.Forms.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 painting 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 As Pen = 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 NamespaceEnd Class 'Form1这是VB代码,去这个网站找Freehand Drawing,没有C#源代码,原理一样
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 = Windows.Forms.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 = Windows.Forms.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 painting 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 As Pen = 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 NamespaceEnd Class 'Form1这是VB代码,去这个网站找Freehand Drawing,没有C#源代码,原理一样