.NET resource file usage

xiaoxiao2021-03-06  107

This article will follow the topics of the previous article to explore the resource file another problem, how to use the resource file 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" in the file, several image resources and a string resource, name "mystr" in the file. We will create this resource file in the above article as an object, combined with a specific program example to see how to use the resource file with Visual C #.

One. This article is designed and running software environment: (1). Microsoft Window 2000 Server Edition (2) .. Net Framework SDK Beta 2. Some concepts and theories in programming: The concepts and theories involved in program design are mainly two variables of mutual conversion. This is the two variables are the so-called real value type variables, and the reference type variable (Reference Type Variable). The mutual conversion between the two is called boxing and out boxes in Visual C # (unboxing). The so-called packing is the process of converting the real value type variable into a reference type variable. So what type of variable is a variable of reference type, what type of variable is a real value type variable? What is the difference between these two? The reference type in Visual C # is generally referring to these types, such as: Object, Class, Interface, Delegate, String, Array, etc. These types of defined variables are reference type variables. The real value type is integer, Boolean, enumeration, etc., which are usually used, and variables defined by these types are real value type variables. The maximum difference between them is that the type variable is a pointer to the entity object, and the real value type variable is a real entity object. Due to packing and out of boxes are a conceptual operational process, detailed introduction requires a lot of space. And this is beyond the main categories discussed in this article. Therefore, this article only introduces and proceeds to the box. Specific operation steps will be described in the next income program. three. The idea of ​​programming has been important steps: (1). How to embed resources during program compilation. Resource files and programs are two separate files. To embed the resource file into the finally generated program, you need to add the "/ resource" command when compiling, this command can be short-handed as "/ res". In this article, the name "use.cs", the name of the resource file is "My.Resources", the compilation command of the resource embedder is as follows:

CSC.EXE /RES: MY.Resources use.cs

(2). How to manage resources in program management resources:

In .NET Framework SDK, this provides a namespace for the creation and use of resource files - System.Resources. In this name space, there is a Class for ResourceManager. The main role of this Class is to manage and use the resource file. Visual C # is managed by this class and uses resources in the resource file in the embedder. The following code is to define a ResourceManager class to manage resources in embedded resource files:

ResourceManager RM = New ResourceManager ("Images", assembly.getexecutingassembly ()); (3). How to use resources in the program:

In the previous article, we have learned that when you create a resource file, use the AddResource () method to join the resource, the first parameter in his syntax is the string that the user can define, this string is the resources. The unique identifier of the resource file is in the program design, is to use a resource through this unique identifier. So how do you get the required resources through this identifier in the program? This is to use the getObject () and getString () methods in the ResourceManager class. These two methods are to obtain the specified resource. Below is the syntax of these two methods:

Object getSting (String) Object getObject (string)

"String" is the unique identifier of resources in the resource file. Careful readers may have noticed that the return value of these two methods is a variable of an Object type, that is, a variable of a reference type, and a string or image in the program, is a real value type variable. This needs to be converted, and this conversion is the boxes and out of the box above. The following code is extracting strings from resource files, images and icons resources:

Extract string resources:

String s = (String) RM.GetString ("mystr");

Extract icon resources:

Icon icodemo = ((icon) RM.GetObject ("Demo.ico");

Extract image resources:

Image a = ((image) (RM.GetObject ("OK-OFF.PNG")))))

four. In combination with a program example, the specific use method of the resource file:

The resource file used by the following program example is the resource file "My.Resources" created in the previous article, and three Lable components are defined in the program, where two functions are image resources in the resource file, in addition One role is to display the string resources in the resource file, the icon of the program is an icon resource taken from the resource file. Below 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 () {// initialization assembly InitializeComponent form ();} // clear the programs used in the resource 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 .Gexecutingassembly ()); this.lblok = new label (); this.lblcancel = new label (); this.lblResource = new label (); this.suspendlayout (); this.lblok.backcolor = system.drawing.color .White; // use the image resources in the resource file this.lblok.Image = ((image)); this.lblok.location = new system.drawing. Point (24, 40); THISLBLOK.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.mouseeleave = new system.eventhandler (this.lblok_mouseeleave); // out of the box // In the image of the resource file Resource this.lbrcancel.Image = ((image) ("ca Zancel-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) id.lblresource.location = new system.drawing.point (88, 8); this.lblResource.name = "lblResource"; this.lblResource.tabindex = 2; // Opening box // Use the string in the resource file Resource this.lblResource.text = ("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); // Opening box // Icon Icon ICODEMO = ((icon) Rm.GetObject ("demo.cy"); this.icon = icodemo; this.name = "form1"; this.text = "Visual C # Using resource files in" "; 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) ("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)))));}} 5. to sum up:

At this time, we have completed all the contents of the programming in Visual C # resource files, the main content is to create a resource file and the use of resources files, I want to introduce this two articles, you should have a resource file. A more comprehensive understanding!

转载请注明原文地址:https://www.9cbs.com/read-124258.html

New Post(0)