自己做了窗体控件,怎么实现鼠标拖动边框,大小就改变. 准备自己做一个比较独特的窗体控件,到了用鼠标拖动边框时不能实现变大,只能变小.是不是要获得鼠标在屏幕中的坐标,怎么能实现呢.还请各位高手能指点 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 鼠标左击标题栏处理程序,开始移动本控件 /// 2004-10-04 修改,改为移动焦点框 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TitlePanel_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if(!this.Entered) { this.Focus(); Entered=true; } if( (e.Button==MouseButtons.Left) ) { Moving=true; OldLeft = e.X; OldTop = e.Y; //获取屏幕上下文 Common.ClipCursorIn(this.Parent); ScreenDC = Crownwood.Magic.Win32.User32.GetDC(System.IntPtr.Zero); Point p = new Point(0,0); p = this.Parent.PointToScreen(p); ClipRgn = WinAPI.CreateRectRgn(p.X,p.Y,p.X + this.Parent.Width,p.Y + this.Parent.Height); WinAPI.SelectClipRgn(ScreenDC,ClipRgn); //rect.left = -900; #region 画焦点框 rect.left = this.Left; rect.top = this.Top; rect.right = this.Left + this.Width; rect.bottom = this.Top + this.Height; p = new Point(rect.left,rect.top); p = this.Parent.PointToScreen(p); rect.left = p.X; rect.top = p.Y; p = new Point(rect.right,rect.bottom); p = this.Parent.PointToScreen(p); rect.right = p.X; rect.bottom = p.Y; Crownwood.Magic.Win32.User32.DrawFocusRect(ScreenDC,ref rect); #endregion //Crownwood.Magic.Win32.User32. } } /// <summary> /// 鼠标在标题栏移动时的程序,移动本控件 /// 2004-10-04 修改,改为移动焦点框 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TitlePanel_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if( Moving ) { //将以前的重新画一遍 //if(rect.left != -900) Crownwood.Magic.Win32.User32.DrawFocusRect(ScreenDC,ref rect); rect.left = this.Left + e.X - OldLeft; rect.top = this.Top + e.Y - OldTop; rect.right = rect.left + this.Width; rect.bottom = rect.top + this.Height; Point p = new Point(rect.left,rect.top); p = this.Parent.PointToScreen(p); rect.left = p.X; rect.top = p.Y; p = new Point(rect.right,rect.bottom); p = this.Parent.PointToScreen(p); rect.right = p.X; rect.bottom = p.Y; Crownwood.Magic.Win32.User32.DrawFocusRect(ScreenDC,ref rect); } } /// <summary> /// 鼠标键在标题栏释放处理程序,移动本控件结束 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TitlePanel_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if (Moving) { Crownwood.Magic.Win32.User32.DrawFocusRect(ScreenDC,ref rect); if ((this.Left!=this.Left+e.X+OldLeft) ||(this.Top!=this.Top+e.Y-OldTop)) { this.Left=this.Left+e.X - OldLeft; this.Top=this.Top+e.Y - OldTop; } Common.RestoreCursor(); WinAPI.DeleteObject(ClipRgn); Crownwood.Magic.Win32.User32.ReleaseDC(System.IntPtr.Zero,ScreenDC); this.Parent.Refresh(); } Moving=false; if(e.Button==MouseButtons.Right&&PopMenu!=null) PopMenu.TrackPopup(TitlePanel.PointToScreen(new Point(e.X,e.Y))); //if Assigned(FOnColRePaint) then // FOnColRePaint(self); } 谢谢xxj!但有很多地方不明白.能不能再给我讲讲Common.ClipCursorIn(this.Parent);ScreenDC = Crownwood.Magic.Win32.User32.GetDC(System.IntPtr.Zero); [DllImport("User32.dll")]public static System.IntPtr GetDC ( System.IntPtr hWnd );ClipCursorIn(this.Parent);/// <summary> /// 锁定鼠标光标活动范围,引入自user32.dll /// </summary> /// <param name="r"></param> [DllImport("user32.dll")] public extern static void ClipCursor(RECT r); 如何正确显示动态创建的控件? 标题与任务栏 关于IDisposable调用问题 C#有没有输入函数 加载flash 关于TabContorl控件问题. [winform] 这个情况你们是怎么操作的? 在线等 怎么在类间调用初始化了的结构体?? 求救:创建事件源问题 C#中值类型有“预定义类型”这是什么意思啊?我是新手!急救! 怎么在运行时能让用户通过拖动控件改变控件的位置和大小等等,如同在.net设计时设计窗体 如何使鼠标拖动可以改变无边框的窗体的大小啊?
/// 鼠标左击标题栏处理程序,开始移动本控件
/// 2004-10-04 修改,改为移动焦点框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TitlePanel_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(!this.Entered)
{
this.Focus();
Entered=true;
}
if( (e.Button==MouseButtons.Left) )
{ Moving=true;
OldLeft = e.X;
OldTop = e.Y;
//获取屏幕上下文 Common.ClipCursorIn(this.Parent);
ScreenDC = Crownwood.Magic.Win32.User32.GetDC(System.IntPtr.Zero);
Point p = new Point(0,0);
p = this.Parent.PointToScreen(p);
ClipRgn = WinAPI.CreateRectRgn(p.X,p.Y,p.X + this.Parent.Width,p.Y + this.Parent.Height);
WinAPI.SelectClipRgn(ScreenDC,ClipRgn);
//rect.left = -900;
#region 画焦点框
rect.left = this.Left;
rect.top = this.Top;
rect.right = this.Left + this.Width;
rect.bottom = this.Top + this.Height; p = new Point(rect.left,rect.top);
p = this.Parent.PointToScreen(p);
rect.left = p.X;
rect.top = p.Y;
p = new Point(rect.right,rect.bottom);
p = this.Parent.PointToScreen(p);
rect.right = p.X;
rect.bottom = p.Y;
Crownwood.Magic.Win32.User32.DrawFocusRect(ScreenDC,ref rect);
#endregion
//Crownwood.Magic.Win32.User32.
}
}
/// <summary>
/// 鼠标在标题栏移动时的程序,移动本控件
/// 2004-10-04 修改,改为移动焦点框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TitlePanel_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if( Moving )
{
//将以前的重新画一遍 //if(rect.left != -900)
Crownwood.Magic.Win32.User32.DrawFocusRect(ScreenDC,ref rect);
rect.left = this.Left + e.X - OldLeft;
rect.top = this.Top + e.Y - OldTop;
rect.right = rect.left + this.Width;
rect.bottom = rect.top + this.Height; Point p = new Point(rect.left,rect.top);
p = this.Parent.PointToScreen(p);
rect.left = p.X;
rect.top = p.Y;
p = new Point(rect.right,rect.bottom);
p = this.Parent.PointToScreen(p);
rect.right = p.X;
rect.bottom = p.Y;
Crownwood.Magic.Win32.User32.DrawFocusRect(ScreenDC,ref rect);
}
}
/// <summary>
/// 鼠标键在标题栏释放处理程序,移动本控件结束
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TitlePanel_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (Moving)
{
Crownwood.Magic.Win32.User32.DrawFocusRect(ScreenDC,ref rect);
if ((this.Left!=this.Left+e.X+OldLeft)
||(this.Top!=this.Top+e.Y-OldTop))
{
this.Left=this.Left+e.X - OldLeft;
this.Top=this.Top+e.Y - OldTop;
}
Common.RestoreCursor();
WinAPI.DeleteObject(ClipRgn);
Crownwood.Magic.Win32.User32.ReleaseDC(System.IntPtr.Zero,ScreenDC);
this.Parent.Refresh();
}
Moving=false;
if(e.Button==MouseButtons.Right&&PopMenu!=null)
PopMenu.TrackPopup(TitlePanel.PointToScreen(new Point(e.X,e.Y)));
//if Assigned(FOnColRePaint) then
// FOnColRePaint(self);
}
ScreenDC = Crownwood.Magic.Win32.User32.GetDC(System.IntPtr.Zero);
public static System.IntPtr GetDC ( System.IntPtr hWnd );ClipCursorIn(this.Parent);/// <summary>
/// 锁定鼠标光标活动范围,引入自user32.dll
/// </summary>
/// <param name="r"></param>
[DllImport("user32.dll")]
public extern static void ClipCursor(RECT r);