番外篇(一)保存打开过的窗体

附加版


图片记录打开过的窗体

介绍:当打开过窗体,并且关闭时,窗体在关闭前的截图保存在DataTabel保存,在另一个窗体进行显示,点击图片打开对应的窗体。

第一步:界面层(UIL)代码,在窗体的Load事件中绑定下拉框的数据

(1)窗体Load事件,对 dtImage 变量 添加列,在当前窗体自定义公共方法,保存图片,和生成图片框

截图效果(打开窗体时):

 

#region  公共变量
static private frm_OpenForm F_OpenForm = newfrm_OpenForm();
//当前窗体的实例化
DataTable dtImage = new DataTable(); //声明表保存图片
#endregion

 

        ///<summary>
        ///窗体加载事件
        ///</summary>
        ///<paramname="sender">当前事件执行者</param>
        ///<paramname="e">执行环境</param>
        privatevoid Form1_Load(objectsender, EventArgs e)
        {
            this.TopMost= true;
            this.ShowInTaskbar= false;//窗体不出现在Windows任务栏中
            dtImage.Columns.Add("FormName", typeof(string)); //添加列 名为 “FormName” 列数据类型 “string” 保存窗体命名空间+窗体名称
            dtImage.Columns.Add("FormText", typeof(string)); //添加列 名为 “FormText” 列数据类型 “string” 保存窗体显示文本
            dtImage.Columns.Add("Image", typeof(Image)); //添加列 名为 “Image” 列数据类型 “Image”         保存窗体截图
            dtImage.Columns.Add("PubF_Type", typeof(string)); //添加列 名为 “PubF_Type” 列数据类型 “string”保存在公共类声明的窗体名称
            this.MouseWheel+= new MouseEventHandler(PnltureArea_MouseWheel);
        }


        ///<summary>
        ///自定义方法赋值图片生成图片框
        ///</summary>
        ///<paramname="strName">窗体命名空间+窗体名称</param>
        ///<paramname="image">窗体截图</param>
        ///<param name="strText">窗体文本Text</param>
        ///<paramname="strPubF_Type">当前窗体的公共声明名称</param>
        public void ShowImage(stringstrName, Image image, string strText,stringstrPubF_Type)
        {
            if(!imgPictureStorage.Images.ContainsKey(strText))              //判断是否不存在 如果存在的不执行方法内容
            {
                if(strName != "")             //参数不为空
                {
                    lblPrompt.Text = "打开过的窗体";
                    dtImage.Rows.Add();  //添加行
                   dtImage.Rows[dtImage.Rows.Count - 1]["FormName"]= strName;  //保存窗体命名空间+窗体名称
                   dtImage.Rows[dtImage.Rows.Count - 1]["FormText"]= strText;  //保存窗体文本
                   dtImage.Rows[dtImage.Rows.Count - 1]["Image"]= image;       //保存窗体截图
                   dtImage.Rows[dtImage.Rows.Count - 1]["PubF_Type"]= strPubF_Type;   //保存在公共类声明的窗体名称
                   imgPictureStorage.Images.Add(strText, image);                //添加到本地 imageslist
                    imgPictureStorage.Images.Keys[imgPictureStorage.Images.Count- 1] = strText;  //赋值对应截图的 key
                }
                for(int intCount = 0, pbX = 10, pbY = 0, lblX =50, lblY = 0; intCount < imgPictureStorage.Images.Count; intCount++)//循环生成图片(窗体图片)选择框
                {
                    pbY = 60 + intCount * 182;//设置PictureBox的 Y坐标
                    lblY = 35 + intCount * 182;//设置Label的 Y坐标
                    PictureBoxpb = new PictureBox()   //创建PictureBox控件放置图片
                    {
                        Size = newSize(150, 150) /*设置大小*/,
                        Name =imgPictureStorage.Images.Keys[intCount],
 //设置名称
                        Tag = intCount,                              
 //设置Tag
                        Location = new Point(pbX,pbY),            
    //设置坐标
                        BackgroundImage =imgPictureStorage.Images[intCount] //放置背景图
                    };
                    pb.MouseEnter += new EventHandler(pb_MouseEnter);
 //鼠标进入事件(窗体未最大化)
                    Labellbl = new Label() 
   //创建Label显示对于图片的窗体名称
                    {
                        Text =imgPictureStorage.Images.Keys[intCount].Trim(), //获取图片对于的名称
                        Location = new Point(lblX,lblY),                      //设置坐标
                        ForeColor = Color.FromArgb(0x14, 0x0E, 0xA6),           //设置文本颜色
                        BackColor = Color.Transparent                          //设置背景颜色为 透明
                    };
                   PnltureArea.Controls.Add(pb);
                   PnltureArea.Controls.Add(lbl);   
    //为窗体里 拖出来的 Panel 控件  添加控件pb 和 lbl
                }
            }
        }
 


 

