
拿到父級窗體的內容,放入一個容器裡,再在容器裡放入一個半透明層.將整個容器賦給父級窗體的內容.

關閉時反向操作.

消息窗彈出時
1 /// <summary>
2 /// 彈出消息框
3 /// </summary>
4 /// <param name="message">消息</param>
5 /// <param name="owner">父級窗體</param>
6 public static void ShowDialog(string message, Window owner)
7 {
8 //蒙板
9 Grid layer = new Grid() { Background = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0)) };
10 //父級窗體原來的內容
11 UIElement original = owner.Content as UIElement;
12 owner.Content = null;
13 //容器Grid
14 Grid container = new Grid();
15 container.Children.Add(original);//放入原來的內容
16 container.Children.Add(layer);//在上面放一層蒙板
17 //將裝有原來內容和蒙板的容器賦給父級窗體
18 owner.Content = container;
19
20 //彈出消息框
21 MessageBox box = new MessageBox() { Owner = owner };
22 box.tbc_message.Text = message;
23 box.ShowDialog();
24 }
消息框關閉時
1 /// <summary>
2 /// 窗體關閉事件
3 /// </summary>
4 private void Window_Closed(object sender, EventArgs e)
5 {
6 //容器Grid
7 Grid grid = this.Owner.Content as Grid;
8 //父級窗體原來的內容
9 UIElement original = VisualTreeHelper.GetChild(grid, 0) as UIElement;
10 //將父級窗體原來的內容在容器Grid中移除
11 grid.Children.Remove(original);
12 //賦給父級窗體
13 this.Owner.Content = original;
14 }
源碼下載:MessageBoxWithLayer.zip