How to make a dialog(view) open up on the same monitor as the main window

落爺英雄遲暮 提交于 2019-12-31 00:50:11

问题


In a PC with multiple monitors, say you run your application which have have a second Window/Dialog other than the main window (such as Options) that you want it to open in the same screen/monitor as your MainWindow. How to force this behavior?

You basically want to stop the scenario that your MainWindow is on one monitor and when you bring up the "Options" page, it shows on a different screen/monitor.


回答1:


Have you looked at the WindowStartupLocation property for Window?

CenterScreen places the Window in the center of the screen containing the cursor, which should normally be fine. For example, if a user clicks a button on your Window and a dialog opens, the cursor will still be over the button and thus the dialog will show up in the center of the same Window.

CenterOwner places the Dialog in the center of the Window specified as it's owner. Declare the new Window similar to this:

MyDialog d = new MyDialog { Owner = parentWindow };
d.ShowDialog();



回答2:


I'd like to add to this the fact that if you start the second dialog maximized, it will default to the main screen. As a workaround, I set the maximize flag in the OnLoad method.



来源:https://stackoverflow.com/questions/29023376/how-to-make-a-dialogview-open-up-on-the-same-monitor-as-the-main-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!