首页 > 代码库 > WPF 窗口自定义拉伸
WPF 窗口自定义拉伸
使用win32
.NET技术交流群 199281001 .欢迎加入。
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 };
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 }
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 }
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);
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。