DragMove()将使与Cornerradius的边界失去其鼠标扳机状态?

发布于 2025-02-06 19:28:11 字数 2167 浏览 3 评论 0原文

我创建了一个带有圆角的无边界窗口,并在其中添加了阻力事件和扳机。这是简单的代码:

<Window x:Class="DebugTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DebugTest"
        mc:Ignorable="d" Height="200" Width="200"
        AllowsTransparency="True" WindowStyle="None" Background="Transparent">
    <Border x:Name="MainBorder" CornerRadius="15" Background="White" BorderBrush="Black" BorderThickness="1">
        <Grid>
            <Grid.Style>
                <Style TargetType="Grid">
                    <Setter Property="Visibility" Value="Hidden" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=MainBorder,Path=IsMouseOver}" Value="True">
                            <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Grid.Style>
            <Button Content="x" HorizontalAlignment="Right" VerticalAlignment="Top" 
                    Margin="5" Height="20" Width="20" Click="Button_Click"/>
        </Grid>
    </Border>
</Window>
        public MainWindow()
        {
            InitializeComponent();
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            this.DragMove();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

但是,当我运行EXE文件时,单击窗口内的空白区域时,该按钮将显而易见。

奇怪的是,这种情况几乎不会发生在Visual Studio中的调试而不是双击文件时,并且在Cornerradius =“ 0”时也不会发生这种情况。

它看起来像在单击时丢失了鼠标触发器,但是我想不出任何避免闪烁出现的好方法,并满足圆角 draggable 强>,并带有触发

I have created a borderless window with rounded corners, and added the drag event and a trigger to it. Here is the simple code:

<Window x:Class="DebugTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DebugTest"
        mc:Ignorable="d" Height="200" Width="200"
        AllowsTransparency="True" WindowStyle="None" Background="Transparent">
    <Border x:Name="MainBorder" CornerRadius="15" Background="White" BorderBrush="Black" BorderThickness="1">
        <Grid>
            <Grid.Style>
                <Style TargetType="Grid">
                    <Setter Property="Visibility" Value="Hidden" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=MainBorder,Path=IsMouseOver}" Value="True">
                            <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Grid.Style>
            <Button Content="x" HorizontalAlignment="Right" VerticalAlignment="Top" 
                    Margin="5" Height="20" Width="20" Click="Button_Click"/>
        </Grid>
    </Border>
</Window>
        public MainWindow()
        {
            InitializeComponent();
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            this.DragMove();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

But when I run the exe file, click on the blank area within the window, the button will appear very obvious flickering situation.

Strangely enough, this situation hardly occurs when debugging in Visual Studio instead of double click the file, and it also doesn't happen while CornerRadius="0".

It looks like it lost the mouseover trigger on click, but I can't think of any good way to avoid flicker appearing, and to satisfy the need for both with rounded corners, draggable, and with trigger.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2025-02-13 19:28:11

好吧,尽管我不知道为什么只有圆角会导致DragMove()触发Mouseleave事件,但我用背景代码绕过它,而不是使用XAML触发器。

    <Border x:Name="MainBorder" CornerRadius="15" Background="White" 
            BorderBrush="Black" BorderThickness="1"
            MouseEnter="MainBorder_MouseEnter" MouseLeave="MainBorder_MouseLeave">
        <Grid Visibility="Hidden" x:Name="TriggerBorder">
            <Button Content="x" HorizontalAlignment="Right" VerticalAlignment="Top" 
                    Margin="5" Height="20" Width="20" Click="Button_Click"/>
        </Grid>
    </Border>
        bool dragMoving = false;
        public MainWindow()
        {
            InitializeComponent();
        }        
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }       
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            dragMoving = true;
            this.DragMove();
            dragMoving = false;
        }
        private void MainBorder_MouseEnter(object sender, MouseEventArgs e)
        {
            TriggerBorder.Visibility = Visibility.Visible;
        }

        private void MainBorder_MouseLeave(object sender, MouseEventArgs e)
        {
            if (dragMoving) return;
            TriggerBorder.Visibility = Visibility.Hidden;
        }

似乎正常。

Well, although I don't know why only rounded corners would cause DragMove() to trigger the MouseLeave event, I circumvented this with background code instead of using xaml trigger.

    <Border x:Name="MainBorder" CornerRadius="15" Background="White" 
            BorderBrush="Black" BorderThickness="1"
            MouseEnter="MainBorder_MouseEnter" MouseLeave="MainBorder_MouseLeave">
        <Grid Visibility="Hidden" x:Name="TriggerBorder">
            <Button Content="x" HorizontalAlignment="Right" VerticalAlignment="Top" 
                    Margin="5" Height="20" Width="20" Click="Button_Click"/>
        </Grid>
    </Border>
        bool dragMoving = false;
        public MainWindow()
        {
            InitializeComponent();
        }        
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }       
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            dragMoving = true;
            this.DragMove();
            dragMoving = false;
        }
        private void MainBorder_MouseEnter(object sender, MouseEventArgs e)
        {
            TriggerBorder.Visibility = Visibility.Visible;
        }

        private void MainBorder_MouseLeave(object sender, MouseEventArgs e)
        {
            if (dragMoving) return;
            TriggerBorder.Visibility = Visibility.Hidden;
        }

Seems to work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文