效果圖:(右下角拖拽改變窗體大小)
第一步:添加xaml代碼:
<Border Name="ResizeBottomRight" MouseMove="ResizePressed"
MouseDown="ResizePressed" Height="15" Width="15"
HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,3,3" RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="88.182"/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
<Border.Background>
<DrawingBrush>
<DrawingBrush.Drawing>
<GeometryDrawing Geometry="M96,128 L896,928 96,128 z M352,384 L128,160 172.19048,160 352,160 352,384 z M608,640 L384,416 608,416 608,640 z M608,384 L384,384 384,160 608,160 608,384 z M864,896 L640,672 864,672 864,896 z M864,640 L640,640 640,416 864,416 864,640 z M864,384 L640,384 640,160 864,160 864,384 z" Brush="#888888"></GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Border.Background>
</Border>
第二步:xaml.cs後台代碼 聲明
#region 初始化窗體可以縮放大小
private const int WM_SYSCOMMAND = 0x112;
private HwndSource _HwndSource;
private Dictionary<ResizeDirection, Cursor> cursors = new Dictionary<ResizeDirection, Cursor>
{
{ResizeDirection.BottomRight, Cursors.SizeNWSE},
};
private enum ResizeDirection
{
BottomRight = 8,
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
#endregion
事件:
this.SourceInitialized += delegate(object sender, EventArgs e)//執行拖拽 { this._HwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource; }; this.MouseMove += new MouseEventHandler(Window_MouseMove);//鼠標移入到邊緣收縮 this.Loaded += new RoutedEventHandler(FluidWrapPanelDemo_Loaded);
private void Window_MouseMove(object sender, MouseEventArgs e)
{
if (Mouse.LeftButton != MouseButtonState.Pressed)
{
FrameworkElement element = e.OriginalSource as FrameworkElement;
if (element != null && !element.Name.Contains("Resize"))
this.Cursor = Cursors.Arrow;
}
}
private void ResizePressed(object sender, MouseEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
ResizeDirection direction = (ResizeDirection)Enum.Parse(typeof(ResizeDirection), element.Name.Replace("Resize", ""));
this.Cursor = cursors[direction];
if (e.LeftButton == MouseButtonState.Pressed)
ResizeWindow(direction);
}
private void ResizeWindow(ResizeDirection direction)
{
SendMessage(_HwndSource.Handle, WM_SYSCOMMAND, (IntPtr)(61440 + direction), IntPtr.Zero);
}
下次更新(菜單縮放):效果圖
轉載請注明地址~謝謝 謝謝大家關注