摘自:http://www.cnblogs.com/zhoujg/archive/2010/08/17/1801271.html参照博客园中的文章,现在写一个简单的自定义窗体:<Window x:Class="WPF熊俊.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF熊俊"
AllowsTransparency="True" WindowStyle="None" Background="Transparent"
Style="{DynamicResource Mainwindow}" WindowStartupLocation="CenterScreen" >
<Window.Resources>
<ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="Control">
<Grid>
<local:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 -4 0 0"
VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<local:ResizeThumb Width="3" Cursor="SizeWE" Margin="-4 0 0 0"
VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
<local:ResizeThumb Width="3" Cursor="SizeWE" Margin="0 0 -4 0"
VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
<local:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 0 0 -4"
VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
</Grid>
</ControlTemplate>
<Style x:Key="Mainwindow" TargetType="{x:Type Window}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Border BorderThickness="1" BorderBrush="Red" Background="#FFD4DEBF">
<Grid>
<Control Template="{StaticResource ResizeDecoratorTemplate}"/>
<ContentPresenter />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
</Window>public class ResizeThumb:Thumb
{
public ResizeThumb()
{
...
...
}
}
启动窗体后,鼠标移到四周,为什么只有左边、上边能看到变样的鼠标光标,而右边和下边始终看不到变样的光标呢?上、下、左、右的代码不都是一样的吗?折腾了好几天了,找不到原因
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF熊俊"
AllowsTransparency="True" WindowStyle="None" Background="Transparent"
Style="{DynamicResource Mainwindow}" WindowStartupLocation="CenterScreen" >
<Window.Resources>
<ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="Control">
<Grid>
<local:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 -4 0 0"
VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<local:ResizeThumb Width="3" Cursor="SizeWE" Margin="-4 0 0 0"
VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
<local:ResizeThumb Width="3" Cursor="SizeWE" Margin="0 0 -4 0"
VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
<local:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 0 0 -4"
VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
</Grid>
</ControlTemplate>
<Style x:Key="Mainwindow" TargetType="{x:Type Window}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Border BorderThickness="1" BorderBrush="Red" Background="#FFD4DEBF">
<Grid>
<Control Template="{StaticResource ResizeDecoratorTemplate}"/>
<ContentPresenter />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
</Window>public class ResizeThumb:Thumb
{
public ResizeThumb()
{
...
...
}
}
启动窗体后,鼠标移到四周,为什么只有左边、上边能看到变样的鼠标光标,而右边和下边始终看不到变样的光标呢?上、下、左、右的代码不都是一样的吗?折腾了好几天了,找不到原因
因为在wpf的HitTestCore中调用Rect.Contains(),而Rect.Contains长宽多算了一个像素。你的程序实际上已经把四个thumb都彻底移出了border(margin=-4),而上边和左边利用了这个bug/特性,所以鼠标有反应。正确的做法是不要把thumb移出border,如果不想看到thumb,可以给thumb重新做个template。
不想做template的话,可以在thumb所在的grid里加上这些资源: <Grid.Resources>
<Style TargetType="Thumb">
<Setter Property="Background" Value="Transparent" />
</Style>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlDarkDarkBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlDarkBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlLightLightBrushKey}" Color="Transparent"/>
</Grid.Resources>
其实改模板的话代码还少两行。
我测试了下,是不是这样的:鼠标水平往右拖动,e.HorizontalChange是正数,往左拖动,e.HorizontalChange就是负数。往上拖动,e.HorizontalChange是正数,往下拖动,e.HorizontalChange就是负数
是这样的吗?
我看了的,就是不太懂,所以问下