gpt4 book ai didi

c# - WPF C# 实现调整 handle 大小

转载 作者:行者123 更新时间:2023-11-30 18:18:02 26 4
gpt4 key购买 nike

我想对我的工具实现调整大小的控制。我知道我可以通过设置 ResizeMode="CanResizeWithGrip" 来激活 handle ,但我想将 handle 置于网格中,这仅在悬停时可见。我用图像制作了一个网格

<Grid x:Name="gridResize" Background="Transparent" >
<Image x:Name="resizeGrip" HorizontalAlignment="Left"
Height="30"
VerticalAlignment="Top"
Width="27"
Source="UserControls/Images/sizegrip.png"/>
</Grid>

我必须在代码中处理这些事件:

private void ResizeGrip_MouseDown(object sender, MouseButtonEventArgs e)
{
throw new NotImplementedException();
}

private void GridResize_MouseLeave(object sender, MouseEventArgs e)
{
gridResize.Visibility = Visibility.Hidden;
}

private void GridResize_MouseEnter(object sender, MouseEventArgs e)
{
gridResize.Visibility = Visibility.Visible;
}

有谁知道如何实现鼠标按下方法?

最佳答案

这是一个简单的例子(注意我使用了边框而不是图像,但是改变它是直接的)

Xaml

 <Window x:Class="Test.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:Test"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
mc:Ignorable="d"
Title="MainWindow"
PreviewMouseMove="window_PreviewMouseMove">

<Grid>
<Grid Background="Red" x:Name="gridResize" Margin="236,235,0,0" >
<Border Focusable="True" Background="Black" BorderBrush="LightGray" BorderThickness="1" Opacity="0.2" x:Name="resizeGrip" HorizontalAlignment="Left" Height="30" VerticalAlignment="Top" Width="27"
PreviewMouseLeftButtonDown="resizeGrip_PreviewMouseLeftButtonDown" PreviewMouseLeftButtonUp="resizeGrip_PreviewMouseLeftButtonUp" >
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="1.0"/>
<Setter Property="Cursor" Value="SizeNWSE"/>
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</Grid>
</Grid>

</Window>

代码

public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}

Point _startPosition;
bool _isResizing = false;
private void resizeGrip_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (Mouse.Capture(resizeGrip))
{
_isResizing = true;
_startPosition = Mouse.GetPosition(this);
}
}

private void window_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (_isResizing)
{
Point currentPosition = Mouse.GetPosition(this);
double diffX = currentPosition.X - _startPosition.X;
double diffY = currentPosition.Y - _startPosition.Y;
double currentLeft = gridResize.Margin.Left;
double currentTop = gridResize.Margin.Top;
gridResize.Margin = new Thickness(currentLeft + diffX, currentTop + diffY, 0, 0);
_startPosition = currentPosition;
}
}

private void resizeGrip_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_isResizing == true)
{
_isResizing = false;
Mouse.Capture(null);
}

}


}

关于c# - WPF C# 实现调整 handle 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42338586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com