蓝派网(www.lan27.com)-精选网络资源,分享和交流! 文章首页站内搜索在线手册广告代码酷站欣赏万年历
您现在的位置: 蓝派网 >> 文章中心 >> 网络编程 >> .NET >> c#(Csharp) >> 正文

C#实现运行期控件设计

作者:佚名    文章来源:网络    更新时间:2009-2-11 11:48:53

最近看到很多人对C#运行期控件设计的问题,于是写了这个Memo希望对大家有用
 
  1、实现了控件自由拖动
 
  2、实现了控件的拖动创建,右键删除等类似IDE的控件创建,当然更多功能靠大家自己完善
  3、实现属性框与控件的绑定,可以在运行期修改控件的Text...

  以下是部分代码

view plaincopy to clipboardprint?

private void button2_Click(object sender, EventArgs e)   
{   
    //控件框的显示与隐藏   
    if (panel1.Visible == true)   
    {   
        button2.Text = "+  控件框";   
        panel1.Visible = false;   
    }   
    else  
    {   
        button2.Text = "-  控件框";   
        panel1.Visible = true;   
    }   
}  
        private void button2_Click(object sender, EventArgs e)
{
//控件框的显示与隐藏
if (panel1.Visible == true)
{
button2.Text = "+  控件框";
panel1.Visible = false;
}
else
{
button2.Text = "-  控件框";
panel1.Visible = true;
}
}

view plaincopy to clipboardprint?

private void button3_MouseDown(object sender, MouseEventArgs e)   
{   
    //判断鼠标左键按下   
    if (e.Button == MouseButtons.Left)   
    {   
        Button btn = (Button)(sender);   
        //初始化拖放操作。   
        btn.DoDragDrop(btn, DragDropEffects.Copy);   
    }   
}   
  
private void panel4_DragDrop(object sender, DragEventArgs e)   
{   
    //开始拖动   
    Button btn = (Button)(e.Data.GetData("System.Windows.Forms.Button"));   
    Button btn_new = new Button();   
    btn_new.ContextMenuStrip = contextMenuStrip1;   
    btn_new.Name = btn_new.Text = btn.Text + "--" + name;   
    btn_new.Left = PointToClient(MousePosition).X-panel4.Left;   
    btn_new.Top = PointToClient(MousePosition).Y - panel4.Top;   
    //加载事件   
    btn_new.Click += new System.EventHandler(this.button1_Click);   
    btn_new.MouseLeave += new System.EventHandler(this.button1_MouseLeave);   
    btn_new.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);   
    btn_new.MouseMove += new System.Windows.Forms.MouseEventHandler(this.button1_MouseMove);   
    btn_new.Parent = panel4;   
    name++;   
}  
        private void button3_MouseDown(object sender, MouseEventArgs e)
{
//判断鼠标左键按下
if (e.Button == MouseButtons.Left)
{
Button btn = (Button)(sender);
//初始化拖放操作。
btn.DoDragDrop(btn, DragDropEffects.Copy);
}
}
private void panel4_DragDrop(object sender, DragEventArgs e)
{
//开始拖动
Button btn = (Button)(e.Data.GetData("System.Windows.Forms.Button"));
Button btn_new = new Button();
btn_new.ContextMenuStrip = contextMenuStrip1;
btn_new.Name = btn_new.Text = btn.Text + "--" + name;
btn_new.Left = PointToClient(MousePosition).X-panel4.Left;
btn_new.Top = PointToClient(MousePosition).Y - panel4.Top;
//加载事件
btn_new.Click += new System.EventHandler(this.button1_Click);
btn_new.MouseLeave += new System.EventHandler(this.button1_MouseLeave);
btn_new.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
btn_new.MouseMove += new System.Windows.Forms.MouseEventHandler(this.button1_MouseMove);
btn_new.Parent = panel4;
name++;
}

view plaincopy to clipboardprint?

private void panel4_DragEnter(object sender, DragEventArgs e)   
{   
    e.Effect = DragDropEffects.Copy;   
}   
  
private void button1_Click(object sender, EventArgs e)   
{   
    groupBox1.Text = (sender as Button).Name + "属性";   
    textBox1.Text = (sender as Button).Text;   
  
}   
  
private void button4_MouseDown(object sender, MouseEventArgs e)   
{   
    //判断鼠标

  • 左键按下   
  •     if (e.Button == MouseButtons.Left)   
  •     {   
  •         Button btn = (Button)(sender);   
  •         //初始化拖放操作。   
  •         btn.DoDragDrop(btn, DragDropEffects.Copy);   
  •     }   
  • }  

      view plaincopy to clipboardprint?

      1. private void toolStripMenuItem2_Click(object sender, EventArgs e)   
      2. {   
      3.     //释放控件   
      4.     btnflag.Dispose();   
      5. }   
      6.   
      7. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)   
      8. {   
      9.     btnflag.Text = textBox1.Text;   
      10. }   
      11.   
      12. private void textBox1_KeyDown(object sender, KeyEventArgs e)   
      13. {   
      14.     //响应回车   
      15.     if (e.KeyValue == 13)   
      16.         btnflag.Text = textBox1.Text;   
      17.   
      18. }   
      19.   
      20. private void button7_Click(object sender, EventArgs e)   
      21. {   
      22.     //控件框的显示与隐藏   
      23.     if (groupBox1.Visible == true)   
      24.     {   
      25.         button7.Text = "+  属性窗口";   
      26.         groupBox1.Visible = false;   
      27.     }   
      28.     else  
      29.     {   
      30.         button7.Text = "-  属性窗口";   
      31.         groupBox1.Visible = true;   
      32.     }   
      33. }  

      实现的效果图如下

      C#实现运行期控件设计

      点击查看大图

        更多功能当然需要大家自己扩展,假如你有兴趣...

    1. 发表评论】【打印此文】【关闭窗口】【点击数:
      ★好玩的休闲小游戏★