分享

Visual 2008 下VB9.0在窗体上绘线条

 冰山上的薰衣草 2010-10-03
'根据相关代码修改而来。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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多