相信不少人使用wpf時會選擇自定義美觀的窗口,所以會設置WindowStyle="None" 取消自帶的標題欄。但這樣使用 WindowState="Maximized" 或者後臺 this.WindowState = System.Windows.WindowState.Maximized; 最大化窗口會覆蓋掉系統任務欄,即全屏了。這其實並非個很好的體驗。html
在網上找答案,排名靠前的都是提供用hook鉤子,篇幅很長,如:http://www.cnblogs.com/zhouyinhui/archive/2008/11/04/1326188.htmlui
我的感受這麼一個小功能添加那麼多的代碼是不人性的,因而繼續尋找,終於看到黎明的曙光:this
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都無用了。那該怎麼辦呢?看下邊:spa
在前臺添加:code
SizeChanged="Window_SizeChanged"
後臺:orm
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); } }
或者在最大化後禁用拖動功能,待恢復原始窗口後在恢復拖動功能也能夠。htm
ok,搞定! 這麼簡單的代碼,相信你們看的懂吧~~blog
另附雙擊標題欄事件:事件
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); } } }
最後總結代碼:get
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(); } };