dotnet-snippets.de
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1314 | Anzahl registrierter User: 1266 | Besucher online: 326
Hauptmenü
Home
Snippet Wettbewerb
Top Ten
Zufälliger Snippet
Vista Gadget
T-Shirts für Geeks
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social
Facebook
Twitter
RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss Delphi.NET
Rss ASP.NET
Werbung
Alice DSL Flatrate

Partner
Partner von Codezone.de


Member of Microsoft Community Leader/Insider Program (CLIP)

ControlResize wie in der IDE auch zur Laufzeit


Autor: Tim Hartwig
Sprache: VB.NET
Bewertung: 7,46
(3 Bewertungen)

Anzahl der Aufrufe: 4862
  

Beschreibung:

Diese Klasse ermöglicht es einem die Größe und Position eines Controls während der Laufzeit zu verändern, dazu werden 8 sogenannte GrabRects um das Control gezeichnet (wie man es aus der IDE kennt) von denen man 3 (gelb untermalt) benutzen kann um die Größe zu verändern (in Visual Studio geht das bei allen Ecken). Um das Control zu verschieben geht man genauso vor wie in VS.

Wenn man die Funktionalität entfernen will muss man die Finish-Methode aufrufen.


Abgelegt unter: Größe, Control.



Visual Basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
Public Class clsResizeControl
    Private WithEvents mControl As Control
    Private WithEvents mOwnerForm As Form
    Private GRS As Integer = 6
    Private InResizeMode As Boolean = False
    Private MoveControl As Boolean = False
    Private LastMousePos As Point
    Private mMinWidth As Integer = 20
    Private mMinHeigth As Integer = 20
    Public GrabRectColor As Color = Color.Yellow

    Private GrabRects() As Control = { _
        New Control, _
        New Control, _
        New Control, _
        New Control, _
        New Control, _
        New Control, _
        New Control, _
        New Control}

    ''' <summary>
    ''' Diese Routine muss aufgerufen werden wenn man die Resize-Funktionalität vom Control entfernen möchte.
    ''' </summary>
    Public Sub Finish()
        RemoveHandler mOwnerForm.Paint, AddressOf Paint

        RemoveHandler mControl.MouseDown, AddressOf ControlMouseDown
        RemoveHandler mControl.MouseMove, AddressOf ControlMouseMove
        RemoveHandler mControl.MouseUp, AddressOf ControlMouseUp

        For i As Integer = 0 To GrabRects.Length - 1
            RemoveHandler GrabRects(i).Paint, AddressOf GrabRectPaint
            RemoveHandler GrabRects(i).MouseClick, AddressOf GrabRectMouseClick
            RemoveHandler GrabRects(i).MouseMove, AddressOf GrabRectMouseMove
            RemoveHandler GrabRects(i).MouseDown, AddressOf GrabRectMouseDown
            RemoveHandler GrabRects(i).MouseUp, AddressOf GrabRectMouseUp
            RemoveHandler GrabRects(i).Move, AddressOf GrabRectMove

            GrabRects(i).Dispose()
        Next

        GrabRects = Nothing

        mOwnerForm.Refresh()
    End Sub

    Sub New(ByVal NewControl As Control, ByVal OwnerForm As Form)
        mControl = NewControl
        mOwnerForm = OwnerForm

        AddHandler mOwnerForm.Paint, AddressOf Paint

        AddHandler mControl.MouseDown, AddressOf ControlMouseDown
        AddHandler mControl.MouseMove, AddressOf ControlMouseMove
        AddHandler mControl.MouseUp, AddressOf ControlMouseUp

        mControl.Cursor = Cursors.SizeAll

        For i As Integer = 0 To GrabRects.Length - 1
            AddHandler GrabRects(i).Paint, AddressOf GrabRectPaint
            AddHandler GrabRects(i).MouseClick, AddressOf GrabRectMouseClick
            AddHandler GrabRects(i).MouseMove, AddressOf GrabRectMouseMove
            AddHandler GrabRects(i).MouseDown, AddressOf GrabRectMouseDown
            AddHandler GrabRects(i).MouseUp, AddressOf GrabRectMouseUp
            AddHandler GrabRects(i).Move, AddressOf GrabRectMove
        Next

        mOwnerForm.Refresh()
    End Sub

    Private Sub ControlMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
        MoveControl = True
        LastMousePos = New Point(e.X, e.Y)

        For i As Integer = 0 To GrabRects.Length - 1
            GrabRects(i).Visible = False
        Next
        mOwnerForm.Refresh()
    End Sub

    Private Sub ControlMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
        If MoveControl = True Then
            mControl.Left = mControl.Left + e.X - LastMousePos.X
            mControl.Top = mControl.Top + e.Y - LastMousePos.Y
            mOwnerForm.Refresh()
        End If
    End Sub

    Private Sub ControlMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
        MoveControl = False

        For i As Integer = 0 To GrabRects.Length - 1
            GrabRects(i).Visible = True
        Next
        mOwnerForm.Refresh()
    End Sub

    Public Sub Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
        If InResizeMode = True Or MoveControl = True Then Exit Sub
        With e.Graphics
            'Rahmen
            .DrawRectangle(Pens.Gray, New Rectangle( _
            mControl.Location.X - (GRS / 2), _
            mControl.Location.Y - (GRS / 2), _
            mControl.Width + GRS, _
            mControl.Height + GRS))

            'Positionen für die GrabRects in der Reihenfolge:
            'TopLeft, TopMiddle, TopRight, MiddleLeft, MiddleRight, BottomLeft,BottomMiddle,BottomRight
            Dim GrabRectPos() As Point = { _
            New Point(mControl.Location.X - GRS, mControl.Location.Y - GRS), _
            New Point(mControl.Location.X + (mControl.Width / 2) - (GRS / 2), mControl.Location.Y - GRS), _
            New Point(mControl.Location.X + mControl.Width, mControl.Location.Y - GRS), _
            New Point(mControl.Location.X - GRS, mControl.Location.Y + (mControl.Height / 2) - (GRS / 2)), _
            New Point(mControl.Location.X + (mControl.Width), mControl.Location.Y + (mControl.Height / 2) - (GRS / 2)), _
            New Point(mControl.Location.X - GRS, mControl.Location.Y + (mControl.Height)), _
            New Point(mControl.Location.X + (mControl.Width / 2) - (GRS / 2), mControl.Location.Y + (mControl.Height)), _
            New Point(mControl.Location.X + mControl.Width, mControl.Location.Y + (mControl.Height))}

            For i As Integer = 0 To GrabRects.Length - 1
                GrabRects(i).Location = GrabRectPos(i)
                GrabRects(i).Size = New Size(GRS, GRS)
                GrabRects(i).BackColor = Color.White
                GrabRects(i).Parent = mOwnerForm
            Next

            GrabRects(4).BackColor = GrabRectColor
            GrabRects(6).BackColor = GrabRectColor
            GrabRects(7).BackColor = GrabRectColor
        End With
    End Sub

    Private Sub GrabRectPaint(ByVal sender As Object, ByVal e As PaintEventArgs)
        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(0, 0, GRS - 1, GRS - 1))
    End Sub

    Private Sub GrabRectMove(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim CurrControl As Control = CType(sender, Control)
        Dim Index As Integer = GetCurrIndex(sender)
    End Sub

    Private Sub GrabRectMouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
        Dim CurrControl As Control = CType(sender, Control)
        Dim Index As Integer = GetCurrIndex(sender)
    End Sub

    Private Sub GrabRectMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
        Dim CurrControl As Control = CType(sender, Control)
        Dim Index As Integer = GetCurrIndex(sender)
        Dim MousePos As Point
        Static Right As Integer
        Static Bottom As Integer

        Select Case Index
            Case 0 : CurrControl.Cursor = Cursors.SizeNWSE
            Case 1 : CurrControl.Cursor = Cursors.SizeNS
            Case 2 : CurrControl.Cursor = Cursors.SizeNESW

            Case 3 : CurrControl.Cursor = Cursors.SizeWE
            Case 4 : CurrControl.Cursor = Cursors.SizeWE

            Case 5 : CurrControl.Cursor = Cursors.SizeNESW
            Case 6 : CurrControl.Cursor = Cursors.SizeNS
            Case 7 : CurrControl.Cursor = Cursors.SizeNWSE
        End Select

        If InResizeMode = True Then
            MousePos = mOwnerForm.PointToClient(Cursor.Position)
            CurrControl.Left = mOwnerForm.PointToClient(Cursor.Position).X 'CurrControl.Left + e.X
            CurrControl.Top = mOwnerForm.PointToClient(Cursor.Position).Y 'CurrControl.Top + e.Y

            Dim TmpHeigth As Integer
            Dim TmpWidth As Integer

            Select Case Index
                Case 0 'TopLeft GrabButton

                Case 1 'TopMiddle GrabButton

                Case 2 'TopRight GrabButton

                Case 3 'MiddleLeft GrabButton

                Case 4 'MiddleRight GrabButton
                    TmpWidth = GrabRects(4).Left - mControl.Left
                    If TmpWidth < mMinWidth Then TmpWidth = mMinWidth
                    mControl.Width = TmpWidth

                Case 5 'BottomLeft GrabButton

                Case 6 'BottomMiddle GrabButton
                    TmpHeigth = CurrControl.Top - mControl.Top
                    If TmpHeigth < mMinHeigth Then TmpHeigth = mMinHeigth
                    mControl.Height = TmpHeigth

                Case 7 'BottomRight GrabButton
                    TmpHeigth = CurrControl.Top - mControl.Top
                    TmpWidth = CurrControl.Left - mControl.Left

                    If TmpHeigth < mMinHeigth Then TmpHeigth = mMinHeigth
                    If TmpWidth < mMinWidth Then TmpWidth = mMinWidth

                    mControl.Size = New Size(TmpWidth, TmpHeigth)
            End Select
            mOwnerForm.Refresh()
        Else
            Right = mControl.Right
            Bottom = mControl.Bottom
        End If
    End Sub

    Private Sub GrabRectMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
        InResizeMode = True
        For i As Integer = 0 To GrabRects.Length - 1
            GrabRects(i).Visible = False
        Next
        mOwnerForm.Refresh()
    End Sub

    Private Sub GrabRectMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
        InResizeMode = False
        For i As Integer = 0 To GrabRects.Length - 1
            GrabRects(i).Visible = True
        Next
        mOwnerForm.Refresh()
    End Sub

    Private Function GetCurrIndex(ByVal sender As Object) As Integer
        Dim CurrControl As Control = CType(sender, Control)
        For i As Integer = 0 To GrabRects.Length
            If GrabRects(i) Is CurrControl Then
                Return i
            End If
        Next
    End Function
End Class
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



Diese Snippets könnten für Sie interessant sein:
[VB.NET] Bildgröße verändern (Verhältnis)
[VB.NET] Bildgröße verändern (Prozentual)
[VB.NET] Bildgröße verändern (Manuell)
[VB.NET] Neues Größenverhältnis eines Bilds berechnen
[VB.NET] Größe eines Strings berechnen
[ASP.net] iFrame Grösse dem Inhalt anpassen
[C#] Größe verändern von Bildern
[C#] Create Nice File Size in C#
[VB.NET] Ordnergröße ermitteln
[C#] Größe eines Strings berechnen
[C#] Größe eines Bildes mit mit gleichem Seitenverhältniss ändern
[C#] Größe einer SQL-Server Datenbank ermitteln
[VB.NET] Größe einer SQL-Server Datenbank ermitteln
[C#] Byte Größenangaben als String formatieren (KB, MB, GB, ...)
[C#] Doppelbufferung für Steuerelemente aktivieren
[C#] Transparenter Hintergrund beim Steuerelement
[C#] CursorChanger
[ASP.net] NullReferenceException
[C#] Generischer Invoker für Windows Form Controls
[ASP.net] Formularelement fokusieren auf Ajax Seiten
[C#] Threadsichere und generische Kommunikation Windows Forms
[C#] Events aus Worker-Thread im GUI-Thread werfen! Kein Invoke!
[C#] Controls gleichen Typs von einem Container in Winform holen
[C#] Eigenschaften aller Steuerlemente eines Formulars setzen
[VB.NET] Alle Textboxen zurücksetzen
[ASP.net] bestimmtes Control auf der aspx-Seite finden
[VB.NET] Oft benötigte Properties von Controls schnell zentral ausfüh
[VB.NET] Das Klick-Event von Controls gemeinsam auffangen und zentral
[VB.NET] Linien u. Kreise a la VB6 - Einfach Zeichnen
[C#] iTunes-ProgressBar
[C#] WaterBox
[VB.NET] List all controls by using the name of the controls
[C#] Custom Toolstrip Renderer
[VB.NET] Alle Controls einer Form Enablen/Disablen
[C#] Löschen aller TextControls auf einer Form in C#
[VB.NET] Control Resize wie in der IDE auch zur Laufzeit - Extended

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.