wpf 自定义 无边框 窗体 resize 实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wpf 自定义 无边框 窗体 resize 实现相关的知识,希望对你有一定的参考价值。

 参数定义

 1         class NativeMethods
 2         {
 3             public const int WM_NCHITTEST = 0x84;
 4             public const int HTCAPTION = 2;
 5             public const int HTLEFT = 10;
 6             public const int HTRIGHT = 11;
 7             public const int HTTOP = 12;
 8             public const int HTTOPLEFT = 13;
 9             public const int HTTOPRIGHT = 14;
10             public const int HTBOTTOM = 15;
11             public const int HTBOTTOMLEFT = 16;
12             public const int HTBOTTOMRIGHT = 17;
13         }

 

加hook

1 IntPtr windowHandle = new WindowInteropHelper(win).Handle;
2  HwndSource windowSource = HwndSource.FromHwnd(windowHandle);
3 windowSource.RemoveHook(WndProc);   

 

 1  private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
 2         {
 3             int GripSize = 16;
 4             int BorderSize = 7;
 5             Window win = (Window)System.Windows.Interop.HwndSource.FromHwnd(hwnd).RootVisual;
 6             if (msg == NativeMethods.WM_NCHITTEST)
 7             {
 8                 int x = lParam.ToInt32() << 16 >> 16, y = lParam.ToInt32() >> 16;
 9                 Point pos = win.PointFromScreen(new Point(x, y));
10 
11                 //bottom
12                 if (pos.X > GripSize &&
13                     pos.X < win.ActualWidth - GripSize &&
14                     pos.Y >= win.ActualHeight - BorderSize)
15                 {
16                     handled = true;
17                     return (IntPtr)NativeMethods.HTBOTTOM;
18                 }
19 
20                 //Right
21                 if (pos.Y > GripSize &&
22                     pos.X > win.ActualWidth - BorderSize &&
23                     pos.Y < win.ActualHeight - GripSize)
24                 {
25                     handled = true;
26                     return (IntPtr)NativeMethods.HTRIGHT;
27                 }
28 
29                 // Top, Left, Right, Corners, Etc.
30                 //HTBOTTOMRIGHT
31                 if (pos.X > win.ActualWidth - GripSize &&
32                    pos.Y >= win.ActualHeight - GripSize)
33                 {
34                     handled = true;
35                     return (IntPtr)NativeMethods.HTBOTTOMRIGHT;
36                 }
37             }
38 
39             return IntPtr.Zero;
40         }

 

以上是关于wpf 自定义 无边框 窗体 resize 实现的主要内容,如果未能解决你的问题,请参考以下文章

WPF一步步实现完全无边框自定义Window(附源码)

WPF中鼠标事件MouseLeftButtonDown,MouseLeftButtonUp实现无边框窗体的最大化,还原

【WPF】自定义标题栏

在WPF中,当在无边框窗口中拖动自定义的标题栏时,窗口会恢复,如何实现呢?

WPF无边框窗体怎么移动?C#

WPF无边框可拖动窗体