private void Window_Loaded(object sender, RoutedEventArgs e) { WinPosition(); } /// <summary> /// 设置窗体在屏幕的位置 /// </summary> public void WinPosition() { double ScreenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;//WPF this.Top = this.ActualWidth / 5; this.Left = ScreenWidth - this.ActualWidth *1.3; }
我设置窗体初始位置在屏幕右上角,怎么把WinPosition进一步封装到类
定义一个类,将WinPosition定义为静态方法喽
这两条代码怎么调用所在窗体
this.Top = this.ActualWidth / 5; this.Left = ScreenWidth - this.ActualWidth *1.3;
static AdjustPosition(Window win)
{
double ScreenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;//WPF
win.Top = win.ActualWidth / 5;
win.Left = ScreenWidth - win.ActualWidth *1.3;
}
public class BaseWindow:Window
{
void Window_Loaded(object sender, RoutedEventArgs e) { AdjustPosition(this); }
}
class YourWindow:BaseWindow
{}