从设置文件用户可选择的BackgroundImage(物理资源) [英] User-selectable BackgroundImage from settings file (Physical and Resource)

查看:160
本文介绍了从设置文件用户可选择的BackgroundImage(物理资源)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单选按钮,改变我的窗体的背景图像的WinForms应用程序,并能节省背景图片到一个文本文件的位置,这样的背景下,选择用户每次运行时被加载应用程序。

I've got a WinForms app with a radio button that changes the background image of my form, and saves the location of that background image to a text file, so the background that the user selected gets loaded each time they run the application.

目前,这些背景图片必须位于用户的 /(用户名)/我的文档/应用程序名称/毛皮/默认/ 文件夹中。

Right now, those background images need to be located in the user's /(USERNAME)/My Documents/Application Name/Skins/Default/ folder.

我想申请做如下:

用户可以选择自定义的皮肤,位于 /(用户名)/我的文档/应用程序名称/毛皮/自定义/ 文件夹中。

The user can select a "Custom" skin, located in the /(USERNAME)/My Documents/Application Name/Skins/Custom/ folder.

和也可以从默认的皮肤选择,但位于项目的资源,这些外观。在这里我的问题是:我可以读取和写入设置文件自定义皮肤的物理位置,但我不知道如何添加到选择用这种方法一个内置的项目资源位置的背景下的能力,仍然有它保存到设置文件。

And also be able to select from the "Default" skins, but have those skins located in the project's resources. My problem here is: I can read and write the physical location of the custom skin from the settings file, but I'm not sure how to add the ability to choose a background from a built-in project resource location with this method, and still have it save to the settings file.

下面是code我使用物理位置现在:

Here is the code I'm using for physical locations right now:

    private void DefaultThemeButton_CheckedChanged(object sender, EventArgs e)
    {
        string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");
        string currentBackgroundImage = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application", "Skins", "Default", "DefaultBackground.png");
        var lines = System.IO.File.ReadAllLines(SettingsPath);
        lines[7] = (currentBackgroundImage);
        System.IO.File.WriteAllLines(SettingsPath, lines);
        MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadLines(SettingsPath).Skip(7).Take(1).First()); ;

我希望能够做到这样的事情:

I would like to be able to accomplish something like this:

// USER SELECTS DEFAULT BACKGROUND
    private void DefaultThemeButton_CheckedChanged(object sender, EventArgs e)
    {
        string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");
        string currentBackgroundImage = ((System.Drawing.Image)(Properties.Resources.DefaultBackground))  // THIS IS THE PART I'M STUCK ON
        var lines = System.IO.File.ReadAllLines(SettingsPath);
        lines[7] = (currentBackgroundImage);
        System.IO.File.WriteAllLines(SettingsPath, lines);
        MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadLines(SettingsPath).Skip(7).Take(1).First()); ;

// USER SELECTS CUSTOM BACKGROUND

    private void CustomThemeButton_CheckedChanged(object sender, EventArgs e)
    {
        string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");
        string currentBackgroundImage = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application", "Skins", "Custom", "Background.png");
        var lines = System.IO.File.ReadAllLines(SettingsPath);
        lines[7] = (currentBackgroundImage);
        System.IO.File.WriteAllLines(SettingsPath, lines);
        MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadLines(SettingsPath).Skip(7).Take(1).First()); ;

有没有办法读取像一个物理位置的资源位置,而无需实际在文件中都有一个物理文件夹?

Is there a way to read the resource location like a physical location without having to actually have the file in a physical folder?

推荐答案

我来了通过改变我的背景是如何选择解决我的问题。在我的选题形式,用户选择的主题被写入我的 Settings.cfg 文件包括主题名称的简单字符串。

I've come up with a solution to my problem by changing how my backgrounds are selected. On my theme selection form, the user's selected theme gets written to my Settings.cfg file as a simple string consisting of the name of the theme.

的主题然后被从任一资源,或主题的物理位置加载,这取决于选择哪一个上

The theme then gets loaded from either the resources, or the physical location of the theme, depending on which one was selected.

主题选择方式:

    private void DEFAULTTHEME_Click(object sender, EventArgs e)
    {            
        string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");            
        string SelectedTheme = "Default Theme"; //THIS GETS WRITTEN TO THE SETTINGS FILE           
        var lines = System.IO.File.ReadAllLines(SettingsPath);
        lines[7] = (SelectedTheme);
        System.IO.File.WriteAllLines(SettingsPath, lines);
        MainForm.BackgroundImage = Properties.Resources.DefaultBackground;
    }

    private void LIGHTTHEME_Click(object sender, EventArgs e)
    {            
        string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");            
        string SelectedTheme = "Light Theme"; //THIS GETS WRITTEN TO THE SETTINGS FILE           
        var lines = System.IO.File.ReadAllLines(SettingsPath);
        lines[7] = (SelectedTheme);
        System.IO.File.WriteAllLines(SettingsPath, lines);
        MainForm.BackgroundImage = Properties.Resources.LightBackground;
    }

    private void CUSTOMTHEME_Click(object sender, EventArgs e)
    {
        string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");
        string CustomBackground = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Skins", "Custom", "Background.png"); //THIS IS SO MY APPLICATION KNOWS THE LOCATION OF THE CUSTOM BACKGROUND IMAGE
        string SelectedTheme = "Custom Theme"; //THIS GETS WRITTEN TO THE SETTINGS FILE
        var lines = System.IO.File.ReadAllLines(SettingsPath);
        lines[7] = (SelectedTheme);
        System.IO.File.WriteAllLines(SettingsPath, lines);
        MainForm.BackgroundImage = Image.FromFile(CustomBackground);
    }

在我的主要形式加载,它会检查在第8行的设置文件的内容,并将其转换为字符串(选定主题的名称)。然后形式决定哪些背景图像来显示,这取决于字符串的内容:

When my main form loads, it checks the contents of the settings file at line 8, and converts that to a string (the name of the selected theme). The form then decides which background image to display, depending on the contents of the string:

private void Form1_Load(object sender, EventArgs e)   
{
 string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");
 string CustomBackground = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Skins", "Custom", "Background.png");
 string SelectedTheme = System.IO.File.ReadLines(SettingsPath).Skip(7).Take(1).First();

//LOAD THEME
        if (SelectedTheme.Equals("Default Theme"))
        {
            this.BackgroundImage = Properties.Resources.DefaultBackground;
        }
        else if (SelectedTheme.Equals("Light Theme"))
        {
            this.BackgroundImage = Properties.Resources.LightBackground;
        }
        else if (SelectedTheme.Equals("Custom Theme"))
            this.BackgroundImage = Image.FromFile(CustomBackground);
        else
        {
            this.BackgroundImage = Properties.Resources.DefaultBackground;
        }
}

我不知道这是否是做到这一点的理想方式,因为我很新的C#中,但这种方法作品非常好我的应用程序。

I'm not sure if this is the ideal way to do this, since I'm very new to C#, but this method works extremely well for my application.

这篇关于从设置文件用户可选择的BackgroundImage(物理资源)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