zoukankan      html  css  js  c++  java
  • WPF 窗口自定义拉伸

     .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         };
    Register user32.dll
     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
     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
    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

    XAML 属性 :MouseMove="ResizePressed" MouseDown="ResizePressed"

  • 相关阅读:
    完整的网站开发技术学习建议
    在微信小程序中绘制图表(part2)
    原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
    第八届蓝桥杯第二题:等差素数列
    第七届蓝桥杯第四题:快速排序
    51Nod:1086背包问题 V2
    POJ:2386 Lake Counting(dfs)
    51Nod:1268 和为K的组合
    迭代器
    51Nod:1134 最长递增子序列
  • 原文地址:https://www.cnblogs.com/gaobing/p/yuxuaner.html
Copyright © 2011-2022 走看看