vb.net 在屏幕上摇动表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16689741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:41:29  来源:igfitidea点击:

Shake form on screen

c#.netvb.netwinformssystem.drawing

提问by GJKH

I would like to 'shake' my winforms form to provide user feedback, much like the effect used on a lot of mobile OS's.

我想“摇动”我的 winforms 表单以提供用户反馈,就像在许多移动操作系统上使用的效果一样。

I can obviously set the location of the window and go back and forth with Form1.Location.Xetc. but the effect from this method is terrible. I'd like something a little more fluent - or alternatively is there a way to shake the entire screen?

我显然可以设置窗口的位置并来回Form1.Location.X等,但这种方法的效果很糟糕。我想要更流畅的东西 - 或者有没有办法摇动整个屏幕?

I'll only be targeting Windows 7 using .net 4.5.

我将只针对使用 .net 4.5 的 Windows 7。

Update

更新

Using both Hans and Vidstige suggestions I've come up with the following, which also works when the window is maximized - I wish I could pick two answers, I've up-voted your answer though Vidstige and hope others will too. Hans' answer hits all the salient points though.

使用 Hans 和 Vidstige 的建议,我提出了以下建议,这在窗口最大化时也有效 - 我希望我可以选择两个答案,虽然 Vidstige 已经对你的答案投了赞成票,但希望其他人也能这样做。不过,汉斯的回答触及了所有要点。

Two forms MainFormand ShakeForm

两种形式MainFormShakeForm

MainForm Code

主窗体代码

 Private Sub shakeScreenFeedback()

        Dim f As New Shakefrm
        Dim b As New Bitmap(Me.Width, Me.Height, PixelFormat.Format32bppArgb)

        Me.DrawToBitmap(b, Me.DisplayRectangle)

        f.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        f.Width = Me.Width
        f.Height = Me.Height
        f.ShowInTaskbar = False

        f.BackgroundImage = b
        f.BackgroundImageLayout = ImageLayout.Center
        f.Show(Me)
        f.Location = New Drawing.Point(Me.Location.X, Me.Location.Y)

        'I found putting the shake code in the formLoad event didn't work
        f.shake()
        f.Close()

        b.Dispose()

    End Sub

ShakeForm Code

ShakeForm 代码

Public Sub shake()
    Dim original = Location
    Dim rnd = New Random(1337)
    Const shake_amplitude As Integer = 10
    For i As Integer = 0 To 9
        Location = New Point(original.X + rnd.[Next](-shake_amplitude, shake_amplitude), original.Y + rnd.[Next](-shake_amplitude, shake_amplitude))
        System.Threading.Thread.Sleep(20)
    Next
    Location = original

End Sub

回答by vidstige

Have you tried something like this?

你有没有尝试过这样的事情?

    private void shakeButton_Click(object sender, EventArgs e)
    {
        Shake(this);
    }

    private static void Shake(Form form)
    {
        var original = form.Location;
        var rnd = new Random(1337);
        const int shake_amplitude = 10;
        for (int i = 0; i < 10; i++)
        {
            form.Location = new Point(original.X + rnd.Next(-shake_amplitude, shake_amplitude), original.Y + rnd.Next(-shake_amplitude, shake_amplitude));
            System.Threading.Thread.Sleep(20);
        }
        form.Location = original;
    }

回答by Hans Passant

The typical problem is having way too many controls on the form, making the painting too slow. So just fake it, create a borderless window that displays a bitmap of the form and shake that one. Create the bitmap with the form's DrawToBitmap() method. Use 32bppPArgb for the pixel format, it draws ten times faster than all the other ones.

典型的问题是表单上的控件太多,导致绘制速度太慢。所以只是伪造它,创建一个无边界窗口,显示表单的位图并摇动那个。使用窗体的 DrawToBitmap() 方法创建位图。使用 32bppPArgb 作为像素格式,它的绘制速度是其他所有格式的十倍。

回答by Santosh Panda

You can use the Aero Shake feature of Windows 7 to achieve this.

您可以使用 Windows 7 的 Aero Shake 功能来实现此目的。

Better you can have a look in the below link for more details:

更好的是,您可以查看以下链接以获取更多详细信息:

http://www.codeproject.com/Articles/36294/Aero-Shake

http://www.codeproject.com/Articles/36294/Aero-Shake

回答by Ramesh

Here is a small work around, you may try it.

这是一个小的解决方法,您可以尝试一下。

private void button1_Click(object sender, EventArgs e)
    {
        this.Width = this.Width - 10;
        this.Height = this.Height - 10;
        Thread.Sleep(15);
        this.Width = this.Width + 10;
        this.Height = this.Height + 10;
    }