用Metafile类记录绘图过程,并保存,在刷新时可重绘

解决方案 »

  1.   

    代码片断
    foreach(DataRowView drv in childNodes)

    string iFilter1="关系结点1='"+drv["任务模板id"].ToString()+"'";DataView conditionsOfThisNode=new DataView(this.dataSet1_1.task_condition_t,iFilter1,"关系结点1",DataViewRowState.CurrentRows);
    foreach(DataRowView drv2 in conditionsOfThisNode)

    Guid gid_1=(Guid)drv2["关系结点1"];
    Guid gid_2=(Guid)drv2["关系结点2"];
    drawLine(gh,gid_1,gid_2);}
    }
    }
    private void drawLine(Graphics gh,Guid g1,Guid g2)

    Point pt1=new Point();
    Point pt2=new Point();
    foreach(Control ctrl in this.groupBox1.Controls)
    {
    if((Guid)ctrl.Tag==g1)

    pt1.X=ctrl.Location.X+ctrl.Width/2;
    pt1.Y=ctrl.Location.Y+ctrl.Height/2;}
    if((Guid)ctrl.Tag==g2)
    { pt2.X=ctrl.Location.X+ctrl.Width/2;
    pt2.Y=ctrl.Location.Y+ctrl.Height/2;}
    }
    Pen myPen=new Pen(Color.Red);
    gh.DrawLine(myPen,pt1,pt2); } 本人新手,请详细说明。
      

  2.   

    Drawing the line in Paint event of GroupBox.Hope it could be help.
      

  3.   

    原作:TheAresusing System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace WindowsApplication1
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; private Point mousePos; // 记录鼠标位置
    private bool beginmove = false; // 记录是否可以移动  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.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.BackColor = System.Drawing.SystemColors.ActiveCaption;
    this.label1.Location = new System.Drawing.Point(16, 48);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(120, 168);
    this.label1.TabIndex = 0;
    this.label1.Text = "label1";
    // 
    // label2
    // 
    this.label2.BackColor = System.Drawing.SystemColors.Desktop;
    this.label2.Location = new System.Drawing.Point(232, 32);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(136, 192);
    this.label2.TabIndex = 1;
    this.label2.Text = "label2";
    this.label2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.label2_MouseUp);
    this.label2.MouseMove += new System.Windows.Forms.MouseEventHandler(this.label2_MouseMove);
    this.label2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.label2_MouseDown);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(520, 286);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.label2,
      this.label1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
    this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
    this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
    this.ResumeLayout(false); }
    #endregion public static void Main()
    {
    Application.Run(new Form1());
    } private void drawLine(PaintEventArgs pe,Point pt1,Point pt2)
    {
    Graphics g = pe.Graphics;
    //g.ResetClip();
    Pen p = new Pen(Color.Blue,1);
    g.DrawLine(p,pt1,pt2); } protected override void OnPaint(PaintEventArgs pe)
    {


    this.drawLine(pe,this.label1.Location + this.label1.Size ,this.label2.Location);
    base.OnPaint(pe);


    } private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {

    } private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    { } private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {

    } private void label2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    beginmove = true;//开始移动
    mousePos = new Point(e.X, e.Y); 
    } private void label2_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(beginmove)
    {
    int diffx,diffy;
    diffx = mousePos.X - e.X;
    diffy = mousePos.Y - e.Y;
    this.label2.Location = new Point(label2.Location.X-diffx,label2.Location.Y-diffy);

    Invalidate();
    // System.Windows.Forms.PaintEventArgs ex=null;
    // this.InvokePaint(this.label2,ex);
    } private void label2_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    beginmove = false;//停止移动 

    } }
    }