兄弟我刚刚开始在C#里面操纵Windows Form,问题估计很简单,请大家不吝赐教啊。
我实现了一个Form,需要记录Form的位置(DesktopLocation)和尺寸(Size),因此我就写了个配置文件记录Form的位置和尺寸。这是读写配置文件的类
class ProfileLoader
{
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
public static extern uint GetPrivateProfileInt(string section, string key, int def, string file);
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
public static extern bool WritePrivateProfileString(string section, string key, string value,string file);
}每次Form开始构建时根据上次Form Close时的位置和尺寸进行初始化,代码如下:
void InitializeUISettings()
{
String curpath = Application.StartupPath;
curpath += "\\GM.ini"; uint original_height = ProfileLoader.GetPrivateProfileInt("GMFormOption", "HEIGHT", 300, curpath);
uint original_width = ProfileLoader.GetPrivateProfileInt("GMFormOption", "WIDTH", 300, curpath);
uint original_x = ProfileLoader.GetPrivateProfileInt("GMFormOption", "X", 0, curpath);
uint origianl_y = ProfileLoader.GetPrivateProfileInt("GMFormOption", "Y", 0, curpath);
this.Size = new System.Drawing.Size((int)original_width, (int)original_height);
this.Location = new Point((int)original_x, (int)origianl_y);
}
每次Form在Closed时,我记录Form最终的位置和尺寸,方法如下:
void StoreUISettings()
{
String curpath = Application.StartupPath;
curpath += "\\GM.ini";
ProfileLoader.WritePrivateProfileString("GMFormOption", "HEIGHT", "" + this.Height, curpath);
ProfileLoader.WritePrivateProfileString("GMFormOption", "WIDTH", "" + this.Width, curpath);
ProfileLoader.WritePrivateProfileString("GMFormOption", "X", ""+this.Location.X, curpath);
ProfileLoader.WritePrivateProfileString("GMFormOption", "Y", ""+this.Location.Y, curpath);
}
但是,奇迹发生了,我能够完整地复原上次关闭Form的尺寸,但是位置总是和上次关闭的Form的位置不一样。兄弟实在不知道啥原因,请赐教啊
我实现了一个Form,需要记录Form的位置(DesktopLocation)和尺寸(Size),因此我就写了个配置文件记录Form的位置和尺寸。这是读写配置文件的类
class ProfileLoader
{
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
public static extern uint GetPrivateProfileInt(string section, string key, int def, string file);
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
public static extern bool WritePrivateProfileString(string section, string key, string value,string file);
}每次Form开始构建时根据上次Form Close时的位置和尺寸进行初始化,代码如下:
void InitializeUISettings()
{
String curpath = Application.StartupPath;
curpath += "\\GM.ini"; uint original_height = ProfileLoader.GetPrivateProfileInt("GMFormOption", "HEIGHT", 300, curpath);
uint original_width = ProfileLoader.GetPrivateProfileInt("GMFormOption", "WIDTH", 300, curpath);
uint original_x = ProfileLoader.GetPrivateProfileInt("GMFormOption", "X", 0, curpath);
uint origianl_y = ProfileLoader.GetPrivateProfileInt("GMFormOption", "Y", 0, curpath);
this.Size = new System.Drawing.Size((int)original_width, (int)original_height);
this.Location = new Point((int)original_x, (int)origianl_y);
}
每次Form在Closed时,我记录Form最终的位置和尺寸,方法如下:
void StoreUISettings()
{
String curpath = Application.StartupPath;
curpath += "\\GM.ini";
ProfileLoader.WritePrivateProfileString("GMFormOption", "HEIGHT", "" + this.Height, curpath);
ProfileLoader.WritePrivateProfileString("GMFormOption", "WIDTH", "" + this.Width, curpath);
ProfileLoader.WritePrivateProfileString("GMFormOption", "X", ""+this.Location.X, curpath);
ProfileLoader.WritePrivateProfileString("GMFormOption", "Y", ""+this.Location.Y, curpath);
}
但是,奇迹发生了,我能够完整地复原上次关闭Form的尺寸,但是位置总是和上次关闭的Form的位置不一样。兄弟实在不知道啥原因,请赐教啊
解决方案 »
- sql 字段自动增加例如:A001 增加以后变成 A002
- 急!如何将一串十六进制的字符转换为含有字母,数字的字符串?
- 我想从串口接收一个字符串,当这个字符串包含Ok字符串时显示form1,否则form1关闭
- 怎样用一个文件打开另一个文件
- 调用API时,如何定义CallWindowProc中的参数
- 关于IL中Marshal的疑问
- 大量高分收购C#源码
- 大家帮忙看看,这段程序怎么不能正确编译成可执行文件?各位高手绑绑忙啊
- C#中有VB中DoEvents这样的语句么?
- 可不可以有选择的打印水晶报表的某一页?
- 关于c#控件子类化的问题。。在线等。。。急。。。。请大家一定要帮忙(我只有30分。全给了。。)
- 请大家帮我看看
private void Form1_Load(object sender, System.EventArgs e)
{
this.Location=new Point(1,1);
}放在构造函数里面是不行的,只能在load的时候来调用