简体   繁体   English

不使用WinForms旋转光标

[英]Rotating cursor without using WinForms

Is it possible to rotate FrameworkElement.Cursor ? 是否可以旋转FrameworkElement.Cursor

My application allows user to rotate objects around their center. 我的应用程序允许用户围绕其中心旋转对象。 Once rotated, default resize cursors appear awkward on top of tilted borders. 旋转后,默认调整大小的光标在倾斜边框上显得很笨拙。

鼠标光标不受变换影响

My first thought was to apply RotateTransform to the Cursor property, but it looks like we can't do that in XAML. 我的第一个想法是将RotateTransform应用于Cursor属性,但看起来我们不能在XAML中这样做。 Next I tried inheriting from Cursor class, but it looks like MS guys have sealed it. 接下来我尝试从Cursor类继承,但看起来MS的人已经sealed它。

Another way is to set default cursor to None and use my own image (with transform) and set its position upon MouseMove . 另一种方法是将默认光标设置为None并使用我自己的图像(使用变换)并在MouseMove设置其位置。 I'm not willing to go down that path if there are easier alternatives. 如果有更容易的选择,我不愿意走这条路。 Anyone with a good suggestion? 谁有好的建议?

I'm looking for a WPF-only solution if at all possible. 如果可能的话,我正在寻找一个仅支持WPF的解决方案。

Finally managed it within the bounds of WPF, without using WinForms or PInvokes. 最后在WPF的范围内管理它,而不使用WinForms或PInvokes。 Instead of creating custom cursors (*.cur) on the fly or converting Visual s into cursors, I used MouseMove event of the parent control along with a WPF element ( Path ) as my cursor. 我没有动态创建自定义游标(* .cur)或将Visual转换为游标,而是使用父控件的MouseMove事件和WPF元素( Path )作为我的光标。 Here's the way just in case anyone is interesed: 这是以防万一有人互动的方式:

  1. Set the Cursor of your resize thumb (or whatever you're using as the border of your shape) to None , so that WPF doesn't display default arrow. 将调整大小拇指的Cursor (或您正在使用的任何形状的边框)设置为None ,以便WPF不显示默认箭头。
  2. Create your own cursor. 创建自己的游标。 Could be any FrameworkElement , but I have used Path for its easy manipulation to create any shape you want. 可以是任何FrameworkElement ,但我使用Path轻松操作来创建你想要的任何形状。 Note that most of the properties I have set below are important. 请注意,我在下面设置的大多数属性都很重要。

    <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" />

Add the following code in your resize thumb: 在resize thumb中添加以下代码:

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);
}

Note that I've not set RotateTransform of my Path anywhere in the code, since it is already part of the resize thumb and therefore acquires the angle of the parent control automatically. 请注意,我没有在代码中的任何位置设置Path RotateTransform ,因为它已经是resize thumb的一部分,因此自动获取父控件的角度。

Hope this helps people down the road. 希望这能帮助人们走上正轨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM