当动画窗口窗体滑入时,窗体出现在错误的位置

时间:2011-10-25 14:15:00

标签: c# .net winforms animation location

我在windows窗体上有datagridviews,我正在使用小的添加对话框向datagridview添加记录,我希望它们在用户按下加载它们的按钮时动画显示。我正在使用

 [DllImport("user32")]
        static extern bool AnimateWindow(IntPtr hwnd, int time, int flags);
  

来源:http://www.c-sharpcorner.com/UploadFile/kirtan007/761/

对话框在屏幕的左上方显示动画。我在添加对话框的Load事件上使用了下面的代码:

//Set the Location  negative values are being returned when my dialog appears
                this.Location = new Point(LocationMainX + WidthOfMain, locationMainy + 10);

                //Animate form
                AnimateWindow(this.Handle, 750, AW_SLIDE | AW_HOR_POSITIVE);

在父表单中,我将其位置传递给子添加表单

AddForm form = new AddForm (this.DesktopLocation)
form.ShowDialog(); //I have also noticed doing form.Show(); messes with the position of dialog

我的主表单在另一个表单中加载,所以我猜它正在返回相对位置。但我试过了:

AddForm form = new AddForm (this.Parent.DesktopLocation)
    form.ShowDialog();

这不返回负值但返回(0,24)也是不正确的。因为对话框的动画效果比父窗体大约150像素。

当我将表单设置为相对this.Parent.Parent.Location然后它显示正确,所以我猜是否有任何正式的方式来访问应用程序的根父级而不是这样做。 parent.parent .....

1 个答案:

答案 0 :(得分:0)

根据您的修改,您只能使用this.ParentForm代替this.Parent。检查是否为空,以防等等。

<强>更新

不确定您希望此表单显示在何处。也许只使用一些pinvoke

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
  public int Left;
  public int Top;
  public int Right;
  public int Bottom;
}

然后显示表格:

RECT fromRECT;
GetWindowRect(new HandleRef(this, button1.Handle), out fromRECT);
form.Location = new Point(fromRECT.Left + (fromRECT.Right - fromRECT.Left), fromRECT.Top);
form.Show();

表单将显示在按钮的右侧。