C#窗体控制源代码

原创 2006年05月23日 21:34:00

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WinF
{
 /// <summary>
 /// Control 的摘要说明。
 /// </summary>
 public class ControlApp
 {
  ///////////////////////////////////////////////////////////////////////////////////
  
  private System.Windows.Forms.Form senderForm;

  private string FilePath=System.AppDomain.CurrentDomain.BaseDirectory.ToString(); //窗体控件设置配置文件位置

  private System.Windows.Forms.Control sender;//定义被操作的控件

  private System.Windows.Forms.ContextMenu DeleCM=new ContextMenu();
  
  public ControlApp(System.Windows.Forms.Form senderForm,string FilePath)
  {
   this.senderForm=senderForm;
   this.FilePath=FilePath;
   StartControl();
  }

//  public System.Windows.Forms.Form sender_Form()
//  {
//   get {return this.senderForm;}
//  }

  
  #region///鼠标状态的枚举类型
  /// </summary>
  enum EnumMousePointPosition
  {
   MouseSizeNone = 0 ,//无

    MouseSizeRight = 1 ,//拉伸右边框

    MouseSizeLeft = 2 ,//拉伸左边框

    MouseSizeBottom = 3 ,//拉伸下边框

    MouseSizeTop = 4 ,//拉伸上边框

    MouseSizeTopLeft = 5 ,//拉伸左上角

    MouseSizeTopRight = 6 ,//拉伸右上角

    MouseSizeBottomLeft = 7 ,//拉伸左下角

    MouseSizeBottomRight = 8 ,//拉伸右下角

    MouseDrag = 9 ,//鼠标拖动
  }
  #endregion


  #region 右键菜单绑定(用于删除控件)
  private void BindMenu()
  {
   System.Windows.Forms.MenuItem DeleItem=new MenuItem();
   DeleItem.Text="删除";
   DeleItem.Click+=new EventHandler(Control_Delete);

   System.Windows.Forms.MenuItem AddItem=new MenuItem();
   AddItem.Text="恢复";
   AddItem.Click+=new EventHandler(Control_Delete);

   DeleCM.MenuItems.Add(0,DeleItem);
   DeleCM.MenuItems.Add(1,AddItem);
  }

  #endregion

  Point p,p1;//定义被操作控件的位置

  EnumMousePointPosition m_MousePointPosition=new EnumMousePointPosition(); //被操作控件当前的鼠标操作状态

  public  void Control_MouseMove(object Sender, System.Windows.Forms.MouseEventArgs e)
  {
   sender=(System.Windows.Forms.Control)Sender;
   if(e.Button==MouseButtons.Left)
   {
    switch(m_MousePointPosition)
    {
       case EnumMousePointPosition.MouseDrag:
       sender.Location = new Point(sender.Left + e.X - p.X, sender.Top + e.Y - p.Y);
      break;

       case EnumMousePointPosition.MouseSizeBottom:
       sender.Size = new Size(sender.Width, sender.Height + e.Y - p1.Y);
       p1 = new Point(e.X, e.Y) ;  //'记录光标拖动的当前点
      break;

       case EnumMousePointPosition.MouseSizeBottomRight:
      sender.Size = new Size(sender.Width + e.X - p1.X, sender.Height + e.Y - p1.Y);
       p1 = new Point(e.X, e.Y);
      break;

       case EnumMousePointPosition.MouseSizeRight:
       sender.Size = new Size(sender.Width + e.X - p1.X, sender.Height);
       p1 = new Point(e.X, e.Y);
      break;

       case EnumMousePointPosition.MouseSizeTop:
       sender.Location = new Point(sender.Left, sender.Top + (e.Y - p.Y));
       sender.Size = new Size(sender.Width, sender.Height - (e.Y - p.Y));
      break;

       case EnumMousePointPosition.MouseSizeLeft:
       sender.Location = new Point(sender.Left + e.X - p.X, sender.Top);
       sender.Size = new Size(sender.Width - (e.X - p.X), sender.Height);
      break;

       case EnumMousePointPosition.MouseSizeBottomLeft:
       sender.Location = new Point(sender.Left + e.X - p.X, sender.Top);
       sender.Size = new Size(sender.Width - (e.X - p.X), sender.Height + e.Y - p1.Y);
       p1 = new Point(e.X, e.Y);
      break;

       case EnumMousePointPosition.MouseSizeTopRight:
       sender.Location = new Point(sender.Left, sender.Top + (e.Y - p.Y));
       sender.Size = new Size(sender.Width + (e.X - p1.X), sender.Height - (e.Y - p.Y));
       p1 = new Point(e.X, e.Y);
      break;

       case EnumMousePointPosition.MouseSizeTopLeft:
       sender.Location = new Point(sender.Left + e.X - p.X, sender.Top + (e.Y - p.Y));
       sender.Size = new Size(sender.Width - (e.X - p.X), sender.Height - (e.Y - p.Y));
      break;
    }
   }
   else
   {
    m_MousePointPosition=ReEMPP(sender.Size,e);

    switch( m_MousePointPosition )  //改变光标
    {
       case EnumMousePointPosition.MouseSizeNone:

       sender.Cursor = Cursors.Arrow       ; //箭头
      break;

       case EnumMousePointPosition.MouseDrag:

       sender.Cursor = Cursors.SizeAll     ; //四方向
      break;

       case EnumMousePointPosition.MouseSizeBottom:

       sender.Cursor = Cursors.SizeNS      ; //南北
      break;

       case EnumMousePointPosition.MouseSizeTop:

       sender.Cursor = Cursors.SizeNS      ; //南北
      break;

       case EnumMousePointPosition.MouseSizeLeft:

       sender.Cursor = Cursors.SizeWE      ; //东西
      break;

       case EnumMousePointPosition.MouseSizeRight:


      sender.Cursor = Cursors.SizeWE      ; //东西
      break;

       case EnumMousePointPosition.MouseSizeBottomLeft:

       sender.Cursor = Cursors.SizeNESW    ; //东北到南西
      break;

       case EnumMousePointPosition.MouseSizeBottomRight:

       sender.Cursor = Cursors.SizeNWSE    ; //东南到西北
      break;

       case EnumMousePointPosition.MouseSizeTopLeft:

       sender.Cursor = Cursors.SizeNWSE    ; //东南到西北
      break;

       case EnumMousePointPosition.MouseSizeTopRight:

       sender.Cursor = Cursors.SizeNESW    ; //东北到南西
      break;
     }
   }
  }


  public void Control_MouseLeave(object Sender, System.EventArgs e)
  {
   sender=(System.Windows.Forms.Control)Sender;
   if(sender.ContextMenu!=null) //卸载右键菜单
   {
    //    sender.ContextMenu.Dispose();
   }
   m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
   SetSenderBG(Color.White);//控件不再被控制时背景色为白色
    sender.Cursor = Cursors.Arrow;
   
  }


  public void Control_MouseDown(object Sender, System.Windows.Forms.MouseEventArgs e)
  {
   sender=(System.Windows.Forms.Control)Sender;

   if(sender.ContextMenu==null) //绑定右键菜单
   {
    if(DeleCM.MenuItems.Count==0)
    {
     BindMenu();
    }
    sender.ContextMenu=DeleCM;
   }
   SetSenderBG(Color.Yellow);//设定当前被操作控件的背景为黄色
   p=new Point(e.X,e.Y);
   p1=new Point(e.X,e.Y);
  }


  #region 控件删除事件的执行过程
  public void Control_Delete(object Sender, System.EventArgs e)
  {
   System.Windows.Forms.MenuItem it=(System.Windows.Forms.MenuItem)Sender;
   switch(it.Text)
   {
    case "删除":
     sender.Visible=false;
     break;
    case "恢复":
     sender.Visible=true;
     sender.BackColor=Color.White;//
     break;
   }
  }

  #endregion

  #region 设定控件背景色
  private void SetSenderBG(Color ColorValue) //设置控件背景色
  {
   if(sender.BackColor!=Color.Red)
   {
    sender.BackColor=ColorValue;
   }
  }

  #endregion

  private static EnumMousePointPosition ReEMPP(System.Drawing.Size size, System.Windows.Forms.MouseEventArgs e)
  {
   int Band = 10;
   if( e.X >= -1 * Band && e.X <= size.Width && e.Y >= -1 * Band && e.Y <= size.Height)
   {
    if (e.X < Band )
    {
     if (e.Y < Band )
     {
      return EnumMousePointPosition.MouseSizeTopLeft;
     }
     else
     {
      if(e.Y >( -1 * Band+size.Height))
       return EnumMousePointPosition.MouseSizeBottomLeft;
      else
       return EnumMousePointPosition.MouseSizeLeft;
     }
    }

    if (e.X > -1 * Band + size.Width )
    {
     if (e.Y < Band )
      return EnumMousePointPosition.MouseSizeTopRight;
     else if (e.Y > -1 * Band + size.Height)
      return EnumMousePointPosition.MouseSizeBottomRight;
     else
      return EnumMousePointPosition.MouseSizeRight;
    }
    else
    {
     if (e.Y < Band)
      return EnumMousePointPosition.MouseSizeTop;
     else if (e.Y > -1 * Band + size.Height)

      return EnumMousePointPosition.MouseSizeBottom;
     else
      return EnumMousePointPosition.MouseDrag;
    }
   }
   else
   {
    return EnumMousePointPosition.MouseSizeNone;
   }

  }

 

  #region //调整删除的控件是否可见
  public void DisDeleted(bool flag) 
  {
    foreach (Control ct in senderForm.Controls)
    {
     if(ct.Visible)
     {
      if(!flag)
      {
       if(ct.BackColor==Color.Red)
       {
        ct.Visible=false;
       }
      }
     }
     else
     {
      if(flag)
      {
       ct.Visible=true;
       ct.BackColor=Color.Red;
      }
     }
    }
  }
  #endregion
 

  public void StartControl()//初始化设置
  {
   foreach (Control ct in senderForm.Controls)
   {
    ct.MouseMove+=new MouseEventHandler(Control_MouseMove);
    ct.MouseLeave+=new EventHandler(Control_MouseLeave);
    ct.MouseDown+=new MouseEventHandler(Control_MouseDown);
   }
   senderForm.BackColor=Color.SkyBlue;
  }

  private void databind()
  {
   try
   {
  
    DataSet DS_Control=new DataSet();
    DS_Control.ReadXml(FilePath+"//FormControl.xml");
    DataView dv=new DataView(DS_Control.Tables["ControlTB"]);
    foreach (Control ct in this.Controls)
    {
     foreach(DataRowView drv in dv)
     {
      if(drv["ControlID"].ToString()==ct.Name)
      {
       ct.Width=int.Parse(drv["Width"].ToString());
       ct.Height=int.Parse(drv["Height"].ToString());
       Point p=new Point(int.Parse(drv["LocationX"].ToString()),int.Parse(drv["LocationY"].ToString()));
       ct.Location=p;
       ct.Visible=bool.Parse(drv["Visible"].ToString());
      }
     }
    }
   }
   catch(Exception e)
   {
    MessageBox.Show(e.ToString());
   }
  }

  #region //结束设置
  public void EndControl()
  {
   SaveData();
   foreach (Control ct in senderForm.Controls)
   {
    ct.MouseMove-=new MouseEventHandler(Control_MouseMove);
    ct.MouseLeave-=new EventHandler(Control_MouseLeave);
    ct.MouseDown-=new MouseEventHandler(Control_MouseDown);
   }
   senderForm.BackColor=Color.Snow;
   DisDeleted(false);
  }
  #endregion

  #region //参数保存过程
  private void  SaveData()
  {
   if(System.IO.Directory.Exists(FilePath))
   {
    System.IO.Directory.CreateDirectory(FilePath);
   }
   DataSet DS_Control=new DataSet();
   DataTable dt =new DataTable();
   dt.TableName="ControlTB";
   DataRow dr;
   try
   {
    dt.Columns.Add(new DataColumn("FormID", typeof(Int32)));
    dt.Columns.Add(new DataColumn("ControlID", typeof(string)));
    dt.Columns.Add(new DataColumn("Width", typeof(Int32)));
    dt.Columns.Add(new DataColumn("Height", typeof(Int32)));
    dt.Columns.Add(new DataColumn("LocationX", typeof(Int32)));
    dt.Columns.Add(new DataColumn("LocationY", typeof(Int32)));
    dt.Columns.Add(new DataColumn("Visible", typeof(bool)));
    dt.Columns.Add(new DataColumn("ModiDateTime", typeof(DateTime)));
   }
   catch(Exception e)
   {
    MessageBox.Show(e.ToString());
   }

   foreach (Control ct in sender.Controls)
   {
    dr = dt.NewRow();
    dr[0] = 123;
    dr[1] = ct.Name;
    dr[2] = ct.Width;
    dr[3] =ct.Height;
    dr[4]=ct.Location.X;
    dr[5]=ct.Location.Y;
    dr[6]=ct.Visible;
    dr[7]=DateTime.Now;
    dt.Rows.Add(dr);
   }
   DS_Control.Tables.Add(dt);
   DS_Control.WriteXml(FilePath+"//FormControl.xml");
   DS_Control.Dispose();
  }
  #endregion


  ////////////////////////////////////////////////////////////////////////////
 }
}

C#窗体远程控制源代码

  • 2013年12月02日 21:29
  • 166KB
  • 下载

C#源代码—在windows窗体中添加一个名为lblShow的Lable控件

1.在windows窗体中添加一个名为lblShow的Lable控件

C#140显示窗体控制 源代码

  • 2016年12月17日 17:52
  • 56KB
  • 下载

鼠标移动痕迹 C#,Delphi,VB,VB.net四种语言版本(附窗体透明)-思路和源代码(3)

鼠标移动痕迹 C#,Delphi,VB,VB.net四种语言版本(附窗体透明)-思路和源代码(1) 鼠标移动痕迹 C#,Delphi,VB,VB.net四种语言版本(附窗体透明)-思路和源代码(2)...

窗体渐变程序(C#源代码编写)

  • 2008年12月03日 14:33
  • 188KB
  • 下载

c++转换c# 窗体的无缝控制——————socketv5代理测试

1.    对于外挂的程序一般的都很感兴趣,例如下面的一个小小的外挂,实现功能自动登录FTP :                   IntPtr hwnd = IntPtr.Zero; ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C#窗体控制源代码
举报原因:
原因补充:

(最多只允许输入30个字)