Also, der Graphicscode ist alles in bester Ordnung! Es ging eher um die Bedienung,
z.B. mein Component setzt die verschiedenen Events. Es hat auch selber ein Event, um
die Anwendung (Form) zu benachrichtigen, dass etwas ausgewählt wurde, weil man
nicht unbedingt immer einfach das Bild ersetzen will, und arbeitet mit beliebigen
Controls und nicht nur mit PictureBox. Und die Logik zu erkennen wenn die Maus
bewegt wird oder Zeichnen fertig ist habe ich etwas geändert. Übrigens, ich schreibe
kaum Clientsoftware, und habe selber kaum Kenntnisse von GDI(+).
Der Aufruf sieht jetzt so aus, nachem RubberBoxComponent eingefügt würde, und
Control Property gesetzt:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
PictureBox1.Image = New Bitmap("F:\Work Dev\VSNet" & _
"Dev\Test\firewks2.jpg")
End Sub
Private Sub RubberBoxComponent1_Selected(ByVal sender As Object, ByVal e As _
RubberBoxComponent.RubberBoxEventArgs) Handles _
RubberBoxComponent1.Selected
Dim newImage As Image = RubberBoxComponent1.CutSelection( _
PictureBox1.Image, PictureBox1.SizeMode = _
PictureBoxSizeMode.StretchImage)
RubberBoxComponent1.Clear()
PictureBox2.Image = newImage
End Sub
Imports System.ComponentModel
Imports System.Windows.forms
<DefaultEvent("Selected")> _
Public Class RubberBoxComponent
Inherits System.ComponentModel.Component
#Region " Component Designer generated code "
Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()
'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Component 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 Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
<Description("Notify that a box has been completely drawn"), _
EditorBrowsable(EditorBrowsableState.Always)> _
Public Event Selected(ByVal sender As Object, ByVal e As RubberBoxEventArgs)
Private Drawing As Boolean = False
Private Drawn As Boolean = False
Private Position As Point
Private Size As Size
Private OriginalPosition As Point
Private cut As Int16 = 1
Private WithEvents ctl As System.windows.forms.Control
Private x0 As Integer
Private x1 As Integer
Private y0 As Integer
Private y1 As Integer
<Browsable(True), _
Category("Mouse"), _
Description("Control in which to draw rubber box"), _
EditorBrowsable(EditorBrowsableState.Always)> _
Public Property Control() As System.Windows.Forms.Control
Get
Return ctl
End Get
Set(ByVal Value As System.Windows.Forms.Control)
ctl = Value
End Set
End Property
Private Sub Draw()
Drawing = True
ControlPaint.DrawReversibleFrame(New Rectangle(Position, Size), _
Color.White, FrameStyle.Dashed)
End Sub
Private Sub Remove()
If Drawn Or Drawing Then
ControlPaint.DrawReversibleFrame(New Rectangle(Position, Size), _
Color.White, FrameStyle.Dashed)
End If
Drawn = False
End Sub |