大家好,本人第一次發表文章(激動中),看了開發高手連續幾篇談到PhotoShop中流動選取框的文章,其實實現并不難,在這里我就用VB.NET實現,在.NET中提供了功能十分強大的GDI+,前篇C#用的也是GDI+,我這里也用上!其實沒有什么區別!希望對學VB.NET的人有幫助,下面是源碼:
創建一個新的VB應用程序,一個窗口中添加一個時間(Timer)組件,Interval設置為50微妙,
Imports System.Drawing.Drawing2D Imports System.Drawing.Graphics Public Class Form1 Inherits System.Windows.Forms.Form Private pen As pen '創建一個畫筆對象 Private GPath As New GraphicsPath '實例化路徑對象 Private Dpattern() As Single = {5.0, 7.0} '實線的長度和虛線長度 Private offset As Single = 0.0 '偏移值
#Region " Windows 窗體設計器生成的代碼 "
Public Sub New() MyBase.New()
'該調用是 Windows 窗體設計器所必需的。 InitializeComponent()
'在 InitializeComponent() 調用之后添加任何初始化
End Sub
'窗體重寫 dispose 以清理組件列表。 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If GPath.Dispose() pen.Dispose() End If MyBase.Dispose(disposing) End Sub
'Windows 窗體設計器所必需的 Private components As System.ComponentModel.IContainer
'注意: 以下過程是 Windows 窗體設計器所必需的 '可以使用 Windows 窗體設計器修改此過程。 '不要使用代碼編輯器修改它。 Friend WithEvents Timer1 As System.Windows.Forms.Timer <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.components = New System.ComponentModel.Container Me.Timer1 = New System.Windows.Forms.Timer(Me.components) ' 'Timer1 ' Me.Timer1.Enabled = True Me.Timer1.Interval = 50 ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14) Me.ClientSize = New System.Drawing.Size(416, 166) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.MaximizeBox = False Me.MinimizeBox = False Me.Name = "Form1" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Photo For VB.NET"
End Sub
#End Region
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Me.Refresh() '刷新窗口 End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint pen.DashOffset = offset '設置偏移值 e.Graphics.DrawPath(pen, GPath) '畫路徑
'改變偏移值的量 offset += 1.0 If offset / 100 = 1 Then offset = 0.0 End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load GPath.AddString("電腦", _ New FontFamily("幼圓"), _ FontStyle.Bold + FontStyle.Italic, _ 120.0F, _ New PointF(30.0F, 20.0F), _ New StringFormat) '添加一個字符路徑 pen = New Pen(Color.Black) '構造畫筆 pen.DashPattern = Dpattern '自定義的短劃線和空白區域 pen.DashStyle = DashStyle.Custom '此屬性的 DashStyle.Custom 值指定:由 DashPattern 屬性定義的短劃線和空白區域的自定義圖案 End Sub End Class 在WINXP+SP1+VS.NET2003編譯通過
有不對的
|