using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace DrillTools { /// /// 拖动提示窗口,显示刀具信息 /// public class DragToolTipWindow : Window { private readonly TextBlock _textBlock; public DragToolTipWindow() { // 设置窗口样式 WindowStyle = WindowStyle.None; AllowsTransparency = true; Background = System.Windows.Media.Brushes.Transparent; ShowInTaskbar = false; Topmost = true; IsHitTestVisible = false; // 不拦截鼠标事件 SizeToContent = SizeToContent.WidthAndHeight; // 根据内容自动调整大小 MaxWidth = 200; // 限制最大宽度 MaxHeight = 50; // 限制最大高度 // 创建内容容器 var border = new Border { Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(224, 255, 255, 255)), BorderBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 120, 212)), BorderThickness = new Thickness(1), CornerRadius = new CornerRadius(3), Padding = new Thickness(5, 2, 5, 2), Effect = new System.Windows.Media.Effects.DropShadowEffect { Color = System.Windows.Media.Colors.Black, Direction = 315, ShadowDepth = 2, BlurRadius = 5, Opacity = 0.3 } }; // 创建文本显示 _textBlock = new TextBlock { FontWeight = FontWeights.Bold, FontSize = 10, // 减小字体大小 TextTrimming = TextTrimming.CharacterEllipsis // 文本过长时显示省略号 }; border.Child = _textBlock; Content = border; } /// /// 设置刀具信息 /// /// 刀具项 public void SetToolInfo(ToolItem tool) { _textBlock.Text = $"T{tool.ToolNumber:D2} - {tool.Diameter:F3}mm"; } /// /// 更新窗口位置 /// /// 鼠标位置 public void UpdatePosition(System.Windows.Point position) { // 将提示窗口显示在鼠标右下方,稍微偏移以避免遮挡 // 确保窗口不会超出屏幕边界 double left = position.X + 15; double top = position.Y - 35; // 获取屏幕尺寸 var screenWidth = SystemParameters.PrimaryScreenWidth; var screenHeight = SystemParameters.PrimaryScreenHeight; // 先设置位置,然后获取实际尺寸 Left = left; Top = top; // 确保窗口不会超出右边界 if (left + ActualWidth > screenWidth) { left = position.X - ActualWidth - 15; } // 确保窗口不会超出左边界 if (left < 0) { left = 5; } // 确保窗口不会超出下边界 if (top + ActualHeight > screenHeight) { top = position.Y - ActualHeight - 5; } // 确保窗口不会超出上边界 if (top < 0) { top = position.Y + 15; } // 重新设置最终位置 Left = left; Top = top; } } }