欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

实现方式一:

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
const int HTLEFT = 10;
        const int HTRIGHT = 11;
        const int HTTOP = 12;
        const int HTTOPLEFT = 13;
        const int HTTOPRIGHT = 14;
        const int HTBOTTOM = 15;
        const int HTBOTTOMLEFT = 0x10;
        const int HTBOTTOMRIGHT = 17;
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x0084:
                    base.WndProc(ref m);
                    Point vPoint = new Point((int)m.LParam & 0xFFFF,
                        (int)m.LParam >> 16 & 0xFFFF);
                    vPoint = PointToClient(vPoint);
                    if (vPoint.X <= 5)
                        if (vPoint.Y <= 5)
                            m.Result = (IntPtr)HTTOPLEFT;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr)HTBOTTOMLEFT;
                        else m.Result = (IntPtr)HTLEFT;
                    else if (vPoint.X >= ClientSize.Width - 5)
                        if (vPoint.Y <= 5)
                            m.Result = (IntPtr)HTTOPRIGHT;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr)HTBOTTOMRIGHT;
                        else m.Result = (IntPtr)HTRIGHT;
                    else if (vPoint.Y <= 5)
                        m.Result = (IntPtr)HTTOP;
                    else if (vPoint.Y >= ClientSize.Height - 5)
                        m.Result = (IntPtr)HTBOTTOM;
                    break;
                case 0x0201://鼠标左键按下的消息
                    m.Msg = 0x00A1;//更改消息为非客户区按下鼠标
                    m.LParam = IntPtr.Zero;//默认值
                    m.WParam = new IntPtr(2);//鼠标放在标题栏内
                    base.WndProc(ref m);
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

实现方式二:

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
#region 更改窗体大小
       private bool isMouseDown = false; //表示鼠标当前是否处于按下状态,初始值为否
       private MouseDirection direction = MouseDirection.None;//表示拖动的方向,起始为None,表示不拖动
       private void QueueForm_MouseDown(object sender, MouseEventArgs e)
       {
           isMouseDown = true;//鼠标按下
       }
 
       private void QueueForm_MouseUp(object sender, MouseEventArgs e)
       {
           isMouseDown = false;// 鼠标弹起,
           direction = MouseDirection.None; //既然鼠标弹起了,那么就不能再改变窗体尺寸,拖拽方向置 none
       }
 
       private void QueueForm_MouseMove(object sender, MouseEventArgs e)
       {
           //鼠标移动过程中,坐标时刻在改变
           //当鼠标移动时横坐标距离窗体右边缘5像素以内且纵坐标距离下边缘也在5像素以内时,要将光标变为倾斜的箭头形状,同时拖拽方向direction置为MouseDirection.Declining
           if (e.Location.X >= this.Width - 5 && e.Location.Y > this.Height - 5)
           {
               this.Cursor = Cursors.SizeNWSE;
               direction = MouseDirection.Declining;
           }
           //当鼠标移动时横坐标距离窗体右边缘5像素以内时,要将光标变为倾斜的箭头形状,同时拖拽方向direction置为MouseDirection.Herizontal
           else if (e.Location.X >= this.Width - 5)
           {
               this.Cursor = Cursors.SizeWE;
               direction = MouseDirection.Herizontal;
           }
           //同理当鼠标移动时纵坐标距离窗体下边缘5像素以内时,要将光标变为倾斜的箭头形状,同时拖拽方向direction置为MouseDirection.Vertical
           else if (e.Location.Y >= this.Height - 5)
           {
               this.Cursor = Cursors.SizeNS;
               direction = MouseDirection.Vertical;
           }
           //否则,以外的窗体区域,鼠标星座均为单向箭头(默认)            
           else this.Cursor = Cursors.Arrow;
           //设定好方向后,调用下面方法,改变窗体大小 
           ResizeWindow();
       }
 
       private void ResizeWindow()
       {
           //这个判断很重要,只有在鼠标按下时才能拖拽改变窗体大小,如果不作判断,那么鼠标弹起和按下时,窗体都可以改变
           if (!isMouseDown) return;
           //MousePosition的参考点是屏幕的左上角,表示鼠标当前相对于屏幕左上角的坐标this.left和this.top的参考点也是屏幕,属性MousePosition是该程序的重点
           if (direction == MouseDirection.Declining)
           {
               //此行代码在mousemove事件中已经写过,在此再写一遍,并不多余,一定要写
               this.Cursor = Cursors.SizeNWSE;
               //下面是改变窗体宽和高的代码,不明白的可以仔细思考一下
               this.Width = MousePosition.X - this.Left;
               this.Height = MousePosition.Y - this.Top;
           }
           //以下同理
           if (direction == MouseDirection.Herizontal)
           {
               this.Cursor = Cursors.SizeWE;
               this.Width = MousePosition.X - this.Left;
           }
           else if (direction == MouseDirection.Vertical)
           {
               this.Cursor = Cursors.SizeNS;
               this.Height = MousePosition.Y - this.Top;
           }
           //即使鼠标按下,但是不在窗口右和下边缘,那么也不能改变窗口大小
           else this.Cursor = Cursors.Arrow;
       }
        
       #endregion
   
   }
 
   /// <summary>
   /// 定义一个枚举,表示拖动方向
   /// </summary>
   public enum MouseDirection
   {
       Herizontal,//水平方向拖动,只改变窗体的宽度       
       Vertical,//垂直方向拖动,只改变窗体的高度
       Declining,//倾斜方向,同时改变窗体的宽度和高度       
       None//不做标志,即不拖动窗体改变大小
   }

  

posted on   sunwugang  阅读(1925)  评论(8编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2016-07-13 Winform/WPF国际化处理
点击右上角即可分享
微信分享提示