(2)鼠标移到对应的图片框里,图片放大

 

截图效果(鼠标移到图片上,放大):

 

        ///<summary>
        ///鼠标进入(小图标)
        ///</summary>
        ///<paramname="sender"></param>
        ///<paramname="e"></param>
        voidpb_MouseEnter(object sender, EventArgs e)
        {
            PictureBoxpb = sender as PictureBox;   
      //把触发事件的对象转换为对象
            LittleAreamy = new LittleArea((Image)dtImage.Rows[(int)pb.Tag]["Image"], dtImage.Rows[(int)pb.Tag]["FormName"].ToString().Trim(),this, dtImage.Rows[(int)pb.Tag]["PubF_Type"].ToString().Trim());
//窗体传值  ,newLittleArea(对应图片,窗体名称,当前窗体) ,
LittleArea 为装放大后的图片
            my.Show();
            my.Location = new Point(this.Location.X + pb.Location.X - 75, this.Location.Y + pb.Location.Y - 75);//显示的窗体在原图片的中心显示变大
        }
 


 

(3)LittleArea窗体,该窗体时放大图片

截图效果:

窗体介绍:

窗体FormBorderStyle属性: 设置为 None

一个在窗体停靠的PictureBox

 

       #region 变量
        ImageImg;  //图片
        objectobjFormName = null;//窗体名称
        frm_OpenFormfrm_S;  //保存图片的窗体
        intintValue = 0; //记录上一个滚动条的值
        stringstrPunF_Type = "";//声明字符串保存窗体在公共类声明的窗体名称
        #endregion


 

构造窗体函数:

///<summary>
        ///构造窗体函数
        ///</summary>
        ///<paramname="img">对应图片</param>
        ///<paramname="strformname">窗体名称</param>
        ///<paramname="frm_s">窗体</param>
        ///<paramname="strPubF_Type">声明字符串保存窗体在公共类声明的窗体名称</param>
        publicLittleArea(Image img, string strformname, frm_OpenFormfrm_s,string strPubF_Type)
        {
            frm_S = frm_s;
            Img = img;
            objFormName = strformname;
            strPunF_Type = strPubF_Type;
            InitializeComponent();
        }


 

 

窗体加载事件:

         ///<summary>
        ///窗体加载事件
        ///</summary>
        ///<paramname="sender"></param>
        ///<paramname="e"></param>
        privatevoid Form1_Load(objectsender, EventArgs e)
        {
            this.TopMost= true;//设置为最顶级窗体
            this.ShowInTaskbar= false;//窗体不出现在Windows任务栏中
            pictureBox1.BackgroundImage =Img;  //为当前
            this.MouseWheel+= new MouseEventHandler(PnltureArea_MouseWheel);//在鼠标滚轮并且控件有焦点时发生
        }




