[英]Saving the forms desktop location and size
I am using winforms and I want to save the desktop location and size of the form when the user changes either. 我正在使用winforms,并且当用户更改其中一个时,我想保存桌面的位置和表单的大小。 I found some useful code and put that in the form closing event and form load event to save and load the size and location respectively. 我找到了一些有用的代码,并将其放在表单关闭事件和表单加载事件中,以分别保存和加载大小和位置。
However, when the user directly shuts down the PC without closing the program first the changed size and location is not saved. 但是,当用户不先关闭程序就直接关闭PC时,更改的大小和位置不会保存。
Therefore I used the same code in the size changed and location changed events but it does not work and size and location are not changed when the program restarts. 因此,我在大小更改和位置更改事件中使用了相同的代码,但是它不起作用,并且在程序重新启动时大小和位置均未更改。
private void frmScopeStatus_SizeChanged(object sender, EventArgs e)
{
Application.UserAppDataRegistry.SetValue("WindowState", this.WindowState);
Application.UserAppDataRegistry.SetValue("WindowSizeH", this.Size.Height);
Application.UserAppDataRegistry.SetValue("WindowSizeW", this.Size.Width);
Application.UserAppDataRegistry.SetValue("LocationX", this.DesktopLocation.X);
Application.UserAppDataRegistry.SetValue("LocationY", this.DesktopLocation.Y);
}
private void frmScopeStatus_LocationChanged(object sender, EventArgs e)
{
Application.UserAppDataRegistry.SetValue("WindowState", this.WindowState);
Application.UserAppDataRegistry.SetValue("WindowSizeH", this.Size.Height);
Application.UserAppDataRegistry.SetValue("WindowSizeW", this.Size.Width);
Application.UserAppDataRegistry.SetValue("LocationX", this.DesktopLocation.X);
Application.UserAppDataRegistry.SetValue("LocationY", this.DesktopLocation.Y);
}
You can provide Application Settings data bindings of User
scope to store these values. 您可以提供User
范围的应用程序设置数据绑定来存储这些值。
Data > (Application Settins)
group 转到表单属性“ Data > (Application Settins)
组 Location
(and ClientSize
) property (scope User) 为Location
(和ClientSize
)属性添加数据绑定(范围为User) FormClosing
event save changed properties Properties.Settings.Default.Save();
在FormClosing
事件上,保存更改的属性Properties.Settings.Default.Save();
FormClosing
This will create for each user of your application file (at %SYSTEMDRIVE%/Users/{username}/AppData/Local/CompanyName/AppName
) with user settings, which will be applied when application starts: 这将为您的应用程序文件的每个用户(在%SYSTEMDRIVE%/Users/{username}/AppData/Local/CompanyName/AppName
)创建带有用户设置的应用程序,这些设置将在应用程序启动时应用:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<AppName.Properties.Settings>
<setting name="FormLocation" serializeAs="String">
<value>345, 234</value>
</setting>
<setting name="FormSize" serializeAs="String">
<value>458, 555</value>
</setting>
</AppName.Properties.Settings>
</userSettings>
</configuration>
BTW I think FormClosed
event is better for saving application settings. 顺便说一句,我认为FormClosed
事件更适合保存应用程序设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.