在 VB.NET 中以动画方式调整窗体大小

发布于 2024-07-30 12:03:09 字数 284 浏览 2 评论 0原文

我的桌面窗口右下角的任务栏/系统托盘上方有一个表单。 这有点像弹出通知。 通知本身看起来很棒,但它上面有一个按钮,可以向上调整表单大小,以 5 像素的增量动画调整大小,同时保持其相对于屏幕右下角的位置。

这样做的问题是它看起来不太光滑。 调整 Me.Width 从左侧调整大小,因此您必须使用 Me.Left 将表单向左移动以进行补偿。 Me.SetBounds 似乎只是设置这些属性的包装器。

我可以做些什么来使表单顺利(或至少看起来)从表单左侧向外调整大小?

I have a form positioned slightly above the taskbar/system tray in the bottom right corner of my desktop window. It's sort of like a pop-up notification. The notification itself looks great, but there's a button on it which resizes the form up, animating the sizing in increments of 5px whilst keeping it's position relative to the bottom-right corner of the screen.

The problem with this is that it doesn't look very smooth. Adjusting Me.Width resizes from the left so you have to then move the form to the left with Me.Left to compensate. Me.SetBounds just seems to be a wrapper for setting those properties anyway.

Is there anything I can do to have the form smoothly (or at least appear to) resize outwards from the left of the form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

您的好友蓝忘机已上羡 2024-08-06 12:03:09

预计到达时间:您实际上可以使用 SetBounds 来完成此操作,因为 SetBounds 将委托给 SetBoundsCore,而 SetBoundsCore 又将调用设置窗口位置。 因此,SetBounds 将在内部真正一次性设置所有边界,并且窗口管理器将仅在设置所有属性后重绘窗口。


另一种选择是导入 MoveWindow function并使用它。 它在这里产生平滑的动画,因为它可以在告诉窗口管理器重绘窗口之前同时设置大小和位置。

我的测试代码如下所示(在 Reflector 的帮助下从 C# 通过 IL 转换为 VB):

Private button1 As Button
Private components As IContainer = Nothing
Private tm As Timer = New Timer

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    MyBase.Left = (Screen.PrimaryScreen.WorkingArea.Width - MyBase.Width)
    MyBase.Top = (Screen.PrimaryScreen.WorkingArea.Height - MyBase.Height)
    Me.tm.Enabled = False
    Me.tm.Interval = 20
    AddHandler Me.tm.Tick, Function 
        If (MyBase.Width < 500) Then
            Form1.MoveWindow(MyBase.Handle, (MyBase.Left - 5), (MyBase.Top - 5), (MyBase.Width + 5), (MyBase.Height + 5), True)
        Else
            Me.tm.Enabled = False
        End If
    End Function
End Sub

<DllImport("user32.dll", SetLastError:=True)> _
Friend Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean
End Function

ETA: You can actually do this using SetBounds, as SetBounds will delegate to SetBoundsCore which, in turn will invoke SetWindowPos. So SetBounds will internally really set all bounds at once and the window manager will redraw the window only after all properties are set.


Another option would be to import the MoveWindow function and use that instead. It produces a smooth animation here, as it can set size and position simultaneously before telling the window manager to redraw the window.

My test code looked like this (converted from C# via IL to VB with help from Reflector):

Private button1 As Button
Private components As IContainer = Nothing
Private tm As Timer = New Timer

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    MyBase.Left = (Screen.PrimaryScreen.WorkingArea.Width - MyBase.Width)
    MyBase.Top = (Screen.PrimaryScreen.WorkingArea.Height - MyBase.Height)
    Me.tm.Enabled = False
    Me.tm.Interval = 20
    AddHandler Me.tm.Tick, Function 
        If (MyBase.Width < 500) Then
            Form1.MoveWindow(MyBase.Handle, (MyBase.Left - 5), (MyBase.Top - 5), (MyBase.Width + 5), (MyBase.Height + 5), True)
        Else
            Me.tm.Enabled = False
        End If
    End Function
End Sub

<DllImport("user32.dll", SetLastError:=True)> _
Friend Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean
End Function
很酷不放纵 2024-08-06 12:03:09

我从未尝试过此操作,但是您是否尝试过将表单的 DoubleBuffer 属性设置为 True? 这将使绘图变得平滑。

I never tried this, but have you tried setting the DoubleBuffer property of the form to True? This should smooth the drawing.

孤独患者 2024-08-06 12:03:09

您可以尝试减少像素步数,它也会增加到 1-2,5px 似乎需要调整大小很多,我可以看到这会显得不稳定。

You might try reducing the pixel steps it's incrementing down to 1-2 as well, 5px seems like a lot to be adjusting size, I could see how this could appear choppy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文