鼠标滚轮事件:

  ///<summary>
        ///鼠标滚轮事件
        ///</summary>
        ///<paramname="sender"></param>
        ///<paramname="e"></param>
        privatevoid PnltureArea_MouseWheel(object sender, MouseEventArgse)
        {
            PointmousePoint = new Point(e.X,e.Y);  //获取鼠标 X 、 Y坐标
            mousePoint.Offset(this.Location.X, this.Location.Y);//获取鼠标位置
            if(frm_S.PnltureArea.RectangleToScreen(frm_S.PnltureArea.DisplayRectangle).Contains(mousePoint))  //判断是否在控件里
            {
                if(e.Delta > 0)
                {
                   frm_S.PnltureArea.AutoScrollPosition = newPoint(0,frm_S.PnltureArea.VerticalScroll.Value - 50);  //向上滑
                }
                else
                {
                    frm_S.PnltureArea.AutoScrollPosition= new Point(0,frm_S.PnltureArea.VerticalScroll.Value + 50);  //向下滑
                }
                if(frm_S.PnltureArea.VerticalScroll.Value != 0)  //进度条的值 最小时 不执行
                {
                    intValue =frm_S.PnltureArea.VerticalScroll.Value;
                   frm_S.PnltureArea.AutoScrollPosition = newPoint(0,frm_S.PnltureArea.VerticalScroll.Value + 1);//试 + 1 判断是否进度条已到最大值
                    if(intValue != frm_S.PnltureArea.VerticalScroll.Value)
                    {
                        if (e.Delta > 0)
                        {
                            this.Location = new Point(this.Location.X,this.Location.Y + 50);   //向下滑
                        }
                        else
                        {
                            this.Location = new Point(this.Location.X,this.Location.Y - 50);   //向上滑
                        }
                    }
                }
            }
            else
            {
                this.Close();//关闭窗体
            }
        }


 

 

鼠标离开PictureBox事件:

        ///<summary>
        ///鼠标离开PictureBox事件
        ///</summary>
        ///<paramname="sender"></param>
        ///<paramname="e"></param>
        privatevoid pictureBox1_MouseLeave(object sender, EventArgse)
        {
            this.Close();       //关闭
        }


 

 

显示界面

       #region 显示界面
        privatevoid pictureBox1_Click(objectsender, EventArgs e)//点击pictureBox1事件
        {
            CreateForm(objFormName.ToString(), "2用友T6-ERP");
        }


        ///<summary> 
        ///反射动态调用窗体,动态实例化窗体的方法
        ///</summary> 
        ///<paramname="strName">窗体的类名</param>
        ///<paramname="AssemblyName">窗体所在类库的名称</param>
        public void CreateForm(stringstrName, string AssemblyName)
        {
            stringpath = AssemblyName;//窗体的 类名+命名空间+名称
            stringname = strName; //类的名字
            if(FormExample() == null)
            {
                Formdoc = (Form)Assembly.Load(path).CreateInstance(name);
                doc.Show();  //显示窗体
            }
        }

        public object FormExample() //返回公共参数的实例 判断是否打开窗体
        {
            PublicStaticFormpForm = new PublicStaticForm();//声明方法
            pForm.strVariable = strPunF_Type;//公共类 pForm 对象 下的 公共字段
            ReturnpForm.GetType().GetField(pForm.strVariable).GetValue(pForm);//获取字段下的值 返回
        }
        #endregion


(4) PublicStaticForm公共类

 

public class PublicStaticForm
    {
        ///<summary>
        ///获取当前类的变量名称
        ///</summary>
        public string strVariable = "";
 
        ///<summary>
        ///登陆窗体
        ///</summary>
        public static frm_LoginPubLogin;
 
        ///<summary>
        ///主界面
        ///</summary>
        public static frm_MainpubMain;
 
        ///<summary>
        ///悬浮界面
        ///</summary>
        public static frm_OpenFormpubfrm_OpenForm;
 
        ///<summary>
        ///放大窗体
        ///</summary>
        public static LoadBearingFormpuLoadBearingForm;
 
….//继续添加窗体变量,记录变量的值,为了防止窗体再次打开
}
 


 

 

