Getting SetDesktopBounds to stick

本文关键字:stick to SetDesktopBounds Getting | 更新日期: 2023-09-27 18:17:41

我正在调用System.Windows.Forms…SetDesktopBounds(x, y, width, height)在刚刚创建但未显示的表单上。当我显示表单,它不去我的SetDesktopBounds点,但显示在其他地方。

如果我显示表单,然后SetDesktopBounds,然后隐藏它…它在下次我显示它的位置。有人知道这是怎么回事吗?我不想事先做Show因为那样你会在我设置的时候看到表单的闪烁

在调用SetDesktopBounds之前调用Activate和/或CreateControl也不起作用。

谢谢!

Getting SetDesktopBounds to stick

如果在表单的Load事件触发之前调用它,将不会得到满意的结果。在此之前,窗口的实际大小是未知的,用户的首选项和重新缩放将在创建本机窗口时生效。如果你在加载后这样做,那么重新定位将是可见的。所以这是最好的:

    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var main = new Form1();
        main.Load += delegate { main.SetDesktopBounds(100, 100, 300, 300);  };
        Application.Run(main);
    }

这是因为在默认情况下,表单初始化为StartPosition = FormStartPosition.WindowsDefaultLocation。将其改为FormStartPosition。手动的,你不需要任何回调加载表单后,它会立即忽略所有的强制定位由操作系统,调用SetDesktopBounds只是做你想要的。