Class for structure storage and window location size record

Source: Internet
Author: User

 

After the test, I took a break and recently wrote a Data encryption software called Privacy Data Safe. This is not a simple small software, but a very powerful software, at present, it is still in the preliminary design phase, but a successful software is always considerate in details, such as recording the location and size of the last closed window, and so on, however, there must be a degree of technical gold plating, and the pursuit of perfection will inevitably be counterproductive. It is better to encapsulate these things together for use.

 

Let's not talk about it. Let's take a look at this code first, which is actually a combination of serialization and file read/write.

Namespace PDSafe. Base

{

Public class Setting

{

/// <Summary>

/// Serialize the object into a byte array

/// </Summary>

Public static byte [] SerializeObject (object obj)

{

If (obj = null)

Return null;

MemoryStream MS = new MemoryStream ();

BinaryFormatter formatter = new BinaryFormatter ();

Formatter. Serialize (MS, obj );

Ms. Position = 0;

Byte [] bytes = new byte [ms. Length];

Ms. Read (bytes, 0, bytes. Length );

Ms. Close ();

Return bytes;

}

 

/// <Summary>

/// Deserialize the byte array into an object

/// </Summary>

Public static object DeserializeObject (byte [] bytes)

{

Object obj = null;

If (bytes = null)

Return obj;

MemoryStream MS = new MemoryStream (bytes );

Ms. Position = 0;

BinaryFormatter formatter = new BinaryFormatter ();

Try

{

Obj = formatter. Deserialize (MS );

}

Catch {obj = null ;}

Ms. Close ();

Return obj;

}

 

Public static bool Save (string path, object value, bool isCeranew)

{

// Create a file if it does not exist

FileStream fs;

If ((! File. Exists (path) & isCeranew)

{

Try

{

Fs = File. Create (path );

}

Catch

{

Return false;

}

}

// Enable it if it exists

Else

{

Try

{

Fs = File. Open (path, FileMode. Open, FileAccess. Write );

}

Catch

{

Return false;

}

 

}

// Write an object

Byte [] buffer = SerializeObject (value );

 

Try

{

For (long I = 0; I <buffer. LongLength; I ++)

Fs. WriteByte (buffer [I]);

}

Catch

{

Return false;

}

Fs. Close ();

Return true;

}

 

Public static object Read (string path)

{

FileStream fs;

Try

{

Fs = File. OpenRead (path );

}

Catch

{

Return null;

}

 

// Read the cache

StreamReader sreader = new StreamReader (fs );

String str = sreader. ReadToEnd ();

Fs. Close ();

Sreader. Close ();

// Analyze content

Byte [] buffer = Encoding. Default. GetBytes (str );

Return DeserializeObject (buffer );

}

[Serializable]

Public struct FormSizeandLocation

{

Public int SizeW;

Public int SizeH;

Public int LocationX;

Public int LocationY;

Public int Style;

}

Private static Setting. FormSizeandLocation fsp = new Setting. FormSizeandLocation ();

Public static void AddRenewFormSizeControl (Form form)

{

Form. FormClosing + = new FormClosingEventHandler (FormcloseEvent );

Form. Load + = new EventHandler (FormloadEvent );

}

Private static void FormcloseEvent (object sender, EventArgs e)

{

Form form = (Form) sender;

Switch (form. WindowState)

{

Case FormWindowState. Maximized:

Fsp. Style = 2;

 

Fsp. SizeW = form. Width;

Fsp. SizeH = form. Height;

Fsp. LocationX = form. Location. X;

Fsp. LocationY = form. Location. Y;

Break;

Case FormWindowState. Minimized:

Fsp. Style = 1;

Break;

Case FormWindowState. Normal:

Fsp. Style = 0;

 

Fsp. SizeW = form. Width;

Fsp. SizeH = form. Height;

Fsp. LocationX = form. Location. X;

Fsp. LocationY = form. Location. Y;

Break;

}

Setting. Save (Directory. GetCurrentDirectory () + @ "\" + "Location. set", fsp, true );

}

Private static void FormloadEvent (object sender, EventArgs e)

{

Form form = (Form) sender;

Object result = Setting. Read (Directory. GetCurrentDirectory () + @ "\" + "Location. set ");

If (result! = Null)

{

Fsp = (Setting. FormSizeandLocation) result;

Switch (fsp. Style)

{

Case 2:

Form. WindowState = FormWindowState. Maximized;

Break;

Default:

Form. WindowState = FormWindowState. Normal;

Break;

}

Form. Left = fsp. LocationX;

Form. Top = fsp. LocationY;

Form. Size = new Size (fsp. SizeW, fsp. SizeH );

}

}

}

}

 

The basic function is to save data of the struct type.

Bool Save (filePath, value, true );

There are also files that read the stored data and read from them. This struct is packed and all you need to do is unpack it.

Object result = Save (filePath, the data instance to be saved, true)

If (result! = Null) // check whether the file exists and is read successfully

...........

 

Combining these two functions, can you record the location of the window and the size of the window? Of course, you can declare a struct to save the size, location, and status.

• [Serializable]

• Public struct FormSizeandLocation

•{

• Public int SizeW;

• Public int SizeH;

• Public int LocationX;

• Public int LocationY;

• Public int Style;

•}

Then save and set. Lines 10-of the Code process it. How does it work?

• Ask the user to provide a window instance

• Subscribe to instance Load and Closing events

• Read the saved file in the load event and change the location and size of the instance.

• Save the size and position in the closing event

AddRenewFormSizeControl (this );

// Only one code is required. It must be written after the InitializeComponent function. Cannot be written in the load event

 

Note that the saved file is a working path + Location. set. You can also rewrite the file by yourself.

 

If you want to know about it, you can see how it works. If you want to use it, you don't have to understand how it works.

No technical skills ....

 

From A Dream

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.