65.9K
CodeProject 正在变化。 阅读更多。
Home

在运行时创建/移动/调整控件大小

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.47/5 (9投票s)

2007年5月25日

CPOL

2分钟阅读

viewsIcon

41637

downloadIcon

1147

通过为用户提供用于调整控件大小的“句柄”来在运行时创建和管理控件。

Screenshot - RuntimeResizer.png

引言

最近,我正在寻找一个 VB.NET 表单创建器,我发现了一个专业控件,售价约 140 美元,它在表单上的控件中添加了调整大小的句柄。所以我决定看看我是否可以在不花那么多钱的情况下复制该功能。此外,我一直在寻找一种方法,可以向表单中添加无限数量的控件,这些控件也具有事件处理程序。您可以在这里看到组合的结果。

使用代码

在代码中,我定义了五个标签 (H_Top_Right, H_Top_Left, H_Bottom_Left, H_Bottom_Right, Mover) 作为句柄。 Drag1-Drag4isMoving 变量用于跟踪特定的句柄是否处于拖动模式。 当鼠标按下时,该变量设置为“True”,当鼠标释放时设置为“False”。SelControl 变量跟踪当前聚焦的控件。 SetActive() 方法用于设置聚焦的控件。

这五个句柄相应地更改了 SelControllefttopheightwidth 属性

Private Sub H_Top_Left_Click (ByVal sender As System.Object, _
          ByVal e As System.Windows.Forms.MouseEventArgs) _
          Handles H_Top_Left.MouseMove
    If Not Drag1 then Exit Sub
    Me.H_Top_Left.Left += e.X
    Me.H_Top_Left.Top += e.Y
    SelControl.Width += -(e.X)
    SelControl.Height += -(e.Y)
    SelControl.Top = H_Top_Left.Top + 4
    SelControl.Left = H_Top_Left.Left + 4
    SetActive(SelControl)
End Sub

Private Sub H_Top_Right_Click (ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles H_Top_Right.MouseMove
    If Not Drag2 then Exit Sub
    Me.H_Top_Right.Left += e.X
    Me.H_Top_Right.Top += e.Y
    SelControl.Width += e.X
    SelControl.Height += -(e.Y)
    SelControl.Top = H_Top_Right.Top + 4
    SetActive(SelControl)
End Sub

Private Sub H_Bottom_Right_Click (ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles H_Bottom_Right.MouseMove
    If Not Drag3 then Exit Sub
    Me.H_Bottom_Right.Left += e.X
    Me.H_Bottom_Right.Top += e.Y
    SelControl.Width += e.X
    SelControl.Height += e.Y
    SetActive(SelControl)
End Sub

Private Sub H_Bottom_Left_Click (ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles H_Bottom_Left.MouseMove
    If Not Drag4 then Exit Sub
    Me.H_Bottom_Left.Left += e.X
    Me.H_Bottom_Left.Top += e.Y
    SelControl.Width += -(e.X)
    SelControl.Height += e.Y
    SelControl.Left = H_Bottom_Left.Left + 4
    SetActive(SelControl)
End Sub

Private Sub Mover_Click (ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles Mover.MouseMove
    If Not isMoving then Exit Sub
    Me.Mover.Left += e.X
    Me.Mover.Top += e.Y
    SelControl.Top = ((Me.Mover.Top + 4) * 2 - SelControl.Height) /2
    SelControl.Left = ((Me.Mover.Left + 4) * 2 - SelControl.Width) /2
    SetActive(SelControl)
End Sub

在这里,根据控件的当前位置计算所有五个句柄的位置。

SetActive() 方法将 SelControl 设置为接收按钮,并相应地定位句柄。

Private Sub SetActive (ByVal button as System.Object)

    SelControl = button

    Me.H_Top_Left.Left = button.Left - 4
    Me.H_Top_Left.Top = button.Top - 4

    Me.H_Top_Right.Left = button.Left + button.Width
    Me.H_Top_Right.Top = button.Top - 4

    Me.H_Bottom_Right.Left = button.Left + button.Width
    Me.H_Bottom_Right.Top = button.Top + button.Height 

    Me.H_Bottom_Left.Left = button.Left - 4
    Me.H_Bottom_Left.Top = button.Top + button.Height 

    Me.Mover.Left = (button.Left + button.Left + Button.Width) /2 - 4
    Me.Mover.Top = (Button.Top + Button.Top + Button.Height) /2 - 4

End Sub

Bug

是的!每个代码都有其错误!由于这是我第一次尝试做这样的事情,我肯定会犯一些错误。列出它们

  • 移动方法尚未实现,即,您无法像在 VS 中那样拖动按钮。您只能使用句柄移动它们。
  • 更新:您现在可以使用控件中心的白色方块移动控件

  • 如果将左上角句柄拖到按钮的底部或按钮的右侧,则会移动整个按钮。这意味着按钮以 0 的高度/宽度移动并移动到当前鼠标位置。令人恼火。

我将尝试及时找出这些错误。欢迎提出建议和提示。

我还将尝试为演示添加更多控件类型(例如,ComboBoxCheckBoxRadioButton 等)。

© . All rights reserved.