Home > Net >  Winform how to preserve the dynamically generated control position to file
Winform how to preserve the dynamically generated control position to file

Time:05-28

Winform how to preserve the dynamically generated control position to file

CodePudding user response:

https://bbs.csdn.net/topics/396830133
Take attributes to also go, take the basic (coordinates, the content of what), set up a class, serialized to a file, can read
 
using System;
using System.Collections.Generic;
Using System. Drawing;
Using System. IO;
Using System. The Runtime. Serialization;
Using System. The Runtime. Serialization. Json;
Using System. Windows. Forms;

The namespace Demo_1
{
//System. "reference" to add the Runtime. Serialization
Public partial class Form1: Form
{
[DataContract]
Public class Record
{
[DataMember]
Public string Obj_Type;//type
[DataMember]
Public Point Obj_Point;//position
[DataMember]
Public Size Obj_Size;//size
[DataMember]
Public string Obj_BackColor;//color
[DataMember]
Public string Obj_Content;//content

Public Record (string t, Point p, the Size s, c string, the string e)
{
Obj_Type=t;
Obj_Point=p;
Obj_Size=s;
Obj_BackColor=c;
Obj_Content=e;
}
}
Public static List Obj_Record=new List (a);

Public _click ()
{
InitializeComponent();

for (int i=0; I & lt; 3; I++)
{
The Label Label=new Label ()
{
Location=new Point (10 + I * 130, 10),
Size=new Size (100, 20),
Text="CSDN - 12345",
BackColor=Color. WhiteSmoke
};
This. Controls. The Add (label);
Obj_Record. Add (new Record (label. GetType (), ToString (), label, the Location, the label, the Size, ColorTranslator. ToHtml (label. BackColor), label, Text));

PictureBox picture=new PictureBox ()
{
Location=new Point (10 + I * 130, 40),
Size=new Size (20, 20),
Pimg. PNG Image=Bitmap. FromFile (" "),
BackColor=Color WhiteSmoke,
};
This. Controls. The Add (picture);
Obj_Record. Add (new Record (picture. GetType (), ToString (), picture, Location, picture, Size, ColorTranslator. ToHtml (label. BackColor), BitmapToBase64String (picture. The Image as Bitmap)));
}

String Serialization_Obj=ObjectToJson (Obj_Record);

The Button button_view=new Button ()
{
Location=new Point (10, living),
Text="view json,"
};
Button_view. Click +=(s, e)=& gt; {MessageBox. Show (Serialization_Obj); };//the serialized string, can be saved as a file
This. Controls. The Add (button_view);
////////////////////////////////////////////////////////////////////////
The Button button_clear=new Button ()//remove components, an array
{
Location=new Point (110, 90),
Text="removal of components,"
};
Button_clear. Click +=(s, e)=& gt;
{
Jmp:
Foreach (var item in this. Controls)
{
if (! (the item is the Button))
{
This. Controls. Remove item (as Control);
Goto Jmp.
}
}
Obj_Record. The Clear ();
};
This. Controls. The Add (button_clear);
////////////////////////////////////////////////////////////////////////
Button button_load=new Button ()//load component
{
Location=new Point (210, 90),
Text="load component",
};
Button_load. Click +=(s, e)=& gt;
{
Obj_Record=JsonToObject (Serialization_Obj);//load a string into object
The foreach (var item in Obj_Record)
{
If (item. Obj_Type. The Contains (" Label "))
{
The Label Label=new Label ()
{
Location=item Obj_Point,
Size=item Obj_Size,
Text=item Obj_Content,
BackColor=ColorTranslator. FromHtml (item. Obj_BackColor),
};
This. Controls. The Add (label);
}
If (item. Obj_Type. The Contains (" PictureBox "))
{
PictureBox picture=new PictureBox ()
{
Location=item Obj_Point,
Size=item Obj_Size,
Image=Base64StringToImage (item. Obj_Content),
BackColor=ColorTranslator. FromHtml (item. Obj_BackColor),
};
This. Controls. The Add (picture);
}
}
};
This. Controls. The Add (button_load);
}

//bitmap base64
Private string BitmapToBase64String Bitmap (BMP)
{
Try
{
MemoryStream ms=new MemoryStream ();
BMP. Save (ms, System. Drawing. Imaging. ImageFormat. Png);//Png image
Byte [] arr=new byte [Ms. Length];
Ms. Position=0;
Ms. Read (arr, 0, (int) Ms. Length);
Ms. Close ();
Return the Convert. ToBase64String (arr);
}
Catch
{
return null;
}
}

//base64 bitmap
Public static Bitmap Base64StringToImage (string inputStr)
{
Try
{
Byte [] arr=the Convert. FromBase64String (inputStr);
MemoryStream ms=new MemoryStream (arr);
Bitmap BMP=new Bitmap (ms);
Ms. Close ();
Return BMP.
}
Catch
{
return null;
}
}

//serialized as json
Public static string ObjectToJson (T T) where T: class
{
DataContractJsonSerializer formatter=new DataContractJsonSerializer (typeof (T));
Using (MemoryStream stream=new MemoryStream ())
{
The formatter. WriteObject (stream, t);
. String result=System. Text. Encoding UTF8. Get string (stream. The ToArray ());
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related