调整无边框表单的大小
我可以使用下面的代码调整表单的大小,但它会从右下角向右侧调整大小,
我想修改代码,以便用户可以从左下角调整表单的大小。
另外,这个网站上给出的大多数解决方案都是基于 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
I am able to resize the from using the code below but it resizes towards right side , from bottom right corner
i want to modify the code so that user can re size the form from bottom left corner.
also most of the solution given on this site are based on WndProc / WM_NCLBUTTONDOWN and i am not using it because my form have lots of controls so it flicker's very badly.
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
Update
i was able to re size the form bottom left corner but it shows the dashed lines many times also the dashed lines does not restrict to minimum height and width of the form the modified code is
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做的是根据 http://msdn.microsoft.com/en-us/library/system.windows.forms.control.suspendlayout%28VS.80%29.aspx
这也许能够阻止闪烁。
What you could do is suspend the Control Layout (Suspend.Layout) according to http://msdn.microsoft.com/en-us/library/system.windows.forms.control.suspendlayout%28VS.80%29.aspx
That might be able to stop the flickering.