Winforms and Resources (Resources/resx)

Source: Internet
Author: User

Http://www.sellsbrothers.com/writing/ResourcesAndWinForms.htm

Resource (A resource) is the name data bound to the Assembly during Build. For example, you can use the following method in your application to set the background image by loading images in the file system:

Public MainForm (){
InitializeComponent ();
This. BackgroundImage = new Bitmap (@ "C: \ WINDOWS \ Web \ Wallpaper \ Azul.jpg ");
}

The question of this Code is, of course, not all users have azul.jpg in windows, and even some may not be in the same position. Even if you put the image in your application, a user who cares about the space may decide to remove it, leading to a failure in your application (this seems to be a bit of a reason, users who dare to delete the content of the application folder must bear this price, although I have done such a thing before ...). The only insurance method is to embed images or other files as a resource file. You can achieve this in two ways. One way is to right-click your project's Solution Explorer, add an existing project, and select the file you want to embed. This file will be copied to the folder of your project, but it will not be embedded. To embed a project as a Resource, you need to right-click the file, select properties, and find the "Embedded Resource" option in properties. After this is done, the file can be loaded at runtime. Many. NET classes provide constructors that use resource identifiers, such as Bitmap:

Public MainForm (){
InitializeComponent ();
This. BackgroundImage = new Bitmap (this. GetType (), "Azul.jpg ");
}

. When an image is loaded at runtime, the first parameter is the type of the resource, followed by the name string.

Of course, if you directly want to see the background effect in the designer, you can use Property Browswer instead of writing the code to load the resource. For example, the background image is the BackgroundImage property. In this way, the designer automatically generates the Code:

Namespace MySecondApp {
Public class MainForm: System. Windows. Forms. Form {
Public MainForm (){
InitializeComponent ();
}
 
Private void InitializeComponent (){
System. Resources. ResourceManager resources =
New System. Resources. ResourceManager (typeof (MainForm ));
...
This. BackgroundImage =
(Bitmap) resources. GetObject ("$ this. BackgroundImage ");
...
}
...
}

In this example, the ResourceManager class is used instead of Bitmap constructor. This class can directly access specific regional resources at runtime, regardless of whether the resources are in the Assembly. In this way, you do not need to modify the code or re-compile the Form.

In addition, the Properties of the project contains a Resource. resx Resource file by default. By default, a resx file is followed by a WinForm file. The resx file is a resource file, that is, a program resource file.
This can be called as follows:
This. BackgroundImage = global: NamespaceName. Properties. Resources. resourcename;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.