首页 > 解决方案 > 无法检测到右键单击鼠标向下和向上事件

问题描述

我在Windows 7下写了一个WPF软件VS 2019。我想实现这个功能:当我在画布上右击鼠标并在画布上画一条线时,会画一条线,从我右击a的点开始鼠标并在我释放右键时结束。

我尝试了很多方法,但似乎程序无法检测到右键单击事件。

<UserControl x:Class="STIGenericReport.UserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="407" Width="633" Background="White" BorderBrush="White" Focusable="False">
    <Grid Name="maingrid" Background="White" Height="405" Width="616" MinWidth="500">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="619*" />
            <ColumnDefinition Width="10*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height ="574*" />
            <RowDefinition Height="26" MinHeight="16" />
        </Grid.RowDefinitions>
    <Viewport3D Name="mainViewport" ClipToBounds = "False"  
                IsHitTestVisible="false"
                RenderOptions.EdgeMode="Aliased" Margin="12,52,144,47">
        <Viewport3D.Camera>
            <OrthographicCamera x:Name="camera" Width="2"
                FarPlaneDistance="10"
                NearPlaneDistance="1" 
                LookDirection="0,0,-1"
                UpDirection="0,1,0"
                Position="0,0,2" />
        </Viewport3D.Camera>
        <Viewport3D.Children>
            <ModelVisual3D x:Name="Light1">
                <ModelVisual3D.Content>
                    <DirectionalLight Color="White" Direction="1, 1, -1" />
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D x:Name="Light2">
                <ModelVisual3D.Content>
                    <DirectionalLight Color="White" Direction="-1, 1, -1" />
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D x:Name="Light3">
                <ModelVisual3D.Content>
                    <DirectionalLight Color="White" Direction="0,-1,-0.5" />
                </ModelVisual3D.Content>
            </ModelVisual3D>
        </Viewport3D.Children>
     </Viewport3D>

    <Canvas x:Name="canvasOn3D" Background="#00E6FFFF" 
            Margin="20,52,102,18"></Canvas>
    <Label Content="Trayinfo    &amp;" FontSize="16" FontWeight="Bold" Height="34" HorizontalAlignment="Left" Margin="192,0,0,0" Name="lblTrayinfo" VerticalAlignment="Top" Width="194" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,257" Name="btn9" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,232" Name="btn8" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button HorizontalAlignment="Left" Margin="499,149,0,207" Name="btn7" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,182" Name="btn6" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,157" Name="btn5" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,132" Name="btn4" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,107" Name="btn3" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,82" Name="btn2" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,57" Name="btn1" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,282" Name="btn10" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,57" Name="lbl1" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,82" Name="lbl2" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,107" Name="lbl3" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,132" Name="lbl4" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,157" Name="lbl5" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,182" Name="lbl6" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,207" Name="lbl7" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,232" Name="lbl8" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,257" Name="lbl9" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,282" Name="lbl10" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Content="Pacakage info" HorizontalAlignment="Center" Width="Auto" Height="32" Name="lblPkgname" FontSize="16" FontWeight="Bold" Margin="300,0,191,347" />
</Grid>
    private void OnPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs args)     
    {
        if (args.RightButton == MouseButtonState.Pressed)                
        {
            currentPoint = args.GetPosition(this);
        }
    }
    private void OnPreviewMouseRightButtonUp(object sender, MouseButtonEventArgs args)      
    {
        if (args.RightButton == MouseButtonState.Released)                  
        {      
            Line line = new Line();
            line.Stroke = SystemColors.WindowFrameBrush;
            line.X1 = currentPoint.X;
            line.Y1 = currentPoint.Y;
            line.X2 = currentPoint_2.X;
            line.Y2 = currentPoint_2.Y;
            line.StrokeThickness = 2;
            canvasOn3D.Children.Add(line);
         }
    }

我调试这个程序的时候,鼠标右键没有进入这个函数。

标签: c#wpf

解决方案


即使在一个简单的实验中,Previewmouserightbuttondown 似乎也不起作用。

您的代码中也有一些奇怪的地方。

鼠标的坐标应该基于画布(大概)。

这是一些更简单的工作标记和代码。

在主窗口中,我刚刚在默认网格中添加了一个画布:

    <Grid>
        <Canvas 
            Name="lineCanvas"
            Background="Transparent"
            PreviewMouseDown="lineCanvas_PreviewMouseDown"
            PreviewMouseUp="lineCanvas_PreviewMouseUp"
                >
        
        </Canvas>
    </Grid>

注意我处理 previewmousedown 和 up 。画布的背景设置为透明,因此可以进行命中测试。

我的代码(正在运行):

    public MainWindow()
    {
        InitializeComponent();
    }
    Point startPoint = new Point();

    private void lineCanvas_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Right)
        {
            startPoint = Mouse.GetPosition(lineCanvas);
        }
    }

    private void lineCanvas_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
        if (startPoint.X == 0 && startPoint.Y == 0)
        {
            return;
        }
        var currentPoint = Mouse.GetPosition(lineCanvas);
        Line line = new Line();
        line.Stroke = SystemColors.WindowFrameBrush;
        line.X1 = startPoint.X;
        line.Y1 = startPoint.Y;
        line.X2 = currentPoint.X;
        line.Y2 = currentPoint.Y;
        line.StrokeThickness = 2;
        lineCanvas.Children.Add(line);

        startPoint = new Point();
    }

请注意,在我们的地图编辑器中,我执行类似的绘制直线的过程,我添加了一条“橡皮筋”临时线,它跟随鼠标轮显示线的结束位置。这再次向用户保证某事正在发生。您可能需要考虑这种方法。


推荐阅读