【问题标题】:Determine whether Width or Height or Both is changed on Resize?确定在调整大小时是否更改了宽度或高度或两者?
【发布时间】:2012-10-17 16:43:27
【问题描述】:

我正在尝试为我的表单按比例调整大小,因此我需要知道每次调整大小时,究竟调整了哪些大小 - 宽度、高度或两者兼而有之。如何从System.EventArgs 参数中获取该信息?

【问题讨论】:

  • 你从 Me.Size 取而代之。旧的大小是这个属性之前的值,直接保存就行了。

标签: vb.net resize


【解决方案1】:

要按比例调整窗体上的子控件的大小,最好使用名为TableLayoutPanel 的本机 .NET 控件 - 这样可以避免大量手动编码。否则,您可以使用Me.Size 并编写如下内容:

Dim _oldSize As Size
Dim _allowScaling As Boolean = False

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  '[...] perform initial setup of your controls
  _oldSize = Me.Size
  _allowScaling = True
End Sub

Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
  If Not _allowScaling Then Exit Sub

  Dim deltaSize As Size = Me.Size - _oldSize
  Dim deltaWidth As Integer = Math.Abs(deltaSize.Width)
  Dim deltaHeight As Integer = Math.Abs(deltaSize.Height)

  If deltaWidth > 0 And deltaHeight > 0 Then
    'both width and height have changed
  ElseIf deltaWidth > 0 Then
    'width has changed
  ElseIf deltaHeight > 0 Then
    'height has changed
  End If

  _oldSize = Me.Size
End Sub

【讨论】:

    猜你喜欢
    • 2021-08-06
    • 2012-09-21
    • 1970-01-01
    • 2014-11-20
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    相关资源
    最近更新 更多