http://www.microsoft.com/china/community/article.asp?oBODY=TechZone/TechArti
cle/TechDoc/csharpui&oXSLT=TechZone/TechArticle/TechContent
有篇文章,是窗体控件form的,希望有可以借鉴的东西。
至于其他,极度关注,帮你up下:)
cle/TechDoc/csharpui&oXSLT=TechZone/TechArticle/TechContent
有篇文章,是窗体控件form的,希望有可以借鉴的东西。
至于其他,极度关注,帮你up下:)
使用:
假设你有一个窗体frm1,窗体上有一个Lable--lable1,添加下面的语句就可以了,比如:
private void frm1_Load(object sender,EventArgs e)
{
Resize rs=new Resize(lable1,frm1);
}
类代码:
public class Resize
{ bool IsMoving=false;
int ctrlLastWidth=0;
int ctrlLastHeight=0;
int ctrlWidth;
int ctrlHeight;
int ctrlLeft;
int ctrlTop;
int cursorL;
int cursorT;
int ctrlLastLeft;
int ctrlLastTop;
bool ctrlIsResizing=false;
System.Drawing.Rectangle ctrlRectangle = new System.Drawing.Rectangle();
private Control ctrl;
private Form frm;
public Resize(Control c,Form frm)
{
ctrl=c;
this.frm=frm;
ctrl.MouseDown+=new MouseEventHandler(MouseDown);
ctrl.MouseMove+=new MouseEventHandler(MouseMove);
ctrl.MouseUp+=new MouseEventHandler(MouseUp);
}
private void MouseMove(object sender,MouseEventArgs e)
{
if (frm==null)
return;
if (e.Button==MouseButtons.Left)
{
if(this.IsMoving)
{
if (ctrlLastLeft == 0)
ctrlLastLeft = ctrlLeft;
if (ctrlLastTop==0)
ctrlLastTop = ctrlTop;
int locationX=(Cursor.Position.X-this.cursorL+this.frm.DesktopLocation.X+this.ctrl.Location.X);
int locationY=(Cursor.Position.Y-this.cursorT+this.frm.DesktopLocation.Y+this.ctrl.Location.Y);
if(locationX<this.frm.DesktopLocation.X)
locationX=this.frm.DesktopLocation.X;
if(locationY<this.frm.DesktopLocation.Y)
locationY=this.frm.DesktopLocation.Y;
this.ctrlLeft=locationX;
this.ctrlTop=locationY;
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLastLeft,this.ctrlLastTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastLeft = ctrlLeft;
ctrlLastTop = ctrlTop;
ctrlRectangle.Location = new System.Drawing.Point(ctrlLeft,ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
return;
}
int sizeageX = (Cursor.Position.X-this.frm.DesktopLocation.X-this.ctrl.Location.X);
int sizeageY = (Cursor.Position.Y-this.frm.DesktopLocation.Y-this.ctrl.Location.Y);
if (sizeageX < 2)
sizeageX = 1;
if (sizeageY < 2)
sizeageY = 1;
ctrlWidth = sizeageX;
ctrlHeight = sizeageY;
if (ctrlLastWidth == 0)
ctrlLastWidth = ctrlWidth;
if (ctrlLastHeight==0)
ctrlLastHeight = ctrlHeight;
if (ctrlIsResizing)
{
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.ctrl.Left,this.frm.DesktopLocation.Y+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlLastWidth,ctrlLastHeight);
}
ctrlIsResizing = true;
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastWidth = ctrlWidth;
ctrlLastHeight = ctrlHeight;
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.ctrl.Left,this.frm.DesktopLocation.Y+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
}
private void MouseDown(object sender,MouseEventArgs e)
{
if (frm==null)
return;
if (e.X<this.ctrl.Width-10||e.Y<this.ctrl.Height-10)
{
this.IsMoving=true;
this.ctrlLeft=this.frm.DesktopLocation.X+this.ctrl.Left;
this.ctrlTop=this.frm.DesktopLocation.Y+this.ctrl.Top;
this.cursorL=Cursor.Position.X;
this.cursorT=Cursor.Position.Y;
this.ctrlWidth=this.ctrl.Width;
this.ctrlHeight=this.ctrl.Height;
}
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
private void MouseUp(object sender,MouseEventArgs e)
{
if (frm==null)
return;
ctrlIsResizing = false;
if (this.IsMoving)
{
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Left=this.ctrlLeft-this.frm.DesktopLocation.X;
this.ctrl.Top=this.ctrlTop-this.frm.DesktopLocation.Y;
this.IsMoving=false;
return;
}
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.ctrl.Left,this.frm.DesktopLocation.Y+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Width = ctrlWidth;
this.ctrl.Height = ctrlHeight;
}
}
使用:
假设你有一个窗体frm1,窗体上有一个Lable--lable1,添加下面的语句就可以了,比如:
private void frm1_Load(object sender,EventArgs e)
{
Resize rs=new Resize(lable1,frm1);
}
类代码:
public class Resize
{ bool IsMoving=false;
int ctrlLastWidth=0;
int ctrlLastHeight=0;
int ctrlWidth;
int ctrlHeight;
int ctrlLeft;
int ctrlTop;
int cursorL;
int cursorT;
int ctrlLastLeft;
int ctrlLastTop;
bool ctrlIsResizing=false;
System.Drawing.Rectangle ctrlRectangle = new System.Drawing.Rectangle();
private Control ctrl;
private Form frm;
public Resize(Control c,Form frm)
{
ctrl=c;
this.frm=frm;
ctrl.MouseDown+=new MouseEventHandler(MouseDown);
ctrl.MouseMove+=new MouseEventHandler(MouseMove);
ctrl.MouseUp+=new MouseEventHandler(MouseUp);
}
private void MouseMove(object sender,MouseEventArgs e)
{
if (frm==null)
return;
if (e.Button==MouseButtons.Left)
{
if(this.IsMoving)
{
if (ctrlLastLeft == 0)
ctrlLastLeft = ctrlLeft;
if (ctrlLastTop==0)
ctrlLastTop = ctrlTop;
int locationX=(Cursor.Position.X-this.cursorL+this.frm.DesktopLocation.X+this.ctrl.Location.X);
int locationY=(Cursor.Position.Y-this.cursorT+this.frm.DesktopLocation.Y+this.ctrl.Location.Y);
if(locationX<this.frm.DesktopLocation.X)
locationX=this.frm.DesktopLocation.X;
if(locationY<this.frm.DesktopLocation.Y)
locationY=this.frm.DesktopLocation.Y;
this.ctrlLeft=locationX;
this.ctrlTop=locationY;
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLastLeft,this.ctrlLastTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastLeft = ctrlLeft;
ctrlLastTop = ctrlTop;
ctrlRectangle.Location = new System.Drawing.Point(ctrlLeft,ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
return;
}
int sizeageX = (Cursor.Position.X-this.frm.DesktopLocation.X-this.ctrl.Location.X);
int sizeageY = (Cursor.Position.Y-this.frm.DesktopLocation.Y-this.ctrl.Location.Y);
if (sizeageX < 2)
sizeageX = 1;
if (sizeageY < 2)
sizeageY = 1;
ctrlWidth = sizeageX;
ctrlHeight = sizeageY;
if (ctrlLastWidth == 0)
ctrlLastWidth = ctrlWidth;
if (ctrlLastHeight==0)
ctrlLastHeight = ctrlHeight;
if (ctrlIsResizing)
{
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.ctrl.Left,this.frm.DesktopLocation.Y+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlLastWidth,ctrlLastHeight);
}
ctrlIsResizing = true;
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastWidth = ctrlWidth;
ctrlLastHeight = ctrlHeight;
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.ctrl.Left,this.frm.DesktopLocation.Y+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
}
private void MouseDown(object sender,MouseEventArgs e)
{
if (frm==null)
return;
if (e.X<this.ctrl.Width-10||e.Y<this.ctrl.Height-10)
{
this.IsMoving=true;
this.ctrlLeft=this.frm.DesktopLocation.X+this.ctrl.Left;
this.ctrlTop=this.frm.DesktopLocation.Y+this.ctrl.Top;
this.cursorL=Cursor.Position.X;
this.cursorT=Cursor.Position.Y;
this.ctrlWidth=this.ctrl.Width;
this.ctrlHeight=this.ctrl.Height;
}
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
private void MouseUp(object sender,MouseEventArgs e)
{
if (frm==null)
return;
ctrlIsResizing = false;
if (this.IsMoving)
{
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Left=this.ctrlLeft-this.frm.DesktopLocation.X;
this.ctrl.Top=this.ctrlTop-this.frm.DesktopLocation.Y;
this.IsMoving=false;
return;
}
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.ctrl.Left,this.frm.DesktopLocation.Y+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Width = ctrlWidth;
this.ctrl.Height = ctrlHeight;
}
}
我运行你的程序是出现这个问题:“frm1是类,这里应该是变量”。然后我直接用一个label代替你ctrl,运行没问题,控件也可以移动,但你的控件虚线却出现在控件上,也不可以拉伸?是不是我调试有问题呢?
十分感谢!
public class ResizeAction
{ bool IsMoving=false;
int ctrlLastWidth=0;
int ctrlLastHeight=0;
int ctrlWidth;
int ctrlHeight;
int ctrlLeft;
int ctrlTop;
int cursorL;
int cursorT;
int ctrlLastLeft;
int ctrlLastTop;
int Htap;
int Wtap;
bool ctrlIsResizing=false;
System.Drawing.Rectangle ctrlRectangle = new System.Drawing.Rectangle();
private Control ctrl;
private Form frm;
public ResizeAction(Control c,Form frm)
{
ctrl=c;
this.frm=frm;
this.Htap=this.frm.Height-this.frm.ClientRectangle.Height;
this.Wtap=this.frm.Width-this.frm.ClientRectangle.Width;
ctrl.MouseDown+=new MouseEventHandler(MouseDown);
ctrl.MouseMove+=new MouseEventHandler(MouseMove);
ctrl.MouseUp+=new MouseEventHandler(MouseUp);
}
public void MouseMove(object sender,MouseEventArgs e)
{
if (frm==null)
return;
if (e.Button==MouseButtons.Left)
{
if(this.IsMoving)
{
if (ctrlLastLeft == 0)
ctrlLastLeft = ctrlLeft;
if (ctrlLastTop==0)
ctrlLastTop = ctrlTop;
int locationX=(Cursor.Position.X-this.cursorL+this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Location.X);
int locationY=(Cursor.Position.Y-this.cursorT+this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Location.Y);
if(locationX<this.frm.DesktopLocation.X+this.Wtap)
locationX=this.frm.DesktopLocation.X+this.Wtap;
if(locationY<this.frm.DesktopLocation.Y+this.Htap)
locationY=this.frm.DesktopLocation.Y+this.Htap;
this.ctrlLeft=locationX;
this.ctrlTop=locationY;
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLastLeft,this.ctrlLastTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastLeft = ctrlLeft;
ctrlLastTop = ctrlTop;
ctrlRectangle.Location = new System.Drawing.Point(ctrlLeft,ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
return;
}
int sizeageX = (Cursor.Position.X-this.frm.DesktopLocation.X-this.Wtap-this.ctrl.Location.X);
int sizeageY = (Cursor.Position.Y-this.frm.DesktopLocation.Y-this.Htap-this.ctrl.Location.Y);
if (sizeageX < 2)
sizeageX = 1;
if (sizeageY < 2)
sizeageY = 1;
ctrlWidth = sizeageX;
ctrlHeight = sizeageY;
if (ctrlLastWidth == 0)
ctrlLastWidth = ctrlWidth;
if (ctrlLastHeight==0)
ctrlLastHeight = ctrlHeight;
if (ctrlIsResizing)
{
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.ctrl.Left+this.Wtap,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlLastWidth,ctrlLastHeight);
}
ctrlIsResizing = true;
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastWidth = ctrlWidth;
ctrlLastHeight = ctrlHeight;
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
}
public void MouseDown(object sender,MouseEventArgs e)
{
if (frm==null)
return;
if (e.X<this.ctrl.Width-10||e.Y<this.ctrl.Height-10)
{
this.IsMoving=true;
this.ctrlLeft=this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left;
this.ctrlTop=this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top;
this.cursorL=Cursor.Position.X;
this.cursorT=Cursor.Position.Y;
this.ctrlWidth=this.ctrl.Width;
this.ctrlHeight=this.ctrl.Height;
}
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
public void MouseUp(object sender,MouseEventArgs e)
{
if (frm==null)
return;
ctrlIsResizing = false;
if (this.IsMoving)
{
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Left=this.ctrlLeft-this.frm.DesktopLocation.X-this.Wtap;
this.ctrl.Top=this.ctrlTop-this.frm.DesktopLocation.Y-this.Htap;
this.IsMoving=false;
this.ctrl.Refresh();
return;
}
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Width = ctrlWidth;
this.ctrl.Height = ctrlHeight;
this.ctrl.Refresh();
}
}
Form1 frm=new Form1();
你传递的是Form1,Form1是类,你需要传递变量 frm
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace ResizeCtrl
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
private void Form1_Load(object sender,EventArgs e)
{
ResizeAction rs=new ResizeAction(label1,this);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(64, 48);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion [STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
是没错误,但lable1不能移动和收缩。。:( 我太笨了。。
我将调用的代码贴出来,将它Copy过去执行就可以了:using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ToolBarButton toolBarButton1;
private System.Windows.Forms.ToolBarButton toolBarButton2;
private System.Windows.Forms.ToolBarButton toolBarButton3;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox listBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
this.textBox1 = new System.Windows.Forms.TextBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.Desktop;
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.checkBox1,
this.comboBox1});
this.panel1.Location = new System.Drawing.Point(32, 80);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(208, 48);
this.panel1.TabIndex = 0;
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(8, 16);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(56, 24);
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "abc";
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"a",
"b",
"c"});
this.comboBox1.Location = new System.Drawing.Point(88, 16);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(104, 20);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "comboBox1";
//
// toolBar1
//
this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
this.toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.toolBarButton1,
this.toolBarButton2,
this.toolBarButton3});
this.toolBar1.Dock = System.Windows.Forms.DockStyle.None;
this.toolBar1.DropDownArrows = true;
this.toolBar1.Location = new System.Drawing.Point(40, 8);
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(208, 40);
this.toolBar1.TabIndex = 1;
//
// toolBarButton1
//
this.toolBarButton1.Pushed = true;
this.toolBarButton1.Text = "a1";
//
// toolBarButton2
//
this.toolBarButton2.Text = "a2";
//
// toolBarButton3
//
this.toolBarButton3.Text = "a3";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 160);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(88, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "textBox1";
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Items.AddRange(new object[] {
"a",
"b",
"c"});
this.listBox1.Location = new System.Drawing.Point(128, 160);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(128, 64);
this.listBox1.TabIndex = 3;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.listBox1,
this.textBox1,
this.toolBar1,
this.panel1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
Resize r1=new Resize(this.toolBar1,this);
Resize r2= new Resize(this.panel1,this);
Resize r3= new Resize(this.textBox1,this);
Resize r4= new Resize(this.listBox1,this); }
}
{ bool IsMoving=false;
int ctrlLastWidth=0;
int ctrlLastHeight=0;
int ctrlWidth;
int ctrlHeight;
int ctrlLeft;
int ctrlTop;
int cursorL;
int cursorT;
int ctrlLastLeft;
int ctrlLastTop;
int Htap;
int Wtap;
bool ctrlIsResizing=false;
System.Drawing.Rectangle ctrlRectangle = new System.Drawing.Rectangle();
private Control ctrl;
private Form frm;
public Resize(Control c,Form frm)
{
ctrl=c;
this.frm=frm;
this.Htap=this.frm.Height-this.frm.ClientRectangle.Height;
this.Wtap=this.frm.Width-this.frm.ClientRectangle.Width;
ctrl.MouseDown+=new MouseEventHandler(MouseDown);
ctrl.MouseMove+=new MouseEventHandler(MouseMove);
ctrl.MouseUp+=new MouseEventHandler(MouseUp);
}
private void MouseMove(object sender,MouseEventArgs e)
{
if (frm==null)
return;
if (e.Button==MouseButtons.Left)
{
if(this.IsMoving)
{
if (ctrlLastLeft == 0)
ctrlLastLeft = ctrlLeft;
if (ctrlLastTop==0)
ctrlLastTop = ctrlTop;
int locationX=(Cursor.Position.X-this.cursorL+this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Location.X);
int locationY=(Cursor.Position.Y-this.cursorT+this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Location.Y);
if(locationX<this.frm.DesktopLocation.X+this.Wtap)
locationX=this.frm.DesktopLocation.X+this.Wtap;
if(locationY<this.frm.DesktopLocation.Y+this.Htap)
locationY=this.frm.DesktopLocation.Y+this.Htap;
this.ctrlLeft=locationX;
this.ctrlTop=locationY;
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLastLeft,this.ctrlLastTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastLeft = ctrlLeft;
ctrlLastTop = ctrlTop;
ctrlRectangle.Location = new System.Drawing.Point(ctrlLeft,ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
return;
}
int sizeageX = (Cursor.Position.X-this.frm.DesktopLocation.X-this.Wtap-this.ctrl.Location.X);
int sizeageY = (Cursor.Position.Y-this.frm.DesktopLocation.Y-this.Htap-this.ctrl.Location.Y);
if (sizeageX < 2)
sizeageX = 1;
if (sizeageY < 2)
sizeageY = 1;
ctrlWidth = sizeageX;
ctrlHeight = sizeageY;
if (ctrlLastWidth == 0)
ctrlLastWidth = ctrlWidth;
if (ctrlLastHeight==0)
ctrlLastHeight = ctrlHeight;
if (ctrlIsResizing)
{
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.ctrl.Left+this.Wtap,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlLastWidth,ctrlLastHeight);
}
ctrlIsResizing = true;
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastWidth = ctrlWidth;
ctrlLastHeight = ctrlHeight;
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
}
private void MouseDown(object sender,MouseEventArgs e)
{
if (frm==null)
return;
if (e.X<this.ctrl.Width-10||e.Y<this.ctrl.Height-10)
{
this.IsMoving=true;
this.ctrlLeft=this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left;
this.ctrlTop=this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top;
this.cursorL=Cursor.Position.X;
this.cursorT=Cursor.Position.Y;
this.ctrlWidth=this.ctrl.Width;
this.ctrlHeight=this.ctrl.Height;
}
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
private void MouseUp(object sender,MouseEventArgs e)
{
if (frm==null)
return;
ctrlIsResizing = false;
if (this.IsMoving)
{
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Left=this.ctrlLeft-this.frm.DesktopLocation.X-this.Wtap;
this.ctrl.Top=this.ctrlTop-this.frm.DesktopLocation.Y-this.Htap;
this.IsMoving=false;
this.ctrl.Refresh();
return;
}
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Width = ctrlWidth;
this.ctrl.Height = ctrlHeight;
this.ctrl.Refresh();
}
}}