实现动态套打

using System;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;


public class Form1 : System.Windows.Forms.Form
  {
   
   private string path;
   public DataRowView SourceRow;
   private Button[] Buttons;
   int num;

         private System.Drawing.Printing.PrintDocument printDocument1;
         private System.Windows.Forms.Panel panel1;
   private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
   
   Form1()
   {
    InitializeComponent();
    printDocument1.PrintPage+=new PrintPageEventHandler(printDocument1_PrintPage);

   }
   /// <summary>
   /// 注意参数num和drv的列数之间的关系
   /// </summary>
   /// <param name="num">控件个数</param>
   /// <param name="drv">DataRowView</param>
   public Form1(int num,DataRowView drv):this()
   {
    this.num=num;
    this.SourceRow=drv;
    Buttons=new Button[num];
    if(num==12)
    path=Application.StartupPath+"\\zuobiao.txt";
    else
    path=Application.StartupPath+"\\zuobiao2.txt";
   }
   
   enum EnumMousePointPosition
   {
    MouseSizeNone = 0,
    MouseSizeRight = 1,
    MouseSizeLeft = 2,
    MouseSizeBottom = 3,
    MouseSizeTop = 4,
    MouseSizeTopLeft = 5,
    MouseSizeTopRight = 6,
    MouseSizeBottomLeft = 7,
    MouseSizeBottomRight = 8,
    MouseDrag = 9
   }
   EnumMousePointPosition m_MousePointPosition;
   Point p;
 private System.Windows.Forms.ToolBar toolBar1;
 private System.Windows.Forms.ToolBarButton toolBarButton_Print;
 private System.Windows.Forms.ToolBarButton toolBarButton_Reset;
 private System.Windows.Forms.ToolBarButton toolBarButton_save;
         Point p1;

   private void MyMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
   {
    p = new Point(e.X, e.Y);
    p1 = new Point(e.X, e.Y);
   }

   private void MyMouseLeave(object sender, System.EventArgs e)
   {
    m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
    this.Cursor = Cursors.Arrow;
   }

   private void MyMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
   {
    if (e.Button == MouseButtons.Left)
    {
     if (m_MousePointPosition == EnumMousePointPosition.MouseDrag)
     {
      ((Button)sender).Location = new Point(((Button)sender).Left + e.X - p.X, ((Button)sender).Top + e.Y - p.Y);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeBottom)
     {
      ((Button)sender).Size = new Size(((Button)sender).Width, ((Button)sender).Height + e.Y - p1.Y);
      p1 = new Point(e.X, e.Y);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeBottomRight)
     {
      ((Button)sender).Size = new Size(((Button)sender).Width + e.X - p1.X, ((Button)sender).Height + e.Y - p1.Y);
      p1 = new Point(e.X, e.Y);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeRight)
     {
      ((Button)sender).Size = new Size(((Button)sender).Width + e.X - p1.X, ((Button)sender).Height);
      p1 = new Point(e.X, e.Y);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeTop)
     {
      ((Button)sender).Location = new Point(((Button)sender).Left, ((Button)sender).Top + (e.Y - p.Y));
      ((Button)sender).Size = new Size(((Button)sender).Width, ((Button)sender).Height - (e.Y - p.Y));
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeLeft)
     {
      ((Button)sender).Location = new Point(((Button)sender).Left + e.X - p.X, ((Button)sender).Top);
      ((Button)sender).Size = new Size(((Button)sender).Width - (e.X - p.X), ((Button)sender).Height);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeBottomLeft)
     {
      ((Button)sender).Location = new Point(((Button)sender).Left + e.X - p.X, ((Button)sender).Top);
      ((Button)sender).Size = new Size(((Button)sender).Width - (e.X - p.X), ((Button)sender).Height + e.Y - p1.Y);
      p1 = new Point(e.X, e.Y);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeTopRight)
     {
      ((Button)sender).Location = new Point(((Button)sender).Left, ((Button)sender).Top + (e.Y - p.Y));
      ((Button)sender).Size = new Size(((Button)sender).Width + (e.X - p1.X), ((Button)sender).Height - (e.Y - p.Y));
      p1 = new Point(e.X, e.Y);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeTopLeft)
     {
      ((Button)sender).Location = new Point(((Button)sender).Left + e.X - p.X, ((Button)sender).Top + (e.Y - p.Y));
      ((Button)sender).Size = new Size(((Button)sender).Width - (e.X - p.X), ((Button)sender).Height - (e.Y - p.Y));
     }
    }
    else
    {
     m_MousePointPosition = MousePointPosition(((Button)sender).Size, e);
     if (m_MousePointPosition == EnumMousePointPosition.MouseSizeNone)
     {
      this.Cursor = Cursors.Arrow;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseDrag)
     {
      this.Cursor = Cursors.SizeAll;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeBottom)
     {
      this.Cursor = Cursors.SizeNS;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeTop)
     {
      this.Cursor = Cursors.SizeNS;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeLeft)
     {
      this.Cursor = Cursors.SizeWE;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeRight)
     {
      this.Cursor = Cursors.SizeWE;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeBottomLeft)
     {
      this.Cursor = Cursors.SizeNESW;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeBottomRight)
     {
      this.Cursor = Cursors.SizeNWSE;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeTopLeft)
     {
      this.Cursor = Cursors.SizeNWSE;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeTopRight)
     {
      this.Cursor = Cursors.SizeNESW;
     }
    }
   }

   private EnumMousePointPosition MousePointPosition(Size Size, System.Windows.Forms.MouseEventArgs e)
   {
    const 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;
      }
     }
     else 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;
    }
   }


 
 private void InitializeComponent()
 {
  System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  this.toolBar1 = new System.Windows.Forms.ToolBar();
  this.toolBarButton_Print = new System.Windows.Forms.ToolBarButton();
  this.toolBarButton_Reset = new System.Windows.Forms.ToolBarButton();
  this.toolBarButton_save = new System.Windows.Forms.ToolBarButton();
  this.printDocument1 = new System.Drawing.Printing.PrintDocument();
  this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
  this.panel1 = new System.Windows.Forms.Panel();
  this.SuspendLayout();
  //
  // toolBar1
  //
  this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                     this.toolBarButton_Print,
                     this.toolBarButton_Reset,
                     this.toolBarButton_save});
  this.toolBar1.DropDownArrows = true;
  this.toolBar1.Location = new System.Drawing.Point(0, 0);
  this.toolBar1.Name = "toolBar1";
  this.toolBar1.ShowToolTips = true;
  this.toolBar1.Size = new System.Drawing.Size(440, 41);
  this.toolBar1.TabIndex = 0;
  this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
  //
  // toolBarButton_Print
  //
  this.toolBarButton_Print.Text = "打印预览";
  //
  // toolBarButton_Reset
  //
  this.toolBarButton_Reset.Text = "重置";
  //
  // toolBarButton_save
  //
  this.toolBarButton_save.Text = "保存";
  //
  // printPreviewDialog1
  //
  this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
  this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
  this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
  this.printPreviewDialog1.Document = this.printDocument1;
  this.printPreviewDialog1.Enabled = true;
  this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
  this.printPreviewDialog1.Location = new System.Drawing.Point(88, 88);
  this.printPreviewDialog1.MinimumSize = new System.Drawing.Size(375, 250);
  this.printPreviewDialog1.Name = "printPreviewDialog1";
  this.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty;
  this.printPreviewDialog1.Visible = false;
  //
  // panel1
  //
  this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
  this.panel1.Location = new System.Drawing.Point(0, 41);
  this.panel1.Name = "panel1";
  this.panel1.Size = new System.Drawing.Size(440, 260);
  this.panel1.TabIndex = 1;
  //
  // Form1
  //
  this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  this.ClientSize = new System.Drawing.Size(440, 301);
  this.Controls.Add(this.panel1);
  this.Controls.Add(this.toolBar1);
  this.Name = "Form1";
  this.Load += new System.EventHandler(this.Form1_Load);
  this.ResumeLayout(false);

 }

   

 

 private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
 {
  switch(this.toolBar1.Buttons.IndexOf(e.Button))
  {
    
   case 0:
    printPreviewDialog1.ShowDialog();
    //this.printDocument1.Print();
    break;
   case 1:
    RestoreDefaultPosition();
    break;
   case 2:
    SavePosition();
    break;

  }
 }

 void SavePosition()
 {
  using (StreamWriter sw = File.CreateText(path))
  {
   foreach(Button ll in this.Buttons )
   {
    sw.WriteLine(ll.Location.X.ToString()+','+ll.Location.Y.ToString()+','+ll.Width+','+ll.Height);
   }
     
  }    
 
 }

 void RestoreDefaultPosition()
 {
  
  if (File.Exists(path))
  {
    
   using (StreamReader sr = File.OpenText(path))
   {
    string s = "";
    string[] Zuobiao=new string[4];
    char[] c={','};
    for(int i=0;i<num;i++)
    {
     s = sr.ReadLine();
     Zuobiao=s.Split(c);
     Buttons[i].Location=new Point(int.Parse(Zuobiao[0]),int.Parse(Zuobiao[1]));
     Buttons[i].Width=int.Parse(Zuobiao[2]);
     Buttons[i].Height=int.Parse(Zuobiao[3]);

    }

   }

  }
   
 }

 private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
 {
  Rectangle rc=new Rectangle();
  foreach(Control ctrl in this.panel1.Controls)
  {
   Button lb=ctrl as Button;
   if(lb!=null)
   {
    //e.Graphics.DrawString(lb.Text,lb.Font,Brushes.Black,lb.Location.X,lb.Location.Y,StringFormat.GenericDefault);
    rc.X=lb.Location.X;
    rc.Y=lb.Location.Y;
    rc.Width=lb.Width;
    rc.Height=lb.Height;
    e.Graphics.DrawString(lb.Text,lb.Font,Brushes.Black,rc,StringFormat.GenericDefault);
   }
  }
 }

 private void Form1_Load(object sender, System.EventArgs e)
 {
  if(SourceRow==null)
  {
   this.Close();
  }
  Button more;
  for(int i=0;i<SourceRow.Row.Table.Columns.Count;i++)
  {
   //如果是时间类型分为3个
   if(Type.GetTypeCode(SourceRow[i].GetType())==TypeCode.DateTime)
   {
    for(int j=0;j<3;j++)
    {
     more = new Button();
     if(j==0)
     {
     more.Text=DateTime.Parse(isEmpty(SourceRow[i])).Year.ToString();
     Buttons[i]=more;
     }
     else if(j==1)
     {
     more.Text=DateTime.Parse(isEmpty(SourceRow[i])).Month.ToString();
     Buttons[num-3+j]=more;
     }
     else
     {
     more.Text=DateTime.Parse(isEmpty(SourceRow[i])).Day.ToString();
     Buttons[num-3+j]=more;
     }

     more.MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);
     more.MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);
     more.MouseLeave += new System.EventHandler(MyMouseLeave);
     this.panel1.Controls.Add(more);
     
    }
    continue;
   }

   more = new Button();
   more.Text=isEmpty(SourceRow[i]);
   more.Name=i.ToString();
   more.MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);
   more.MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);
   more.MouseLeave += new System.EventHandler(MyMouseLeave);
   this.panel1.Controls.Add(more);
   Buttons[i]=more;
  }
  RestoreDefaultPosition();

 }

 string isEmpty(object o)
 {
  string str1="";
  if(Convert.IsDBNull(o))
   return String.Empty;
  else
  {
   str1=o.ToString();
   return str1;
  }
   
 }
}
 

你可能感兴趣的:(动态)