我想用FormStartPosition.CenterParent从另一个表单打开一个表单。 当有多个屏幕并且我通过Visual Studio运行程序时,这可以正常工作。但是当我运行EXE文件时,它实际上无法工作。
var dialog = new Form();
dialog.StartPosition = FormStartPosition.CenterParent;
dialog.Show(this);
但是当我运行EXE文件(不是通过Visual Studio)并将我的表单拖到另一个屏幕并按下运行上述代码的按钮时,它仍然会在另一个屏幕中打开该表单。
我找到了一个在同一个屏幕上打开表单的解决方案,但应该有一种方法可以更方便地在CenterParent中打开表单吗?
以下代码片段将使表单始终至少在同一屏幕上打开:
var dialog = new Form();
dialog.StartPosition = FormStartPosition.Manual;
var currentScreen = Screen.FromControl(this);
dialog.Location = currentScreen.Bounds.Location;
dialog.Show(this);
有什么想法吗?
/罗布
答案 0 :(得分:0)
请参阅此帖子:https://stackoverflow.com/a/15862043/495455
您需要确定"虚拟屏幕"的大小,其中包括所有显示器。
int screenLeft = SystemInformation.VirtualScreen.Left;
int screenTop = SystemInformation.VirtualScreen.Top;
int screenWidth = SystemInformation.VirtualScreen.Width;
int screenHeight = SystemInformation.VirtualScreen.Height;