(5)放大窗体,利用承载父容器的方法,先设计另一个窗体LoadBearingForm

承载的父窗体LoadBearingForm的代码

截图效果(设计效果):

界面控件为一个 Panel 容器。

        ///<summary>
        ///窗体关闭前发生的事件
        ///</summary>
        ///<paramname="sender"></param>
        ///<paramname="e"></param>
        privatevoid LoadBearingForm_FormClosing(object sender, FormClosingEventArgse) 
        {
            frm_OpenForm.Instance().Parent= null;                              //设置父容器无
            frm_OpenForm.Instance().TopLevel= true;                            //为顶级窗体
            frm_OpenForm.Instance().Size= new Size(177,700);                  //设置大小
            frm_OpenForm.Instance().Location= new Point(1180,5);              //设置坐标
           
            frm_OpenForm.Instance().pnlTitle.Visible= true;                    //显示放大按钮
 
            frm_OpenForm.Instance().PnltureArea.Controls.Clear();//清除放大的图片
            frm_OpenForm.Instance().ShowImage("", null,"","");  //显示图片列表
        }

点击放大按钮,窗体放大

截图效果(点击放大按钮)窗体变大,用其他窗体作父容器

截图效果(放大窗体时):

 

private voidpanel_Magnify_Click(object sender, EventArgs e)    //放大按钮
        {
            PublicStaticForm.puLoadBearingForm= new LoadBearingForm();
 //放大时,显示 该窗体 承载 本窗体
            PublicStaticForm.puLoadBearingForm.Show();
            this.TopLevel= false;  //不为顶级窗体
            this.Size= new Size(PublicStaticForm.puLoadBearingForm.Size.Width -20, PublicStaticForm.puLoadBearingForm.Size.Height- 50);//设置大小
            this.Parent= PublicStaticForm.puLoadBearingForm;//设置父容器
            this.Location= new Point(8,33);      //设置坐标
            this.BackgroundImageLayout= ImageLayout.Stretch;  //放大背景图片
            pnlTitle.Visible = false;            //隐藏放大按钮
 
            PnltureArea.Controls.Clear();  //清除控件集合
 
            for(int intCount = 0, pbX = 100, pbY = 0, lblX =380, lblY = 0; intCount < dtImage.Rows.Count; intCount++) //重新布局大图
            {
                intRowsCount = intCount / 2;
                if(intCount % 2 == 0)
                {
                    pbX = 80;
                    lblX = 310;
                }
                else
                {
                    pbX += 680;
                    lblX += 680;
                }
                pbY = 30 + RowsCount * 600;
                lblY = 5 + RowsCount * 600;
                PictureBoxpb = new PictureBox()
                {
                    Size = new Size(500, 500)/*设置大小*/,
                    Name =dtImage.Rows[intCount]["FormText"].ToString(),
                    Tag = intCount,
                    Location = new Point(pbX,pbY),
                    BackgroundImage =(Image)dtImage.Rows[intCount]["Image"],
                    BackgroundImageLayout=ImageLayout.Stretch
                };
                pb.MouseEnter += new EventHandler(Mpb_MouseEnter);
                Labellbl = new Label()
                {
                    Text =dtImage.Rows[intCount]["FormText"].ToString(),
                    Location = new Point(lblX,lblY),
                    ForeColor = Color.FromArgb(0x14, 0x0E, 0xA6),
                    BackColor = Color.Transparent,
                    Font = new Font("华文行楷", 15.0f)
                };
                PnltureArea.Controls.Add(pb);
                PnltureArea.Controls.Add(lbl);
            }
        }
 


 

鼠标移进图片,图片得再需要放大

