弹来弹去跑马灯!

WPF 无边框窗体改变大小和移动

WIN32 API:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
private const int WM_NCHITTEST = 0x0084;
     private readonly int agWidth = 12; //拐角宽度
     private readonly int bThickness = 4; // 边框宽度
     private Point mousePoint = new Point(); //鼠标坐标
     public enum HitTest : int
     {
         HTERROR = -2,
         HTTRANSPARENT = -1,
         HTNOWHERE = 0,
         HTCLIENT = 1,
         HTCAPTION = 2,
         HTSYSMENU = 3,
         HTGROWBOX = 4,
         HTSIZE = HTGROWBOX,
         HTMENU = 5,
         HTHSCROLL = 6,
         HTVSCROLL = 7,
         HTMINBUTTON = 8,
         HTMAXBUTTON = 9,
         HTLEFT = 10,
         HTRIGHT = 11,
         HTTOP = 12,
         HTTOPLEFT = 13,
         HTTOPRIGHT = 14,
         HTBOTTOM = 15,
         HTBOTTOMLEFT = 16,
         HTBOTTOMRIGHT = 17,
         HTBORDER = 18,
         HTREDUCE = HTMINBUTTON,
         HTZOOM = HTMAXBUTTON,
         HTSIZEFIRST = HTLEFT,
         HTSIZELAST = HTBOTTOMRIGHT,
         HTOBJECT = 19,
         HTCLOSE = 20,
         HTHELP = 21,
     }
 
     protected override void OnSourceInitialized(EventArgs e)
     {
         base.OnSourceInitialized(e);
         HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
         if (hwndSource != null)
         {
             hwndSource.AddHook(new HwndSourceHook(this.WndProc));
         }
     }
     protected virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
     {
         switch (msg)
         {
             case WM_NCHITTEST:
                 this.mousePoint.X = (lParam.ToInt32() & 0xFFFF);
                 this.mousePoint.Y = (lParam.ToInt32() >> 16);
 
 
                 #region 测试鼠标位置
 
                 // 窗口左上角
                 if (this.mousePoint.Y - this.Top <= this.agWidth
                    && this.mousePoint.X - this.Left <= this.agWidth)
                 {
                     handled = true;
                     return new IntPtr((int)HitTest.HTTOPLEFT);
                 }
                 // 窗口左下角   
                 else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.agWidth
                    && this.mousePoint.X - this.Left <= this.agWidth)
                 {
                     handled = true;
                     return new IntPtr((int)HitTest.HTBOTTOMLEFT);
                 }
                 // 窗口右上角
                 else if (this.mousePoint.Y - this.Top <= this.agWidth
                    && this.ActualWidth + this.Left - this.mousePoint.X <= this.agWidth)
                 {
                     handled = true;
                     return new IntPtr((int)HitTest.HTTOPRIGHT);
                 }
                 // 窗口右下角
                 else if (this.ActualWidth + this.Left - this.mousePoint.X <= this.agWidth
                    && this.ActualHeight + this.Top - this.mousePoint.Y <= this.agWidth)
                 {
                     handled = true;
                     return new IntPtr((int)HitTest.HTBOTTOMRIGHT);
                 }
                 // 窗口左侧
                 else if (this.mousePoint.X - this.Left <= this.bThickness)
                 {
                     handled = true;
                     return new IntPtr((int)HitTest.HTLEFT);
                 }
                 // 窗口右侧
                 else if (this.ActualWidth + this.Left - this.mousePoint.X <= this.bThickness)
                 {
                     handled = true;
                     return new IntPtr((int)HitTest.HTRIGHT);
                 }
                 // 窗口上方
                 else if (this.mousePoint.Y - this.Top <= this.bThickness)
                 {
                     handled = true;
                     return new IntPtr((int)HitTest.HTTOP);
                 }
                 // 窗口下方
                 else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.bThickness)
                 {
                     handled = true;
                     return new IntPtr((int)HitTest.HTBOTTOM);
                 }
                 else // 窗口移动
                 {
                     handled = true;
                     return new IntPtr((int)HitTest.HTCAPTION);
                 }
                 #endregion
         }
         return IntPtr.Zero;
     }

  

posted @   wgscd  阅读(254)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示