C # creation and use the resource file

xiaoxiao2021-03-06  37

Creating a resource file resource file As the name refer to store resources. Resource files have their own unique advantages in programming, and he is independent of the source program so that the resource file can be used by multiple programs. At the same time, when the program is designed, sometimes the important thing is stored in the resource file for security or other factors, and the security and safety effect can be achieved. So what is the resource file used by Visual C #? Three types of data resources can be stored in the use of Visual C #, which are byte arrays, various objects, and strings, respectively. This article will specifically explain how to create a resource file with Visual C # in conjunction with a program example.

One. Some concepts and theories used by Visual C # in creating resource files: In the .NET Framework SDK, one name is called System.Resources namespace, which provides many creation, storage, and use resource files for the application in this namespace. Class and interface. One of these is called ResourceWriter, and Visual C # is created and stored in the resource file by calling this class.

two. Visual C # How to create a resource file: First of all, to inherit a resourceWriter class, then call a resourceWriter class generate (), you can generate a resource file. The specific statement is as follows:

ResourceWriter RW = New ResourceWriter ("My.Resources"); rw.generate ();

At this point, a resource file named "My.Resources" is generated in the disk, but the resource file at this time does not have anything, let's take a look at how to add resources to the resource file.

three. Add a resource in the resource file: A AddResource () method is provided in the ResourceWriter class, which is the role of this method to add resources to the resource file. Different resources are different in Visual C #.

(1). Add byte array, grammar format is:

Public void addresource (string, byte []);

Note: Where String is the unique identifier of this byte array in the program when using the resource file

(2). Join the object, the grammatical format is:

Public void addresource (string, object);

Note: Where string is the unique identifier in the program when using the resource file

In this document, we use this call mode to join icons and images, as follows:

Icon ICO = New Icon ("Demo.ico"); Image Canceloff = Image.FromFile ("Cancel-Off); Image Cancelon = Image.FromFile (" Cancel-on.PNG "); Image CancelOver = Image. Fromfile ("ca Zan-over.png"); image okdown = image.fromfile ("ok-down.png"); image okoff = image.fromfile ("OK-OFF.PNG"); Image Okon = Image.FROMFile "ok-on.png");

RW.AddResource ("Demo.ico", ICO); // Add Icon // below in the resource file to add image RW.AddResource ("Cancel-Off); rw.addresource ("Cancel-on.PNG", Cancelon; rw.addresource ("ca Zan-over.png", cancelover; rw.addresource ("ok-down.png", okdown; rw.addresource ("OK-OFF .png ", okoff); rw.addresource (" OK-ON.PNG ", OKON; (3). Add a string, the specific syntax is as follows:

Public void addresource (string1, string2);

Note: Where string1 is when using the resource file, this string is in the program in the program, in the program of this article:

Rw.addResource ("MyStr", "Read the string from the resource file!");

At this time, we have created a resource file, and several resources have been added in the resource file. Of course, you should pay attention to save this resource file, and turn off the resource file, as follows:

Rw.close ();

four. Source of the resource file: Through the above discussion, we don't even understand the following code. The following program code is to create a name "My.Resources" resource file, and add an icon resource, several image resources, and a string resource in this resource file. The specific code is as follows:

Creatresources.cs: using system.drawing; using system.resources;

Class CreatResource {public static void main () {resourceWriter RW = new resourceWriter ("my.resources"); icon ico = new icon ("demo.ico");

Image Canceloff = image.fromfile ("ca Zancel-off.png"); image canage ("ca Zan-on.png"); image canage ("ca Zan-over.png"); Image Okdown = Image.Fromfile ("ok-down.png"); image okoff = image.fromfile ("OK-OFF.PNG"); Image Okon = Image.FromFile ("OK-ON.PNG");

RW.AddResource ("Demo.ico", ICO);

RW.AddResource ("ca Zan-off.png", ca Zan; rw.addresource ("ca Zan-on.png", ca Zan; rw.addresource ("ca Zan-over.png", ca Zan; rw.addresource Ok-down.png ", okdown; rw.addresource (" OK-OFF.PNG ", OKOFF); rw.addresource (" ok-on.png ", okon); rw.addresource (" mystr "," from Read the string in the resource file! "); Rw.Generate (); rw.close ();}}

It is best to remind it that after successful compiling into executive files, when this file is executed, you must ensure that the same directory of this execution file, there is an icon and image that mentioned in the above code, otherwise the resource file is created It will be wrong.

Fives. Summary: The visible Visual C # Creating a resource file is not a complex process. In the next article, we will show you how to use the resource files in Visual C #. This is the focus and difficulties of Visual C # resource file programming. Of course, the resource files used in the text are the resource files created in this article. Use the resource file in "Visual C # Resource File Programming - Create Resource Files" to create a resource file with Visual C #. 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

two. 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 in the resource file 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 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, of which three Lable components are defined in the program. The two functions are the image resources in the resource file, and the other role is to display a string resource in the resource file, and the icon of the program is an icon resource in 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 .Getexecutingassembly ()); this.lblok = new label (); this.lblcancel = new label (); this.lblResource = new label (); this.suspendlayout ();

This.lblok.backcolor = system.drawing.color.white; // Using the image resources in the resource file this.lblok.Image = ((image)))))); 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); // Opening the box // Use the image resources in the resource file this.lblcancel.image = ((image))); 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.Ev Enthandler; this.lblresource.location = new system.drawing.point (88, 8); this.lblResource.name = "lblResource"; this.lblResource.tabindex = 2; // Opening box // String resources 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); // Opening box // Using the icon resource in the resource file icon icodemo = ((icon) RM.GetObject ("demo.ico")); this.icon = icodemo; this.name = "Form1"; this.text = "Visual C # uses resource files in Visual C #"; 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))))) }

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"))))))))))

Fives. Summary: At this time, we have completed the entire content of the Visual C # resource file, the main content is to create a resource file and the second part of the use of resources, I want to introduce the two articles, you should With a more comprehensive understanding!

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

New Post(0)