在屏幕上摇动表格

时间:2013-05-22 10:44:10

标签: c# .net vb.net winforms system.drawing

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

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

我只会使用.net 4.5定位Windows 7.

更新

使用Hans和Vidstige的建议我已经提出了以下内容,当窗口最大化时也可以使用 - 我希望我可以选择两个答案,我已经通过Vidstige投票给你答案,并希望其他人也会这样做。汉斯的回答虽然突破了所有重点。

两种表单MainFormShakeForm

MainForm代码

 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代码

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

4 个答案:

答案 0 :(得分:15)

你尝试过这样的事吗?

    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;
    }

答案 1 :(得分:13)

典型的问题是在窗体上有太多的控件,使得绘画太慢。所以假装它,创建一个无边框窗口,显示窗体的位图并摇动它。使用表单的DrawToBitmap()方法创建位图。使用32bppPArgb作为像素格式,它比其他所有格式快十倍。

答案 2 :(得分:1)

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

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

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

答案 3 :(得分:-1)

这是一个小小的工作,你可以尝试一下。

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;
    }