积极答复者
对于c#鼠标移动事件的问题

问题
-
我在写一个用鼠标拖动改变窗体大小的程序的时候,
发现当鼠标移开的边框时候窗体大小不改变了,
各位大虾帮忙解决下嘛- 已移动 Sheng Jiang 蒋晟Moderator 2009年6月22日 20:24 Windows窗体类库问题 ([Loc]From:Visual C#)
答案
-
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
全部回复
-
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