110,538
社区成员
发帖
与我相关
我的任务
分享
private void ComboBox1_TextChanged(object sender, EventArgs e)
{
string ConfigFilePath = GetConfigFilePath(); // 获取玩家需要修改帧数的配置文件路径
if (File.Exists(ConfigFilePath))
{
try
{
pictureBox3.Visible = true; // 显示配置文件的图标,用户点击后可以用记事本改帧数
// 将配置文件中关于帧数的值放到TextBox内供用户修改
TextBoxesGetValues(ConfigFilePath);
// 读取配置文件中是否隐藏宠物的参数
string LoverPetShowFlag = ReadValueFromIniFile(Section, strLoverPetShowFlag, "null", ConfigFilePath);
if (LoverPetShowFlag == "0")
{
checkBox1.CheckState = CheckState.Checked; // 隐藏宠物
}
else
{
checkBox1.CheckState = CheckState.Unchecked; // 显示宠物
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
pictureBox3.Visible = false;
EmptyValueTextBoxes();
}
}
private void EmptyValueTextBoxes()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
private void TextBoxesGetValues(string iniFilePath)
{
textBox1.Text = ReadValueFromIniFile(Section, strFramelimit, "null", iniFilePath);
textBox2.Text = ReadValueFromIniFile(Section, strOutdoorFramelimit, "null", iniFilePath);
textBox3.Text = ReadValueFromIniFile(Section, strDanceFramelimit, "null", iniFilePath);
}
private string GetConfigFilePath()
{
StringBuilder sb = new StringBuilder();
sb.Append(QQSpeedDirectoryPath);
sb.Append(@"\");
sb.Append(comboBox1.Text);
sb.Append(@"\配置\config.ini");
return sb.ToString();
}
// FormBorderStyle = None
// 在Client Area上绘制文字和最小化、关闭按钮做标题栏
// 为防止窗口控件闪烁,采用Graphic.DrawString的方式,取代Label控件
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
SolidBrush drawBrush = new SolidBrush(Color.Black);
e.Graphics.DrawString(TitleString, new Font("Microsoft YaHei", 9), drawBrush, 2, 5);
e.Graphics.DrawLine(new Pen(Color.LightGray, 1), 0, 25, 300, 25);
Font font = new Font("Verdana", 10);
SizeF QQstringSize = e.Graphics.MeasureString("QQ", font);
SizeF FramelimitstringSize = e.Graphics.MeasureString("Framelimit", font);
SizeF OutdoorFramelimitstringSize = e.Graphics.MeasureString("OutdoorFramelimit", font);
SizeF DanceFramelimitstringSize = e.Graphics.MeasureString("DanceFramelimit", font);
int downoffset = 3;
int dis = 1;
int UpperLowerOffset = 3;
e.Graphics.DrawString("QQ", font, drawBrush, comboBox1.Location.X - dis - QQstringSize.Width - 2, comboBox1.Location.Y + downoffset);
e.Graphics.DrawString("Framelimit", font, drawBrush, textBox1.Location.X - dis - FramelimitstringSize.Width, textBox1.Location.Y + downoffset);
e.Graphics.DrawString("OutdoorFramelimit", font, drawBrush, textBox2.Location.X - dis - OutdoorFramelimitstringSize.Width + UpperLowerOffset, textBox2.Location.Y + downoffset);
e.Graphics.DrawString("DanceFramelimit", font, drawBrush, textBox3.Location.X - dis - DanceFramelimitstringSize.Width + UpperLowerOffset, textBox3.Location.Y + downoffset);
drawBrush.Dispose();
font.Dispose();
// e.Graphics.Dispose();
}
在这里本来想加入e.Graphics.Dispose();语句,因为我看到网上关于资源释放的示例代码有这句,但是写上之后就没法编译直接报错。
我昨晚又查了一些资料,网上又有说法说Paint事件不要轻易自己写。我在想会不会是本文开头提到的ComboBox的TextChanged事件触发了这里的 Form1_Paint事件,而Paint事件又没有进行e.Graphics.Dispose()导致的内存攀升。 #region ini文件读取写入函数
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public static string ReadValueFromIniFile(string Section, string Key, string def, string filePath)
{
StringBuilder temp = new StringBuilder(1024);
int i = GetPrivateProfileString(Section, Key, def, temp, 1024, filePath);
return temp.ToString();
}
public static void WriteValueToIniFile(string Section, string Key, string Value, string filePath)
{
WritePrivateProfileString(Section, Key, Value, filePath);
}
#endregion
至于这里该怎么释放,我试过这样的:
public static string ReadValueFromIniFile(string Section, string Key, string def, string filePath)
{
StringBuilder temp = new StringBuilder(1024);
int i = GetPrivateProfileString(Section, Key, def, temp, 1024, filePath);
// temp.Dispose();
return temp.ToString();
}
但是这样写函数就没法返回string了。一开始做这程序的时候,就是因为对C/C++一无所知,要学Windows消息机制什么的,而且自己也没有那个能力写出安全的代码,在网上看到说写C#代码不需要自己对内存进行管理,就选择了C#。没想到后面做的时候发现很多关键地方还是得调用C/C++ API……