157 lines
4.9 KiB
C#
157 lines
4.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
|
||
namespace SamplePre.UIWpf.BaseWindows
|
||
{
|
||
public class BaseWindow : Window
|
||
{
|
||
public BaseWindow()
|
||
{
|
||
//InitializeStyle();
|
||
//this.Loaded += delegate
|
||
//{
|
||
// InitializeEvent();
|
||
//};
|
||
|
||
InitializeStyle();
|
||
// 一定要在 Loaded 里执行,确保模板已加载
|
||
Loaded += (s, e) => InitializeEvent();
|
||
|
||
|
||
}
|
||
|
||
|
||
private void InitializeEvent()
|
||
{
|
||
// ✅ 关键:从可视化树查找模板内的元素
|
||
Button btnMin = FindChild<Button>(this, "btnMin");
|
||
Button btnMax = FindChild<Button>(this, "btnMax");
|
||
Button btnClose = FindChild<Button>(this, "btnClose");
|
||
Border borderTitle = FindChild<Border>(this, "borderTitle");
|
||
|
||
// 最小化
|
||
if (btnMin != null)
|
||
btnMin.Click += (s, e) => WindowState = WindowState.Minimized;
|
||
|
||
// 最大化/还原
|
||
if (btnMax != null)
|
||
btnMax.Click += (s, e) =>
|
||
WindowState = WindowState == WindowState.Normal ? WindowState.Maximized : WindowState.Normal;
|
||
|
||
// 关闭
|
||
if (btnClose != null)
|
||
btnClose.Click += (s, e) => Close();
|
||
|
||
// 拖动窗口
|
||
if (borderTitle != null)
|
||
{
|
||
borderTitle.MouseMove += (s, e) =>
|
||
{
|
||
if (e.LeftButton == MouseButtonState.Pressed)
|
||
DragMove();
|
||
};
|
||
|
||
// 双击标题最大化
|
||
borderTitle.MouseLeftButtonDown += (s, e) =>
|
||
{
|
||
if (e.ClickCount >= 2)
|
||
WindowState = WindowState == WindowState.Normal ? WindowState.Maximized : WindowState.Normal;
|
||
};
|
||
}
|
||
}
|
||
|
||
#region 万能查找控件方法(核心)
|
||
/// <summary>
|
||
/// 在可视化树中查找指定名称的子元素
|
||
/// </summary>
|
||
public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
|
||
{
|
||
if (parent == null) return null;
|
||
|
||
T foundChild = null;
|
||
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
|
||
|
||
for (int i = 0; i < childrenCount; i++)
|
||
{
|
||
var child = VisualTreeHelper.GetChild(parent, i);
|
||
|
||
if (child is T tChild && child is FrameworkElement element && element.Name == childName)
|
||
{
|
||
foundChild = tChild;
|
||
break;
|
||
}
|
||
|
||
foundChild = FindChild<T>(child, childName);
|
||
if (foundChild != null) break;
|
||
}
|
||
return foundChild;
|
||
}
|
||
#endregion
|
||
|
||
private void InitializeStyle()
|
||
{
|
||
Style = (Style)Application.Current.Resources["BaseWindowStyle"];
|
||
}
|
||
|
||
////==================================================
|
||
//private void InitializeEvent()
|
||
//{
|
||
// ControlTemplate baseWindowTemplate = (ControlTemplate)App.Current.Resources["BaseWindowControlTemplate"];
|
||
|
||
// //Button minBtn = (Button)baseWindowTemplate.FindName("btnMin", this);
|
||
// //minBtn.Click += delegate
|
||
// //{
|
||
|
||
// // this.WindowState = WindowState.Minimized;
|
||
// //};
|
||
|
||
// //Button maxBtn = (Button)baseWindowTemplate.FindName("btnMax", this);
|
||
// //maxBtn.Click += delegate
|
||
// //{
|
||
|
||
// // this.WindowState = (this.WindowState == WindowState.Normal ? WindowState.Maximized : WindowState.Normal);
|
||
// //};
|
||
|
||
// //Button closeBtn = this.FindName("btnClose") as Button;
|
||
|
||
// 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)
|
||
// {
|
||
// if (e.LeftButton == MouseButtonState.Pressed)
|
||
// {
|
||
// this.DragMove();
|
||
// }
|
||
// };
|
||
// borderTitle.MouseLeftButtonDown += delegate (object sender, MouseButtonEventArgs e)
|
||
// {
|
||
// if (e.ClickCount >= 2)
|
||
// {
|
||
// //maxBtn.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
|
||
// }
|
||
// };
|
||
//}
|
||
|
||
|
||
//private void InitializeStyle()
|
||
//{
|
||
// this.Style = (Style)App.Current.Resources["BaseWindowStyle"];
|
||
//}
|
||
|
||
|
||
|
||
}
|
||
}
|