Visual C # resource file programming-use resource files

Source: Internet
Author: User

This article will go on to the topic of the previous article to discuss another question about resource files, how to use resource files in Visual C. In the previous article, we have successfully created a resource file named "My. resources. This resource file contains an icon resource named "demo. ICO", several image resources and a string resource, and the name "mystr" in the file ". We will take the resource file created in the previous article as an object, and take a specific program example to see how to use the resource file with Visual C.

I. software environment for program design and operation in this article:

(1). Microsoft Windows 2000 Server Edition

(2). Net Framework SDK beta 2

Ii. Some concepts and theories in programming:

The concept and theory involved in programming are mainly the mutual conversion of two variables. These two variables are called value type variable and reference type variable ). The conversion between the two is called boxing and unboxing in Visual C ). The so-called packing is the process of converting a real-value type variable into a reference type variable, and vice versa. So what types of variables are reference type variables and what types of variables are real-value variables? What is the difference between the two? The reference types in Visual C # generally refer to these types, such as object, class, interface, Delegate, string, and array. The variables defined by these types are reference type variables. The real value type is the common integer, Boolean, and enumeration types. The variables defined by these types are the real value type variables. The biggest difference between them is that reference type variables are pointers to object objects, and real-value type variables are real object objects. Since packing and picking are very conceptual operations, it takes a lot of time to describe in detail. This is beyond the main scope of this article. Therefore, this article only introduces Program-related operations. The detailed procedure will be introduced in the next session.

Iii. Solutions to the important steps of programming:

(1). How to embed resources during program compilation. The resource file and program are two independent files. to embed the resource file into the final generated program, you must add the "/resource" command during compilation, this command can be abbreviated as "/Res ". In this article, the program name is "use. cs" and the resource file name is "My. Resources". The compilation command of the resource embedding program is as follows:

Csc.exe/Res: My. Resources Use. CS

(2). How to manage resources in resource files in a program:

The. NET Framework SDK provides a namespace for creating and using resource files-system. Resources. In this namespace, a class is ResourceManager. The main function of this class is to manage and use resource files. Visual C # uses this class to manage and use resources in the resource files embedded in the program. The following code defines a ResourceManager class to manage resources in the embedded program resource file:

ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());

(3). How to use resources in resource files in a program:

In the previous article, we learned that when creating a resource file, we used the addresource () method to add resources. The first parameter in his syntax is a string that can be defined by the user, this string is the unique identifier of a resource in a resource file. In programming, it is used to use a resource. So how can we get the required resources through this identifier in the program? This requires the GetObject () and getstring () methods in the ResourceManager class. These two methods are used to obtain the specified resource. The syntax of these two methods is as follows:

Object getsting (string)
Object GetObject (string)

The "string" is the unique identifier of the resource in the resource file. Careful readers may have noticed that the return values of these two methods are all object-type variables, that is, a reference-type variable, and strings or images in the program, is a real-value type variable. This requires conversion, and this conversion is the packing and output mentioned above. The following code extracts strings, images, and ICON resources from resource files:

Extract string resources:

String S = (string) Rm. getstring ("mystr "));

Extract ICON resources:

Icon icodemo = (icon) Rm. GetObject ("demo. ICO "));

Image extraction resources:

Image A = (image) (RM. GetObject ("ok-off.png ")));

4. Take a program example to see how to use the resource file:

The resource file used in the following program example is the resource file created in the previous article "My. resources ", the program defines three lable components, two of which are used to display image resources in the resource file, and the other is to display string Resources in the resource file, the program icon is taken from the icon resource in the resource file. The following is the source code of the program:

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. Resources;
Using system. reflection;
Public class form1: Form
{
Private Label lblok;
Private Label lblcancel;
Private system. componentmodel. Container components = NULL;
Private Label lblresource;
Public form1 ()
{
// Initialize components in the form
Initializecomponent ();
}
// Clear resources used in the program
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
Private void initializecomponent ()
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblok = new label ();
This. lblcancel = new label ();
This. lblresource = new label ();
This. suspendlayout ();

This. lblok. backcolor = system. Drawing. color. White;
// Use image resources in the resource file
This. lblok. Image = (image) (RM. GetObject ("ok-off.png ")));
This. lblok. Location = new system. Drawing. Point (24, 40 );
This. lblok. Name = "lblok ";
This. lblok. size = new system. Drawing. Size (75, 23 );
This. lblok. tabindex = 0;
This. lblok. Click + = new system. eventhandler (this. lblok_click );
This. lblok. mouseenter + = new system. eventhandler (this. lblok_mouseenter );
This. lblok. mouseleave + = new system. eventhandler (this. lblok_mouseleave );
// Export
// Use image resources in the resource file
This. lblcancel. Image = (image) (RM. GetObject ("cancel-off.png ")));
This. lblcancel. Location = new system. Drawing. Point (152, 40 );
This. lblcancel. Name = "lblcancel ";
This. lblcancel. size = new system. Drawing. Size (75, 23 );
This. lblcancel. tabindex = 1;
This. lblcancel. Click + = new system. eventhandler (this. lblcancel_click );
This. lblcancel. mouseenter + = new system. eventhandler (this. lblcancel_mouseenter );
This. lblcancel. mouseleave + = new system. eventhandler (this. lblcancel_mouseleave );

This. lblresource. Location = new system. Drawing. Point (88, 8 );
This. lblresource. Name = "lblresource ";
This. lblresource. tabindex = 2;
// Export
// Use the string resource in the resource file
This. lblresource. Text = (string) Rm. getstring ("mystr "));

This. autoscalebasesize = new system. Drawing. Size (5, 13 );
This. clientsize = new system. Drawing. Size (240,101 );
This. Controls. Add (lblresource );
This. Controls. Add (lblcancel );
This. Controls. Add (lblok );
// Export
// Use the icon resource in the resource file
Icon icodemo = (icon) Rm. GetObject ("demo. ICO "));
This. Icon = icodemo;
This. Name = "form1 ";
This. Text = "Visual C # use resource files! ";
This. resumelayout (false );

}
Static void main ()
{
Application. Run (New form1 ());
}
Private void lblok_mouseenter (Object sender, system. eventargs E)
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblok. Image = (image) (RM. GetObject ("ok-on.png ")));
}

Private void lblok_mouseleave (Object sender, system. eventargs E)
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblok. Image = (image) (RM. GetObject ("ok-off.png ")));
}

Private void lblok_click (Object sender, system. eventargs E)
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblok. Image = (image) (RM. GetObject ("ok-down.png ")));
}

Private void lblcancel_mouseenter (Object sender, system. eventargs E)
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblcancel. Image = (image) (RM. GetObject ("cancel-onr.png ")));
}

Private void lblcancel_mouseleave (Object sender, system. eventargs E)
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblcancel. Image = (image) (RM. GetObject ("cancel-off.png ")));
}

Private void lblcancel_click (Object sender, system. eventargs E)
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblcancel. Image = (image) (RM. GetObject ("cancel-over.png ")));
}
}

V. Summary:

So far, we have completed all the programming in Visual C # resource files. The main content is to create resource files and use resource files, I want to introduce these two articles. You should have a comprehensive understanding of resource files!

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.