兄弟我刚刚开始在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的位置不一样。兄弟实在不知道啥原因,请赐教啊