Troubleshooting of Windows Form initialization size records

Source: Internet
Author: User

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 !!

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.