AvaloniaTool/ZTools/Views/MainWindow.axaml.cs

144 lines
4.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Collections.ObjectModel;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Media;
namespace ZTools.Views;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
AddColumns();
// AutoWeight();
InitAutoDrag();
}
private void Button_OpenSetting(object? sender, RoutedEventArgs e)
{
var settingWindow = new SettingWindow();
settingWindow.Show();
}
private void Button_OpenLog(object? sender, RoutedEventArgs e)
{
new LogWindow().Show();
}
private void Button_OpenTest(object? sender, RoutedEventArgs e)
{
new TestWindow().Show();
}
double _dragStartX;
double _initialWidth;
double _initialRightWidth;
bool _isResizing = false;
void InitAutoDrag()
{
// MainDockPanel.Children.Add(resizeThumb);
//
// MainDockPanel.Children.Add(resizeThumb1);
// 监听拖拽区域的PointerPressed事件
resizeThumb.PointerPressed += (sender, e) =>
{
if (e.GetCurrentPoint(MainDockPanel).Properties.IsLeftButtonPressed)
{
_dragStartX = e.GetPosition(this).X;
_initialWidth = MenuDockPanel.Width;
_initialRightWidth = SecondMenuDockPanel.Width;
_isResizing = true;
e.Handled = true;
}
};
resizeThumb1.PointerPressed += (sender, e) =>
{
if (e.GetCurrentPoint(SecondMenuDockPanel).Properties.IsLeftButtonPressed)
{
_dragStartX = e.GetPosition(this).X;
_initialWidth = SecondMenuDockPanel.Width;
_initialRightWidth = FunctionDockPanel.Width;
_isResizing = true;
e.Handled = true;
}
};
// 监听鼠标移动事件实时更新左侧Panel的宽度
resizeThumb.PointerMoved += (sender, e) =>
{
if (_isResizing)
{
var currentX = e.GetPosition(this).X;
var delta = currentX - _dragStartX;
var updateLeft = _initialWidth + delta;
var updateRight = _initialRightWidth - delta;
MenuDockPanel.Width = updateLeft > 1 ? updateLeft : 1; // 更新左侧Panel的宽度
SecondMenuDockPanel.Width = updateRight > 1 ? updateRight : 1;
e.Handled = true;
}
};
resizeThumb1.PointerMoved += (sender, e) =>
{
if (_isResizing)
{
var currentX = e.GetPosition(this).X;
var delta = currentX - _dragStartX;
var updateLeft = _initialWidth + delta;
var updateRight = _initialRightWidth - delta;
SecondMenuDockPanel.Width = updateLeft > 1 ? updateLeft : 1; // 更新左侧Panel的宽度
FunctionDockPanel.Width = _initialRightWidth - delta; // updateRight > 1 ? updateRight : 1;
e.Handled = true;
}
};
// 监听鼠标释放事件,结束拖拽
resizeThumb.PointerReleased += (sender, e) => { _isResizing = false; };
resizeThumb1.PointerReleased += (sender, e) => { _isResizing = false; };
}
private void AddColumns()
{
// Grid myGrid = this.FindControl<Grid>("myGrid");
int count = 4;
for (int i = 0; i < 19; i++) // 添加 5 列
{
var hang = i / count;
var lie = i % count;
Log.Debug($"{hang} : {lie}");
if (this.myGrid.RowDefinitions.Count < hang + 1)
this.myGrid.RowDefinitions.Add(new RowDefinition(new GridLength(hang, GridUnitType.Auto)));
if (this.myGrid.ColumnDefinitions.Count < lie + 1)
myGrid.ColumnDefinitions.Add(new ColumnDefinition(new GridLength(lie, GridUnitType.Auto)));
Border border = new Border()
{
Width = 60,
Height = 40,
BorderBrush = Brushes.Black, // 设置边框颜色
BorderThickness = new Thickness(2), // 设置边框厚度
Padding = new Thickness(2), // 设置内部填充
Background = Brushes.LightGray, // 设置背景颜色
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
};
var textBlock = new TextBlock
{
Text = $"{hang} : {lie}",
HorizontalAlignment = HorizontalAlignment.Center,
Background = i % 2 == 0 ? Brushes.Red : Brushes.Green,
};
border.Child = textBlock;
this.myGrid.Children.Add(border);
Grid.SetRow(border, hang);
Grid.SetColumn(border, lie);
}
}
}