none
对于c#鼠标移动事件的问题 RRS feed

  • 问题

  • 我在写一个用鼠标拖动改变窗体大小的程序的时候,
    发现当鼠标移开的边框时候窗体大小不改变了,
    各位大虾帮忙解决下嘛
    2009年6月22日 15:17

答案

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    namespace X.WinFormsApp
    {
        /// <summary>
        /// 标题:简单鼠标操作
        /// 日期:2009-05-11
        /// 描述:用C#怎么实现标题栏功能 左键单击退拽左键双击最大化;通过拖放改变窗体大小
        ///     
        /// </summary>
        public partial class X200905111129 : Form
        {
            public X200905111129()
            {
                InitializeComponent();
    
                this.FormBorderStyle = FormBorderStyle.None;
                this.MouseDown += new MouseEventHandler(X200905111129_MouseDown);
                this.MouseMove += new MouseEventHandler(X200905111129_MouseMove);
                this.DoubleClick += new EventHandler(X200905111129_DoubleClick);
            }
    
            #region " 拖动无边框窗体 "
    
            private void X200905111129_DoubleClick(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Maximized;
            }
    
            private Point fPoint;
    
            private void X200905111129_MouseDown(object sender, MouseEventArgs e)
            {
                fPoint = e.Location;
            }
    
            private void X200905111129_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    int fNewX = this.Location.X + e.X - fPoint.X;
                    int fNewY = this.Location.Y + e.Y - fPoint.Y;
                    this.Location = new Point(fNewX, fNewY);
                }
            }
    
            #endregion
    
            #region " 改变无边框窗体大小 "
    
            const int WM_NCHITTEST = 0x0084;
            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)
            {
                base.WndProc(ref m);
                switch (m.Msg)
                {
                    case WM_NCHITTEST:
                        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;
                }
            }
    
            #endregion
    
        }
    }
    

    知识改变命运,奋斗成就人生!
    • 已标记为答案 ifree 2009年6月22日 16:47
    2009年6月22日 16:29
    版主

全部回复

  • 你设置窗体的属性 FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable 就自动可以以拖动方式改变窗体大小了,不需要自己写代码
                

    知识改变命运,奋斗成就人生!
    2009年6月22日 15:31
    版主
  • 你设置窗体的属性 FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable 就自动可以以拖动方式改变窗体大小了,不需要自己写代码
                

    知识改变命运,奋斗成就人生!

    大哥,我是用n张图片拼好的,那没用的 帮帮忙阿

    • 已编辑 ifree 2009年6月22日 16:03
    2009年6月22日 16:02
  • n张图片拼好的是什么意思?
    知识改变命运,奋斗成就人生!
    2009年6月22日 16:03
    版主
  • 窗口没有边框,用图片做的窗口, 当鼠标移出窗口改变大小的方法就无法调用了,我在考虑用鼠标钩子,但是考虑到系统性能,

    2009年6月22日 16:14
  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    namespace X.WinFormsApp
    {
        /// <summary>
        /// 标题:简单鼠标操作
        /// 日期:2009-05-11
        /// 描述:用C#怎么实现标题栏功能 左键单击退拽左键双击最大化;通过拖放改变窗体大小
        ///     
        /// </summary>
        public partial class X200905111129 : Form
        {
            public X200905111129()
            {
                InitializeComponent();
    
                this.FormBorderStyle = FormBorderStyle.None;
                this.MouseDown += new MouseEventHandler(X200905111129_MouseDown);
                this.MouseMove += new MouseEventHandler(X200905111129_MouseMove);
                this.DoubleClick += new EventHandler(X200905111129_DoubleClick);
            }
    
            #region " 拖动无边框窗体 "
    
            private void X200905111129_DoubleClick(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Maximized;
            }
    
            private Point fPoint;
    
            private void X200905111129_MouseDown(object sender, MouseEventArgs e)
            {
                fPoint = e.Location;
            }
    
            private void X200905111129_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    int fNewX = this.Location.X + e.X - fPoint.X;
                    int fNewY = this.Location.Y + e.Y - fPoint.Y;
                    this.Location = new Point(fNewX, fNewY);
                }
            }
    
            #endregion
    
            #region " 改变无边框窗体大小 "
    
            const int WM_NCHITTEST = 0x0084;
            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)
            {
                base.WndProc(ref m);
                switch (m.Msg)
                {
                    case WM_NCHITTEST:
                        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;
                }
            }
    
            #endregion
    
        }
    }
    

    知识改变命运,奋斗成就人生!
    • 已标记为答案 ifree 2009年6月22日 16:47
    2009年6月22日 16:29
    版主
  • 谢谢了,解决了呵呵

    2009年6月22日 16:48