110,535
社区成员
发帖
与我相关
我的任务
分享
public Form1()
{
InitializeComponent();
string str = ini.readvalue("pifu", "divi");
if (str.Length>4)
{
this.BackgroundImage = Image.FromFile(str);
}
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
}
你试试吧using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace yytd
{
class Ini
{
//声明(把一个Win32 API函数转成C#函数)
/// <summary>
/// 声明INI文件的写操作函数 WritePrivateProfileString()
/// </summary>
/// <param name="section">配置节</param>
/// <param name="key">键名</param>
/// <param name="value">键值</param>
/// <param name="filepath">地址</param>
/// <returns></returns>
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string value, string filepath);
/// <summary>
/// 声明INI文件的读操作函数 GetPrivateProfileString()
/// </summary>
/// <param name="section">配置节</param>
/// <param name="key">键名</param>
/// <param name="def">用于读取时,为null返回def</param>
/// <param name="reval">读取配置的字符串缓冲区</param>
/// <param name="size">大小,至少为size</param>
/// <param name="filepath">地址</param>
/// <returns></returns>
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder reval, int size, string filepath);
private string sPath = null;
/// <summary>
///主要用于传输地址
/// </summary>
/// <param name="path">地址</param>
public Ini(string path)
{
this.sPath = path;
}
/// <summary>
/// 写函数,用于写配置
/// </summary>
/// <param name="section">配置节</param>
/// <param name="key">配置节下的键名</param>
/// <param name="value">键值</param>
public void write(string section, string key, string value)
{
WritePrivateProfileString(section, key, value,sPath);
}
/// <summary>
/// 读函数,用于读取配置
/// </summary>
/// <param name="section">配置节</param>
/// <param name="key">键名</param>
/// <returns>返回配置</returns>
public string readvalue(string section, string key)
{
StringBuilder temp = new StringBuilder(255);
GetPrivateProfileString(section, key, "", temp, 255, sPath);
return temp.ToString();
}
}
}
private void button8_Click(object sender, EventArgs e)
{
string URL =null;
OpenFileDialog fm = new OpenFileDialog();
fm.Filter = "图片格式|*.jpg;*.bmp";
if (fm.ShowDialog() == DialogResult.OK)
{
URL = fm.FileName;
this.BackgroundImage = Image.FromFile(URL);
this.BackgroundImageLayout = ImageLayout.Stretch;
ini.write("pifu", "divi", URL);
}
}
类里面定义全局变量string st1
Ini ini = new Ini(@"D:\config.ini");
public string str1;
form1里面添加
str1 = ini.readvalue("pifu", "divi");
load事件里面添加
this.BackgroundImage = Image.FromFile(str1);