码迷,mamicode.com
首页 > 其他好文 > 详细

WPF 窗口自定义拉伸

时间:2014-06-27 19:33:57      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   tar   

使用win32

 .NET技术交流群 199281001 .欢迎加入。

bubuko.com,布布扣
 1   //自定义窗体拉伸
 2 
 3         public HwndSource _HwndkaifaSource;
 4         private const int WM_SYSCOMMAND = 0x112;
 5         [DllImport("user32.dll", CharSet = CharSet.Auto)]
 6         private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
 7 
 8         private Dictionary<ResizeDirection, Cursor> cursors = new Dictionary<ResizeDirection, Cursor>
 9         {
10             {ResizeDirection.Top, Cursors.SizeNS},
11             {ResizeDirection.Bottom, Cursors.SizeNS},
12             {ResizeDirection.Left, Cursors.SizeWE},
13             {ResizeDirection.Right, Cursors.SizeWE},
14             {ResizeDirection.TopLeft, Cursors.SizeNWSE},
15             {ResizeDirection.BottomRight, Cursors.SizeNWSE},
16             {ResizeDirection.TopRight, Cursors.SizeNESW},
17             {ResizeDirection.BottomLeft, Cursors.SizeNESW}
18         };
Register user32.dll

 

bubuko.com,布布扣
 1 public enum ResizeDirection
 2         {
 3             /// <summary>
 4             /// 5             /// </summary>
 6             Left = 1,
 7             /// <summary>
 8             /// 9             /// </summary>
10             Right = 2,
11             /// <summary>
12             ///13             /// </summary>
14             Top = 3,
15             /// <summary>
16             /// 左上
17             /// </summary>
18             TopLeft = 4,
19             /// <summary>
20             /// 右上
21             /// </summary>
22             TopRight = 5,
23             /// <summary>
24             ///25             /// </summary>
26             Bottom = 6,
27             /// <summary>
28             /// 左下
29             /// </summary>
30             BottomLeft = 7,
31             /// <summary>
32             /// 右下
33             /// </summary>
34             BottomRight = 8,
35         }
ResizeDirection

 

bubuko.com,布布扣
 1         void Window_MouseMove(object sender, MouseEventArgs e)
 2         {
 3 
 4             if (Mouse.LeftButton != MouseButtonState.Pressed)
 5             {
 6                 FrameworkElement element = e.OriginalSource as FrameworkElement;
 7                 if (element != null && !element.Name.Contains("Resize")) this.Cursor = Cursors.Arrow;
 8             }
 9 
10         }
11 
12         private void ResizePressed(object sender, MouseEventArgs e)
13         {
14 
15             FrameworkElement element = sender as FrameworkElement;
16             ResizeDirection direction = (ResizeDirection)Enum.Parse(typeof(ResizeDirection), element.Name.Replace("Resize", ""));
17 
18             this.Cursor = cursors[direction];
19             if (e.LeftButton == MouseButtonState.Pressed) ResizeWindow(direction);
20 
21         }
22 
23         private void ResizeWindow(ResizeDirection direction)
24         {
25 
26             SendMessage(_HwndkaifaSource.Handle, WM_SYSCOMMAND, (IntPtr)(61440 + direction), IntPtr.Zero);
27 
28         }
Event

 

bubuko.com,布布扣
1  this.SourceInitialized += delegate(object sender, EventArgs e)
2  {
3       this._HwndkaifaSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
4 };
5          this.MouseMove += new MouseEventHandler(Window_MouseMove);
construction

 

 

WPF 窗口自定义拉伸,布布扣,bubuko.com

WPF 窗口自定义拉伸

标签:style   class   blog   code   http   tar   

原文地址:http://www.cnblogs.com/gaobing/p/3809508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!