以下のコードを使用してサイズを変更できますが、右下隅から右側に向かってサイズ変更されます
ユーザーが左下隅からフォームのサイズを変更できるように、コードを変更したいと考えています。
また、このサイトで提供されているソリューションのほとんどは WndProc / WM_NCLBUTTONDOWN に基づいています。フォームには多くのコントロールがあり、ちらつきが非常に悪いため、使用していません。
Private Shared frmLastWidth As Integer = 0
Private Shared frmLastHeight As Integer = 0
Private Shared frmWidth As Integer
Private Shared frmHeight As Integer
Private Shared frmIsResizing As Boolean = False
Private frmRectangle As New System.Drawing.Rectangle()
Private Sub ResizeMe_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ResizeMe.MouseUp
If frmIsResizing Then
frmRectangle.Location = New System.Drawing.Point(Me.Left, Me.Top)
frmRectangle.Size = New System.Drawing.Size(frmWidth, frmHeight)
ControlPaint.DrawReversibleFrame(frmRectangle, Color.Black, System.Windows.Forms.FrameStyle.Dashed)
Me.Width = frmWidth
Me.Height = frmHeight
frmIsResizing = False
End If
End Sub
Private Sub ResizeMe_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ResizeMe.MouseDown
frmRectangle.Location = New System.Drawing.Point(Me.Left, Me.Top)
frmRectangle.Size = New System.Drawing.Size(frmWidth, frmHeight)
ControlPaint.DrawReversibleFrame(frmRectangle, Color.Black, System.Windows.Forms.FrameStyle.Dashed)
End Sub
Private Sub ResizeMe_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ResizeMe.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
'Me.ResizeRedraw = false
Dim sizeageX As Integer = MousePosition.X - Me.Location.X
Dim sizeageY As Integer = MousePosition.Y - Me.Location.Y
' Use this to restrict Width
If sizeageX < Me.MinimumSize.Width Then
sizeageX = Me.MinimumSize.Width
End If
' Use this to restrict Height
If sizeageY < Me.MinimumSize.Height Then
sizeageY = Me.MinimumSize.Height
End If
frmWidth = sizeageX
frmHeight = sizeageY
If frmLastWidth = 0 Then
frmLastWidth = frmWidth
End If
If frmLastHeight = 0 Then
frmLastHeight = frmHeight
End If
If frmIsResizing Then
frmRectangle.Location = New System.Drawing.Point(Me.Left, Me.Top)
frmRectangle.Size = New System.Drawing.Size(frmLastWidth, frmLastHeight)
End If
frmIsResizing = True
ControlPaint.DrawReversibleFrame(frmRectangle, Color.Black, System.Windows.Forms.FrameStyle.Dashed)
frmLastWidth = frmWidth
frmLastHeight = frmHeight
frmRectangle.Location = New System.Drawing.Point(Me.Left, Me.Top)
frmRectangle.Size = New System.Drawing.Size(frmWidth, frmHeight)
ControlPaint.DrawReversibleFrame(frmRectangle, Color.Black, System.Windows.Forms.FrameStyle.Dashed)
End Sub
Private Sub ResizeRight(ByVal e As System.Windows.Forms.MouseEventArgs)
'Me.ResizeRedraw = false
Dim sizeageX As Integer = (MousePosition.X + Me.Width) - Me.Location.X
Dim sizeageY As Integer = (MousePosition.Y + Me.Height) - Me.Location.Y
' Use this to restrict Width
If sizeageX < Me.MinimumSize.Width Then
sizeageX = Me.MinimumSize.Width
End If
' Use this to restrict Height
If sizeageY < Me.MinimumSize.Height Then
sizeageY = Me.MinimumSize.Height
End If
frmWidth = sizeageX
frmHeight = sizeageY
If frmLastWidth = 0 Then
frmLastWidth = frmWidth
End If
If frmLastHeight = 0 Then
frmLastHeight = frmHeight
End If
If frmIsResizing Then
frmRectangle.Location = New System.Drawing.Point(Me.Left, Me.Top)
frmRectangle.Size = New System.Drawing.Size(frmLastWidth, frmLastHeight)
End If
frmIsResizing = True
ControlPaint.DrawReversibleFrame(frmRectangle, Color.Black, System.Windows.Forms.FrameStyle.Dashed)
frmLastWidth = frmWidth
frmLastHeight = frmHeight
frmRectangle.Location = New System.Drawing.Point(Me.Left, Me.Top)
frmRectangle.Size = New System.Drawing.Size(frmWidth, frmHeight)
ControlPaint.DrawReversibleFrame(frmRectangle, Color.Black, System.Windows.Forms.FrameStyle.Dashed)
End If
End Sub
アップデート
フォームの左下隅のサイズを変更できましたが、破線が何度も表示され、破線はフォームの最小の高さと幅に制限されません変更されたコードは
Private Sub ResizeRight(ByVal e As System.Windows.Forms.MouseEventArgs)
'Me.ResizeRedraw = false
Dim sizeageX As Integer = MousePosition.X + Me.Location.X
Dim sizeageY As Integer = MousePosition.Y + Me.Location.Y
' Use this to restrict Width
If sizeageX > Me.MinimumSize.Width Then
sizeageX = Me.MinimumSize.Width
End If
' Use this to restrict Height
If sizeageY > Me.MinimumSize.Height Then
sizeageY = Me.MinimumSize.Height
End If
frmWidth = sizeageX - e.X
frmHeight = sizeageY - e.Y
If frmLastWidth = 0 Then
frmLastWidth = frmWidth
End If
If frmLastHeight = 0 Then
frmLastHeight = frmHeight
End If
If frmIsResizing Then
frmRectangle.Location = New System.Drawing.Point(Me.Left + e.X, Me.Top)
frmRectangle.Size = New System.Drawing.Size(frmLastWidth, frmLastHeight)
End If
frmIsResizing = True
ControlPaint.DrawReversibleFrame(frmRectangle, Color.Black, System.Windows.Forms.FrameStyle.Dashed)
frmLastWidth = frmWidth
frmLastHeight = frmHeight
frmRectangle.Location = New System.Drawing.Point(Me.Left + e.X, Me.Top)
frmRectangle.Size = New System.Drawing.Size(frmWidth, frmHeight)
ControlPaint.DrawReversibleFrame(frmRectangle, Color.Black, system.Windows.Forms.FrameStyle.Dashed)
End Sub