[英]Save programmatically created picturebox control and reload
Actually I am in trouble.其实我有麻烦了。 My project is to create a desktop with shortcuts.
我的项目是创建一个带有快捷方式的桌面。 The desktop is ready, it is a picturebox as background.
桌面准备好了,它是一个图片框作为背景。 And now the shortcuts.
现在是快捷方式。
Through a dialog, I will create a shortcut on the picturebox (desktop).通过一个对话框,我将在图片框(桌面)上创建一个快捷方式。 Example: Here is my desktop: https://pasteboard.co/IHSdXvX.png
示例:这是我的桌面: https://pasteboard.co/IHSdXvX.png
Here is my dialog: https://pasteboard.co/IHSefrz.png First textbox is the path to the file.这是我的对话框: https://pasteboard.co/IHSefrz.png第一个文本框是文件的路径。 Second textbox is the name of the shortcut.
第二个文本框是快捷方式的名称。
And here is the result: https://pasteboard.co/IHSeM7Y.png In the upper left corner is a new green shortcut.结果如下: https://pasteboard.co/IHSeM7Y.png左上角是一个新的绿色快捷方式。 It is a picturebox, which was created by this code:
它是一个图片框,由以下代码创建:
Icon ico = Icon.ExtractAssociatedIcon(textBox1.Text);
var picture = new PictureBox
{
Name = textBox2.Text,
Size = new Size(48, 46),
Location = new Point(100, 100),
Image = ico.ToBitmap(),
SizeMode = PictureBoxSizeMode.Zoom,
};
Form1Singleton.FormVerweis.pictureBox1.Controls.Add(picture);
So, my question is:所以,我的问题是:
How can I save the new created picturebox (the green icon upper left corner) and load it back to the same position, and the same click-Event to start the.exe behind it (exe path in textbox 1 in the dialog before) when the application starts.当应用程序启动。 Information about the picturebox to save: Location, Icon, .exe Path.
有关要保存的图片框的信息:位置、图标、.exe 路径。
Thanks for your help and I am really happy about code-examples.感谢您的帮助,我对代码示例感到非常高兴。 Joshua
约书亚
You will need to create a class that stores all the information you need to create an icon:您将需要创建一个 class 来存储创建图标所需的所有信息:
[Serializable]
public class IconDefinition
{
public string Name { get; set; }
public string Path { get; set; }
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
Then you have some method that actually creates your icon based on these values:然后你有一些方法可以根据这些值实际创建你的图标:
void createIcon(IconDefinition iconDefinition)
{
PictureBox pictureBox = new PictureBox()
{
Name = iconDefinition.Name,
Size = new Size(iconDefinition.Width, iconDefinition.Height),
Location = new Point(iconDefinition.X, iconDefinition.Y),
Image = Icon.ExtractAssociatedIcon(iconDefinition.Path).ToBitmap(),
SizeMode = PictureBoxSizeMode.Zoom
};
pictureBox.Click += myClickHandler;
Form1Singleton.FormVerweis.pictureBox1.Controls.Add(pictureBox);
}
Next you have your methods to serialize and deserialize these.接下来你有你的方法来序列化和反序列化这些。 Since you probably want to store all your icons in the same file for persistence you would use a
List
for serialization.由于您可能希望将所有图标存储在同一个文件中以保持持久性,因此您将使用
List
进行序列化。
static void saveIcons(List<IconDefinition> icons)
{
using (Stream stream = new FileStream(@".\icons.bin", FileMode.Create, FileAccess.Read))
new BinaryFormatter().Serialize(stream, icons);
}
static List<IconDefinition> loadIcons()
{
using (Stream stream = new FileStream(@".\icons.bin", FileMode.Open, FileAccess.Read))
return (List<IconDefinition>)new BinaryFormatter().Deserialize(stream);
}
These will provide you functionality to write a List<IconDefinition>
to a file, or restore it from a file.这些将为您提供将
List<IconDefinition>
写入文件或从文件恢复的功能。
Now you will have to have a place somewhere to put all your icons:现在您必须在某个地方放置所有图标:
List<IconDefinition> allIcons = new List<IconDefinition>();
When manually creating the icon (as you do already):手动创建图标时(就像您已经做的那样):
IconDefinition iconDefinition = new IconDefinition()
{
Path = textBox1.Text,
Name = textBox2.Text,
X = 100,
Y = 100,
Width = 48,
Height = 46
};
allIcons.Add(iconDefinition);
createIcon(iconDefinition);
When you want to save all your icons:当您想保存所有图标时:
saveIcons(allIcons);
And when your application starts to restore them:当您的应用程序开始恢复它们时:
// When application starts you can load the icons
allIcons = loadIcons();
// then restore them all
foreach (IconDefinition definition in allIcons)
createIcon(definition);
Hope that helps.希望有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.