這是一個示例應用程序。它只是一個帶有ToolStrip的表單,還有一些按鈕以及添加到每個按鈕的圖像。每個按鈕的關鍵屬性是CheckOnClick=True
,這將保持按下按鈕。
ToolStrips沒有類似於單選按鈕的功能,所以您必須自己「取消」其他ToolStripButtons,這是我在ItemClicked
事件中處理的。
Public Class Form1
Private _ActiveImage As Image = Nothing
Private Class ImagePoint
Public Location As Point
Public Image As Image
Public Sub New(ByVal image As Image, ByVal location As Point)
Me.Image = image
Me.Location = location
End Sub
End Class
Private _Images As New List(Of ImagePoint)
Public Sub New()
InitializeComponent()
Me.DoubleBuffered = True
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
For Each imageItem As ImagePoint In _Images
e.Graphics.DrawImage(imageItem.Image, imageItem.Location)
Next
End Sub
Private Sub ToolStrip1_ItemClicked(ByVal sender As Object, ByVal e As ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
For Each toolButton As ToolStripButton In ToolStrip1.Items.OfType(Of ToolStripButton)()
If toolButton.CheckOnClick Then
If e.ClickedItem.Equals(toolButton) Then
_ActiveImage = e.ClickedItem.Image
Else
toolButton.Checked = False
End If
End If
Next
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown
If _ActiveImage IsNot Nothing AndAlso e.Button = MouseButtons.Left Then
_Images.Add(New ImagePoint(_ActiveImage, e.Location))
Me.Invalidate()
End If
End Sub
End Class
這個例子只是使用一個簡單的類來保存這些圖像被放置在什麼位置和油漆事件只是列表循環和油漆的形象。
如果在將來刪除圖像,則在繪製任何圖像之前,必須先撥打e.Graphics.Clear(Color.White)
。
Javascript!= Java – colithium 2012-02-09 20:13:51