截图效果(放大窗体鼠标移到图片上时并且点击图片打开对应窗体):

 

        ///<summary>
        ///鼠标进入(大图标)
        ///</summary>
        ///<param name="sender"></param>
        ///<paramname="e"></param>
        voidMpb_MouseEnter(object sender, EventArgs e)
        {
            PictureBoxpb = sender as PictureBox;          //把触发事件的对象转换为对象
            LittleAreamy = new LittleArea((Image)dtImage.Rows[(int)pb.Tag]["Image"], dtImage.Rows[(int)pb.Tag]["FormName"].ToString().Trim(),this, dtImage.Rows[(int)pb.Tag]["PubF_Type"].ToString().Trim());
            my.Show();
            my.Size = newSize(700,600);  //显示比图标大的图片
            my.Location = new Point(this.Location.X + pb.Location.X - 100, this.Location.Y + pb.Location.Y - 50);
        }
 


 

 

(6)需要保存在此 显示条里的窗体,写上代码

截图效果(打开并且关闭其他窗体时):

 

 

窗体Load事件:

PublicStaticForm.pubfrm_PurchaseOddNumbers = newfrm_PurchaseOddNumbers();
//实例化当前窗体的公共变量 说明已经打开了窗体 ,不需要打开


窗体FormClosing窗体关闭前事件:         

        #region 窗体关闭前事件
        privatevoid frm_PurchaseOddNumbers_FormClosing(object sender, FormClosingEventArgse)
        {
            PublicStaticMothd.FrmDraw(this.Width, this.Height,this.Location.X, this.Location.Y,this.Size, this.CompanyName+ "." + this.Name,this.Text, "pubfrm_PurchaseOddNumbers");
            //对窗体截图    公共类.公共方法(窗体宽度,窗体高度,窗体对应屏幕X坐标,窗体对应Y坐标,窗体大小,窗体命名空间.窗体名称,窗体显示文本,本窗体在公共类声明的窗体类型)
            PublicStaticForm.pubfrm_PurchaseOddNumbers= null; //让变量为空
        }
        #endregion

PublicStaticMothd 类里面的方法 FrmDraw

        #region 对窗体进行截图
        ///<summary>
        ///对窗体进行截图
        ///</summary>
        ///<paramname="intFrm_Width">窗体宽度</param>
        ///<paramname="intFrm_Height">窗体高度</param>
        ///<paramname="intLocationX">窗体X坐标</param>
        ///<paramname="intLocationY">窗体Y坐标</param>
        ///<paramname="Frm_Size">窗体大小</param>
        ///<paramname="strFrm_Name">窗体命名空间Namespace</param>
        ///<paramname="strFrm_Text">窗体Text</param>
        ///<paramname="strPubF_Type">窗体在公共类声明的窗体名称</param>
        public static void FrmDraw(int intFrm_Width, intintFrm_Height, int intLocationX, int intLocationY, System.Drawing.Size Frm_Size,
            stringstrFrm_Name, string strFrm_Text, string strPubF_Type)
        {
            Imageimg = new Bitmap(intFrm_Width,intFrm_Height);  //生成一张 宽为 intFrm_Width ,高为 intFrm_Height 的图片
            Graphicsg = Graphics.FromImage(img);   //对图片GDI+封装
            g.CopyFromScreen(new System.Drawing.Point(intLocationX,intLocationY), new System.Drawing.Point(0, 0), Frm_Size);//对调用的窗体进行截图 放到 img 变量里
            IntPtrdc = g.GetHdc();
            g.ReleaseHdc(dc);
            _2用友T6_ERP.FormDisplayer.frm_OpenForm.Instance().ShowImage(strFrm_Name,img, strFrm_Text, strPubF_Type);  //调用 frm_OpenForm 窗体的 自定义公共方法 ShowImage
            //ShowImage(窗体名称Name, 窗体截图, 窗体文本Text,当前窗体的公共声明名称)
        }
        #endregion


完成上面步骤后,当窗体写到第(6)步时,窗体关闭前对窗体进行截图,显示条就自动生成图片,点击图片,对应打开窗体。附加文件,请下载我上传的资源!

(注:某些方法从网上找来修改后使用,并非全部原创!)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值