Как я могу позиционировать позицию окна при запуске в правой части экрана пользователя?
В настоящее время я создаю приложение на стороне боковой панели WPF на С#. Когда пользователь запускает приложение, я хотел бы, чтобы окно автоматически позиционировало его самостоятельно в стороне экрана пользователя. Я пробовал несколько методов и поисковых запросов Google, но не нашел никакой помощи.
Вот пример того, что я пытаюсь сделать:
http://prntscr.com/5tfkz
Как я могу эффективно достичь чего-то подобного?
@dknaack
Я пробовал этот код:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Left = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right - this.Width;
this.Top = 0;
this.Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
}
и получили следующие ошибки:
Ошибка 1 Тип "System.Drawing.Size" определен в сборке, на которую не ссылаются. Вы должны добавить ссылку на сборку "System.Drawing, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a". C:\Users\Test\Documents\Expression\Blend 4\Projects\WindBar_Prototype_1\WindBar_Prototype_1\MainWindow.xaml.cs 32 13 WindBar_Prototype_1
и
Ошибка 2 'System.Drawing.Size' не содержит определения для 'Width', и не может быть найден метод расширения 'Width', принимающий первый аргумент типа 'System.Drawing.Size' (вам не хватает использования директива или ссылка на сборку?) C:\Users\Test\Documents\Expression\Blend 4\Projects\WindBar_Prototype_1\WindBar_Prototype_1\MainWindow.xaml.cs 32 78 WindBar_Prototype_1
Любая помощь?
Ответы
Ответ 1
Описание
Вы можете использовать Screen
от System.Windows.Forms
.
Поэтому добавьте ссылку на System.Windows.Forms.dll
и System.Drawing.dll
. Затем измените свойства Left
и Height
в методе MainWindow_Loaded
.
Пример
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.Left = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right - this.Width;
this.Top = 0;
this.Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
}
Дополнительная информация
Ответ 2
Вы можете сделать это без ссылки на сборки форм выигрышей с помощью SystemParameters
. В коде для вашего окна XAML:
MainWindow() {
InitializeComponents();
this.Loaded += new RoutedEventHandler(
delegate(object sender, RoutedEventArgs args) {
Width = 300;
Left = SystemParameters.VirtualScreenLeft;
Height = SystemParameters.VirtualScreenHeight;
}
}
Документация SystemParameters
Ответ 3
в вашем xaml:
WindowStartupLocation="Manual"
в конструкторе:
Left = System.Windows.SystemParameters.PrimaryScreenWidth - Width
Top=0
Ответ 4
public MainWindow()
{
InitializeComponent();
WindowStartupLocation = WindowStartupLocation.Manual;
Left = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width - Width;
}
Ответ 5
использовать свойство свойства или местоположения startPosition
Ответ 6
<body>
<script>
function myfunc()
{
w1=window.open('http://www.google.com','Google','left=0,top=0,width=250px,height=250px');
w2=window.open('http://www.yahoomail.com','Yahoo Mail','left=1166,top=0,width=250px,height=250px');
w3=window.open('http://www.people.com','People Magazine','left=1166,top=500,width=250px,height=250px');
w4=window.open('http://www.time.com','Time Magazines','left=0,top=500,width=250px,height=250px');
w5=window.open('http://www.ew.com','Entertainment Weekly','left=550,top=250,width=250px,height=250px');
}
function myclose()
{
w1.close(); w2.close(); w3.close(); w4.close(); w5.close();
w6=window.open('smartwindow.html',' mywindow',',');
}
</script>
<div id="cover">
<img src="images/abstract.jpeg" id="1" width="500px" height="400px" alt="color defined"onClick="myfunc()"/>
<input type="button" value="click to close all windows" onClick="myclose()"/>
</div>
</body>