相信很多人使用wpf時會選擇自定義美觀的窗口,因此會設置WindowStyle="None" 取消自帶的標題欄。但這樣使用 WindowState="Maximized" 或者后台 this.WindowState = System.Windows.WindowState.Maximized; 最大化窗口會覆蓋掉系統任務欄,即全屏了。這其實並不是個很好的體驗。
在網上找答案,排名靠前的都是提供用hook鈎子,篇幅很長,如:http://www.cnblogs.com/zhouyinhui/archive/2008/11/04/1326188.html
個人感覺這么一個小功能添加那么多的代碼是不人性的,於是繼續尋找,終於看到黎明的曙光:
Rect rcnormal;//定義一個全局rect記錄還原狀態下窗口的位置和大小。 /// <summary> /// 最大化 /// </summary> private void btnMaximize_Click(object sender, RoutedEventArgs e) { this.btnMaximize.Visibility = Visibility.Collapsed; this.btnNormal.Visibility = Visibility.Visible; rcnormal = new Rect(this.Left, this.Top, this.Width, this.Height);//保存下當前位置與大小 this.Left = 0;//設置位置 this.Top = 0; Rect rc = SystemParameters.WorkArea;//獲取工作區大小 this.Width = rc.Width; this.Height = rc.Height; } /// <summary> /// 還原 /// </summary> private void btnNormal_Click(object sender, RoutedEventArgs e) { this.Left = rcnormal.Left; this.Top = rcnormal.Top; this.Width = rcnormal.Width; this.Height = rcnormal.Height; this.btnMaximize.Visibility = Visibility.Visible; this.btnNormal.Visibility = Visibility.Collapsed; }
好了,最大化和最小化事件自定義好了。那如果窗口拖動到頂端鼠標出界的話窗口將會最大化是不是?在wpf中 WindowStyle="None" 下也還是全屏效果,而且會覆蓋掉我們自定義的效果,這個時候你的this.width和this.height都無用了。那該怎么辦呢?看下邊:
在前台添加:
SizeChanged="Window_SizeChanged"
后台:
private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { if (this.ActualHeight > SystemParameters.WorkArea.Height || this.ActualWidth > SystemParameters.WorkArea.Width) { this.WindowState = System.Windows.WindowState.Normal; btnMaximize_Click(null, null); } }
或者在最大化后禁用拖動功能,待恢復原始窗口后在恢復拖動功能也可以。
ok,搞定! 這么簡單的代碼,相信大家看的懂吧~~
另附雙擊標題欄事件:
private void Grid_MouseDown(object sender, MouseButtonEventArgs e) { if (e.ClickCount == 2) { if (this.ActualWidth == SystemParameters.WorkArea.Width) { btnNormal_Click(null, null); } else { btnMaximize_Click(null, null); } } }
最后總結代碼:
Rect rcnormal; protected virtual void InitializeEvent() { ControlTemplate baseWindowTemplate = (ControlTemplate)Application.Current.Resources["BaseWindowControlTemplate"]; TextBlock title = (TextBlock)baseWindowTemplate.FindName("lab_title", this); title.Text = window_title; title.Margin = mrg; Button maxBtn = (Button)baseWindowTemplate.FindName("btnMax", this); maxBtn.Visibility = btn_max; bool tag = true; maxBtn.Click += delegate { if (tag) { tag = false; rcnormal = new Rect(this.Left, this.Top, this.Width, this.Height); this.Left = 0; this.Top = 0; Rect rc = SystemParameters.WorkArea; this.Width = rc.Width; this.Height = rc.Height; maxBtn.Style = (Style)Application.Current.Resources["btn_max2"]; } else { tag = true; this.Left = rcnormal.Left; this.Top = rcnormal.Top; this.Width = rcnormal.Width; this.Height = rcnormal.Height; maxBtn.Style = (Style)Application.Current.Resources["btn_max"]; } }; Button closeBtn = (Button)baseWindowTemplate.FindName("btnClose", this); closeBtn.Click += delegate { this.Close(); }; //拖動 Border borderTitle = (Border)baseWindowTemplate.FindName("borderTitle", this); borderTitle.MouseMove += delegate(object sender, MouseEventArgs e) {
//tag==true 只有窗口恢復到原始窗口后才可拖動
if (e.LeftButton == MouseButtonState.Pressed && tag==true) {
this.DragMove();
}
};
