WPF自定義窗體改變大小


前台代碼:
<Window x:Class= " WpfResizeWindow.Window1 "
xmlns= " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
  xmlns:x= " http://schemas.microsoft.com/winfx/2006/xaml "
    Title= " Window1 " Height= " 300 " Width= " 300 " WindowStyle= " None " AllowsTransparency= " True ">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height= " 4 "/>
            <RowDefinition/>
            <RowDefinition Height= " 4 "/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width= " 4 "/>
            <ColumnDefinition/>
            <ColumnDefinition Width= " 4 "/>
        </Grid.ColumnDefinitions>

        <Rectangle Name= " ResizeTopLeft " Fill= " Black " Grid.Row= " 0 " Grid.Column= " 0 " MouseMove= " ResizePressed " MouseDown= " ResizePressed "/>
        <Rectangle Name= " ResizeTop " Fill= " Black " Grid.Row= " 0 " Grid.Column= " 1 " MouseMove= " ResizePressed " MouseDown= " ResizePressed "/>
        <Rectangle Name= " ResizeTopRight " Fill= " Black " Grid.Row= " 0 " Grid.Column= " 2 " MouseMove= " ResizePressed " MouseDown= " ResizePressed "/>
        <Rectangle Name= " ResizeLeft " Fill= " Black " Grid.Row= " 1 " Grid.Column= " 0 " MouseMove= " ResizePressed " MouseDown= " ResizePressed "/>
        <Rectangle Name= " ResizeRight " Fill= " Black " Grid.Row= " 1 " Grid.Column= " 3 " MouseMove= " ResizePressed " MouseDown= " ResizePressed "/>
        <Rectangle Name= " ResizeBottomLeft " Fill= " Black " Grid.Row= " 3 " Grid.Column= " 0 " MouseMove= " ResizePressed " MouseDown= " ResizePressed "/>
        <Rectangle Name= " ResizeBottom " Fill= " Black " Grid.Row= " 3 " Grid.Column= " 1 " MouseMove= " ResizePressed " MouseDown= " ResizePressed "/>
        <Rectangle Name= " ResizeBottomRight " Fill= " Black " Grid.Row= " 3 " Grid.Column= " 2 " MouseMove= " ResizePressed " MouseDown= " ResizePressed "/>
    </Grid>
</Window>

后台代碼: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace SimpleZigBee
{
     ///   <summary>
    
///  鼠標方向
    
///   </summary>
     public  enum ResizeDirection
    {
         ///   <summary>
        
///  左
        
///   </summary>
        Left =  1,
         ///   <summary>
        
///  右
        
///   </summary>
        Right =  2,
         ///   <summary>
        
///  上
        
///   </summary>
        Top =  3,
         ///   <summary>
        
///  左上
        
///   </summary>
        TopLeft =  4,
         ///   <summary>
        
///  右上
        
///   </summary>
        TopRight =  5,
         ///   <summary>
        
///  下
        
///   </summary>
        Bottom =  6,
         ///   <summary>
        
///  左下
        
///   </summary>
        BottomLeft =  7,
         ///   <summary>
        
///  右下
        
///   </summary>
        BottomRight =  8,
    }
     ///   <summary>
    
///  Window1.xaml 的交互邏輯
    
///   </summary>
     public  partial  class Window1 : Window
    {
         private HwndSource _HwndSource;
         private  const  int WM_SYSCOMMAND =  0x112;
        [DllImport( " user32.dll ", CharSet = CharSet.Auto)]
         private  static  extern IntPtr SendMessage(IntPtr hWnd,  uint Msg, IntPtr wParam, IntPtr lParam);

         private Dictionary<ResizeDirection, Cursor> cursors =  new Dictionary<ResizeDirection, Cursor>
        {
            {ResizeDirection.Top, Cursors.SizeNS},
            {ResizeDirection.Bottom, Cursors.SizeNS},
            {ResizeDirection.Left, Cursors.SizeWE},
            {ResizeDirection.Right, Cursors.SizeWE},
            {ResizeDirection.TopLeft, Cursors.SizeNWSE},
            {ResizeDirection.BottomRight, Cursors.SizeNWSE},
            {ResizeDirection.TopRight, Cursors.SizeNESW},
            {ResizeDirection.BottomLeft, Cursors.SizeNESW}
        };

         public Window1()
        {
            InitializeComponent();

             this.SourceInitialized +=  delegate( object sender, EventArgs e)
            {
                 this._HwndSource = PresentationSource.FromVisual((Visual)sender)  as HwndSource;
            };

             this.MouseMove +=  new MouseEventHandler(Window_MouseMove);
        }

         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);
        }
    }
}

另CodeProject中有已經封裝好的類庫(有相應源碼下載): http://www.codeproject.com/Articles/23138/WPF-Window-Resizing


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM