To let the system record the size and status of the main form when the application exits, adopt the Implementation Technology in WebMatrix: update the size and status of the form from the "Memory" when the form is constructed, when the form is closed, refresh the size of the form to "Memory:
Load "Memory ":
Try
{
// Set the initial position and size of the form.
String strInfo = this. _ applicationIdentity. GetSetting ("WindowMaximized ");
If (strInfo! = Null) & strInfo. Equals ("true "))
{
Base. WindowState = FormWindowState. Maximized;
}
Else
{
Base. WindowState = FormWindowState. Normal;
}
// Set the form size area
StrInfo = this. _ applicationIdentity. GetSetting ("WindowLeft", false );
If (strInfo! = Null) & (strInfo. Length! = 0 ))
{
Int iLeft = Convert. ToInt32 (strInfo );
StrInfo = this. _ applicationIdentity. GetSetting ("WindowTop", false );
Int iTop = Convert. ToInt32 (strInfo );
StrInfo = this. _ applicationIdentity. GetSetting ("required wwidth", false );
Int iWidth = Convert. ToInt32 (strInfo );
StrInfo = this. _ applicationIdentity. GetSetting ("Running wheight", false );
Int iHeight = Convert. ToInt32 (strInfo );
Base. Bounds = new Rectangle (iLeft, iTop, iWidth, iHeight );
}
Else
{
Base. Bounds = Screen. FromPoint (Cursor. Position). WorkingArea;
}
Base. StartPosition = FormStartPosition. Manual;
}
Catch (Exception err)
{
MessageBox. Show (this, err. Message );
}
Save "Memory ":
Try
{
// Save status information such as the Form Location
If (base. WindowState = FormWindowState. Maximized)
{
This. _ applicationIdentity. SetSetting ("WindowMaximized", "true ");
}
Else if (base. WindowState! = FormWindowState. Minimized)
{
This. _ applicationIdentity. SetSetting ("WindowMaximized", null );
Rectangle rectangle1 = base. Bounds;
This. _ applicationIdentity. SetSetting ("WindowLeft", Convert. ToString (rectangle1.Left ));
This. _ applicationIdentity. SetSetting ("WindowTop", Convert. ToString (rectangle1.Top ));
This. _ applicationIdentity. SetSetting ("descriwwidth", Convert. ToString (rectangle1.Width ));
This. _ applicationIdentity. SetSetting ("WindowHeight", Convert. ToString (rectangle1.Height ));
}
}
Catch (Exception)
{
}
A problem is found:
I want to write these two pieces of code in the base class, and the result is a problem: the size of the form in the normal state will not be effectively applied during loading, the reason for the analysis is that the construction of the base class is executed first, while that of the derived class is executed later. When designing the derived class, the derived class has the code to modify the form size during design.
It would be unrealistic to delete the code for modifying the form size (in InitializeComponent) in a derived class, so I thought of "One trick": processing the code when the form is HandleCreated, the size of the normal state is solved, but the form cannot be set to the maximum value during initialization, and it will always be normal.
Again !!