WPF: Make simple highlight on mouse

隐身守侯 提交于 2021-02-19 08:33:10

问题


I try to write code to make simple highlight on mouse I make a full window and making it transparent it's working but i can't click through i searched for solution i find This Solution but the eclipse doesn't move and whole events doesn't work

XMAL Code:

<Window  x:Class="ChangeMousePosition.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Topmost="True" AllowsTransparency="True"  WindowStyle="None" WindowState="Maximized" >
    <Window.Background>
        <SolidColorBrush Color="Black" Opacity=".1"></SolidColorBrush>
    </Window.Background>
        <!--<Image x:Name="ima" Height="285" Canvas.Left="10" Canvas.Top="24" Width="285" Drop="FFFFF" AllowDrop="False"/>-->
        <Canvas  x:Name="board" MouseMove="Move" Background="Transparent" >
            <!--<Button Content="Button" Height="83" Canvas.Left="244" Canvas.Top="89" Width="168" PreviewMouseDown="Button_MouseDown_1" PreviewMouseUp="Button_PreviewMouseUp" Click="Button_Click"/>-->
            <Path x:Name="NO" Fill="#FFF50D0D" Data="M22.860409,82.622946 L23.007377,83.346104 C25.813204,96.355545 33.601627,108.39023 45.707599,116.36658 69.919536,132.31929 102.2331,125.99773 117.88195,102.24696 121.79416,96.309273 124.29446,89.844378 125.46598,83.237726 L125.56714,82.622946 z M73.026829,21.552124 C56.27346,21.596912 39.98328,29.632862 30.202747,44.477087 27.024074,49.30146 24.777485,54.473871 23.41839,59.787574 L23.178018,60.778945 124.44506,60.778945 124.15735,59.747282 C120.77321,48.147671 113.34813,37.586039 102.37709,30.357466 93.297619,24.375198 83.078853,21.525253 73.026829,21.552124 z M71.968075,0.0010070801 C86.064632,-0.072628021 100.38476,3.8962669 113.09603,12.271439 146.99274,34.60523 156.61267,79.81521 134.58275,113.25075 112.55281,146.68629 67.215297,155.68603 33.318584,133.35223 -0.57813454,111.01844 -10.198063,65.808459 11.831869,32.372923 25.600578,11.475712 48.473808,0.12373352 71.968075,0.0010070801 z" Height="145.624" RenderTransformOrigin="0.495907960139618,0.492371499077968" Stretch="Fill" Width="146.415" Canvas.Left="252" Canvas.Top="63.188" Visibility="Hidden">
                <Path.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform/>
                        <RotateTransform Angle="-33.38"/>
                        <TranslateTransform/>
                    </TransformGroup>
                </Path.RenderTransform>
            </Path>
            <Ellipse x:Name="mark" Height="29" Canvas.Left="81" Canvas.Top="58" Width="33"  IsHitTestVisible="False" >
                <Ellipse.Fill>
                    <RadialGradientBrush>
                        <GradientStop Color="Black" Offset="0"/>
                        <GradientStop Offset="1"/>
                    </RadialGradientBrush>
                </Ellipse.Fill>
            </Ellipse>

        </Canvas>

</Window>

C# Code:

public static class WindowsServices
{
          const int WS_EX_TRANSPARENT = 0x00000020;
          const int GWL_EXSTYLE = (-20);

         [DllImport("user32.dll")]
         static extern int GetWindowLong(IntPtr hwnd, int index);

        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);

         public static void SetWindowExTransparent(IntPtr hwnd)
         {
             var extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
             SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
         }
  }
public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
    }

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        var hwnd = new WindowInteropHelper(this).Handle;
       WindowsServices.SetWindowExTransparent(hwnd);

    }


    private void Move(object sender, MouseEventArgs e)
    {
        Point p = e.GetPosition(board);
        Debug.WriteLine(p);
        Rect MyRect = new Rect();
        MyRect.X = p.X - mark.ActualWidth / 2;
        MyRect.Y = p.Y - mark.ActualHeight / 2;
        MyRect.Width = mark.ActualWidth;
        MyRect.Height = mark.ActualHeight;
        mark.Arrange(MyRect);

    }

}

回答1:


I have done very similar in Material Design in XAML Toolkit.

I have a VisualFeedbackContentControl content control.

This overrides OnMouseMove and sets it's own dependency properties:

private void OnMouseMove(object sender, MouseEventArgs mouseEventArgs)
{
    var position = mouseEventArgs.GetPosition(this);
    MouseX = position.X;
    MouseY = position.Y;
}

And here is a cut down snippet of the XAML/Template for the control. I display the ellipse in a canvas underneath the content (but as IsHitTestIsVisible=False you can move it above the ContentPresenter)

<ControlTemplate TargetType="{x:Type local:VisualFeedbackContentControl}">
    <Grid ClipToBounds="True" Background="Transparent">
        <Canvas IsHitTestVisible="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Ellipse Opacity="0.3" Width="60" Height="60" Canvas.Left="{TemplateBinding MouseX}" Canvas.Top="{TemplateBinding MouseY}"                                     
                     x:Name="HoverEllipse" Fill="{TemplateBinding Foreground, Converter={StaticResource BrushToRadialGradientBrushConverter}}">
            </Ellipse>
        </Canvas>
        <ContentPresenter Content="{TemplateBinding Content}"
                          ContentTemplate="{TemplateBinding ContentTemplate}"
                          Cursor="{TemplateBinding Cursor}"
                          Margin="{TemplateBinding Padding}"
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
    </Grid>   
</ControlTemplate>


来源:https://stackoverflow.com/questions/29954736/wpf-make-simple-highlight-on-mouse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!