不使用 WinForms 旋转光标

Rotating cursor without using WinForms

是否可以旋转FrameworkElement.Cursor

我的应用程序允许用户围绕其中心旋转对象。旋转后,默认的调整大小光标在倾斜的边框顶部显得笨拙。

我的第一个想法是将 RotateTransform 应用于 Cursor 属性,但在 XAML 中似乎无法做到这一点。接下来我尝试从 Cursor class 继承,但看起来 MS 家伙有 sealed 它。

另一种方法是将默认光标设置为 None 并使用我自己的图像(带有变换)并将其位置设置在 MouseMove 上。如果有更简单的选择,我不愿意走那条路。谁有好的建议?

如果可能的话,我正在寻找仅限 WPF 的解决方案。

最终在 WPF 范围内管理它,而不使用 WinForms 或 PInvokes。我没有即时创建自定义游标 (*.cur) 或将 Visuals 转换为游标,而是使用父控件的 MouseMove 事件和 WPF 元素 (Path) 作为我的光标。以下是万一有人感兴趣的方法:

  1. 将调整大小拇指(或您用作形状边框的任何东西)的 Cursor 设置为 None,以便 WPF 不显示默认箭头。
  2. 创建您自己的游标。可以是任何 FrameworkElement,但我使用 Path 是因为它易于操作以创建您想要的任何形状。请注意,我在下面设置的大部分属性都很重要。

    <Path x:Name="PART_EW" Data="M0,20 L25,0 25,15 75,15 75,0 100,20 75,40 75,25 25,25 25,40z" Fill="White" Stroke="Black" StrokeThickness="1" Visibility="Collapsed" Width="50" Height="20" Opacity=".7" Stretch="Fill" Panel.ZIndex="100001" HorizontalAlignment="Left" VerticalAlignment="Top" IsHitTestVisible="False" />

在调整大小的拇指中添加以下代码:

protected override void OnMouseEnter(MouseEventArgs e)
{
  base.OnMouseEnter(e);

  var Pos = e.GetPosition(this);
  PART_EW.Margin = new Thickness(
                       Pos.X - PART_EW.Width / 2, 
                       Pos.Y - PART_EW.Height / 2, 
                       -PART_EW.Width, 
                       -PART_EW.Height);
  PART_EW.Visibility = Visibility.Visible;
}

protected override void OnMouseLeave(MouseEventArgs e)
{
  base.OnMouseLeave(e);
  PART_EW.Visibility = Visibility.Collapsed;
}

protected override void OnMouseMove(MouseEventArgs e)
{
  base.OnMouseMove(e);

  var Pos = e.GetPosition(designerItem);
  PART_EW.Margin = new Thickness(
                       Pos.X - PART_EW.Width / 2, 
                       Pos.Y - PART_EW.Height / 2, 
                       -PART_EW.Width, 
                       -PART_EW.Height);
}

请注意,我没有在代码的任何地方设置 PathRotateTransform,因为它已经是调整大小滑块的一部分,因此会自动获取父控件的角度。

希望这对以后的人有所帮助。