http://www.microsoft.com/china/community/article.asp?oBODY=TechZone/TechArti
cle/TechDoc/csharpui&oXSLT=TechZone/TechArticle/TechContent
有篇文章,是窗体控件form的,希望有可以借鉴的东西。
至于其他,极度关注,帮你up下:)

解决方案 »

  1.   

    下面是我写的一个类,你直接使用它就可以了。它可以使窗体上的控件象设计时一样自由移动和伸缩,缺点是控件的父控件必须是窗体,如果你有兴趣可以完善它。
    使用:
    假设你有一个窗体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;
    }
    }
      

  2.   

    下面是我写的一个类,你直接使用它就可以了。它可以使窗体上的控件象设计时一样自由移动和伸缩,缺点是控件的父控件必须是窗体,如果你有兴趣可以完善它。
    使用:
    假设你有一个窗体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;
    }
    }
      

  3.   

    dy_2000_abc(芝麻开门):
        我运行你的程序是出现这个问题:“frm1是类,这里应该是变量”。然后我直接用一个label代替你ctrl,运行没问题,控件也可以移动,但你的控件虚线却出现在控件上,也不可以拉伸?是不是我调试有问题呢?
        十分感谢!
      

  4.   

    这是我从我写的一个控件里临时弄出的一点代码,需要改动一下才能用好。在MouseUp后面添加ctrl.Refresh();这样虚线就没有了。你将鼠标放到控件右下角,就可以拉伸控件了。还有的问题就是位置问题,控件的实际位置总是比你放置的位置大处frm1.Location.X-frm1.ClientRectangle.X和frm1.Location.Y-frm.ClientRectangle.Y。
      

  5.   

    我把代码改完整,这样好用多了,
    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();
    }
    }
      

  6.   

    奇怪,我还是无法拉伸。还有这个问题:“frm1是类,这里应该是变量”该如何解决呢?谢谢!!!
      

  7.   

    打个比方:
    Form1 frm=new Form1();
    你传递的是Form1,Form1是类,你需要传递变量 frm
      

  8.   

    如果在窗体Load时,传递 this 表示本窗体
      

  9.   

    我这样子:
    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不能移动和收缩。。:( 我太笨了。。
      

  10.   

    我在代码中设置在不离开控件右下角10*10像素时,鼠标拉动将调整控件的大小。我估计你的问题是你用的控件是Label,它的右下角不一定是文字内容的右下角,你在Lable上按下鼠标,你将看到一个虚线框,那才是Lable的工作区域。为了直观一些,你使用Button,看得就清楚了。
      

  11.   

    不好意思。连移动都不行。能不能帮我看看我上面的调试方法对不对吗?我想应该是那样调试的。我昨晚用了textbox试过还是老问题。你能否能把调试好的程序寄给我下。[email protected],这么多次麻烦你真的不好意思!!!
      

  12.   

    TextBox也是可以的,我测试过Vs.net上的控件,都没有问题。TextBox的问题是你需要小心地注意鼠标的位置,你把鼠标放到TextBox的右下角,看到鼠标显示为|时,点击拉动就可以调整大小了,当然,如果TextBox的MultiLine=false的话,你就只能调整它的宽了。如果嫌麻烦,你可以将调整大小的区域设大一些,具体设置if (e.X<this.ctrl.Width-10||e.Y<this.ctrl.Height-10)这一句,将10改成20或者其他
    我将调用的代码贴出来,将它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); }
    }

      

  13.   

    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;
    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();
    }
    }}
      

  14.   

    成功!!!请立刻结账!非常感谢dy_2000_abc(芝麻开门)!以后请多多指教!!!