Csc.exe /res:My.resources use.cs |
ResourceManager rm = new ResourceManager ( "Images" , Assembly.GetExecutingAssembly ( ) ) ; |
object GetSting(String) object GetObject(String) |
String s = ( ( String ) rm.GetString ( "MyStr" ) ) ; |
Icon icoDemo = ( ( Icon ) rm.GetObject ( "demo.ico" ) ) ; |
Image a = ( ( Image ) ( rm.GetObject ( "ok-off.png" ) ) ) ; |
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 ( ) { //初始化窗体中的组件 InitializeComponent ( ) ; } //清除程序中使用过的资源 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 ; //使用资源文件中的图象资源 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 ) ; //出箱 //使用资源文件中的图象资源 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 ; //出箱 //使用资源文件中的字符串资源 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 ) ; //出箱 //使用资源文件中的图标资源 Icon icoDemo = ( ( Icon ) rm.GetObject ( "demo.ico" ) ) ; this.Icon = icoDemo ; this.Name = "Form1" ; this.Text = "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 ) ( 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" ) ) ) ; } } |