Rotate the cursor without using WinForms

Is it possible to rotate FrameworkElement.Cursor ?

My application allows the user to rotate objects around their center. After rotation, the default resize pointers look uncomfortable over sloping borders.

Mouse cursor not affected by conversion

My first thought was to apply RotateTransform to the Cursor property, but it looks like we can't do this in XAML. Then I tried to inherit the Cursor class, but it looks like the MS guys have sealed it.

Another way is to set the default cursor to None and use my own image (with conversion) and set its position to MouseMove . I do not want to go this way if there are simpler alternatives. Anyone with a good suggestion?

I am only looking for a WPF solution, if at all possible.

+5
source share
1 answer

Finally, he managed it under WPF without using WinForms or PInvokes. Instead of creating custom cursors (* .cur) on the fly or converting Visual to cursors, I used the MouseMove event of the parent control along with the WPF ( Path ) element as my cursor. Here's the way, just in case anyone is interested:

  • Set the Cursor your large size (or what you use as the border of your figure) to None so that WPF does not display the default arrow.
  • Create your own cursor. It can be any FrameworkElement , but I used Path to easily process it to create whatever shape you want. Please note that most of the properties that I 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 to your large size:

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

Please note that I did not set the RotateTransform my Path anywhere in the code, as it is already part of a large size and therefore automatically gets the angle of the parent control.

Hope this helps people in the future.

0
source

All Articles