'根据相关代码修改而来。WinDowsXp +Spack3.0 +VS2008下调试通过
Imports System
Imports System.Drawing Imports System.Windows.Forms Imports System.Collections Public Class Form1
Private Enum Tool Line = 1 Rectangle End Enum Private myTool As Tool
Private ptDown As Point Private ptUp As Point Public ptStart As New ArrayList()
Public ptEnd As New ArrayList() Private bDrawing As Boolean
Private Sub DrawRectangleleOnClick(ByVal sender As Object, ByVal e As EventArgs)
myTool = Tool.Rectangle End Sub Private Sub DrawLineOnClik(ByVal sender As Object, ByVal e As EventArgs) myTool = Tool.Line End Sub Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
If e.Button = MouseButtons.Left AndAlso _
myTool = Tool.Line Then ptDown = New Point(e.X, e.Y)
ptStart.Add(ptDown) bDrawing = True End If
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs) Select Case myTool
Case Tool.Line Cursor.Current = Cursors.Cross Case Tool.Rectangle Cursor.Current = Cursors.VSplit End Select
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs) If Not bDrawing Then Return Select Case myTool
Case Tool.Line Dim g As Graphics = CreateGraphics()
ptUp = New Point(e.X, e.Y) g.DrawLine(New Pen(ForeColor), ptDown, ptUp) g.Dispose() ptEnd.Add(ptUp) bDrawing = False Case Tool.Rectangle
' '畫長方形自己加 ' End Select
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics Dim i As Int32
Dim myptStart() As Point = DirectCast(ptStart.ToArray(GetType(Point)), Point()) Dim myptEnd() As Point = DirectCast(ptEnd.ToArray(GetType(Point)), Point()) For i = 0 To ptEnd.Count - 1
If myptStart.Length >= 0 Then
g.DrawLine(New Pen(ForeColor), myptStart(i), myptEnd(i)) End If Next i End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
With Me
.Text = "My Drawing" .ForeColor = SystemColors.WindowText .BackColor = SystemColors.Window .Menu = New MainMenu() .ResizeRedraw = True End With Menu.MenuItems.Add("&Tool")
Menu.MenuItems(0).MenuItems.Add("&Line", AddressOf DrawLineOnClik) Menu.MenuItems(0).MenuItems.Add("&Rectangle", AddressOf DrawRectangleleOnClick) ' '可能要畫其它圖形的按鈕自己加 ' End Sub
End Class |
|