.net教程:在.net中嵌入和使用资源文件
[size=3]嵌入和使用资源文件,以下是全部源代码:using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.reflection;
using system.io;
using system.diagnostics;
namespace resourcedemo
{
/// <summary>
/// summary description for form1.
/// </summary>
public class form1 : system.windows.forms.form
{
arraylist pics;
private system.windows.forms.groupbox groupbox1;
private system.windows.forms.picturebox pbox;
private system.windows.forms.button btndisplay;
private system.windows.forms.textbox txtinfo;
/// <summary>
/// required designer variable.
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// required for windows form designer support
//
initializecomponent();
// instantiate our arraylist
pics = new arraylist();
}
/// <summary>
/// clean up any resources being used.
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows form designer generated code
/// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
this.pbox = new system.windows.forms.picturebox();
this.groupbox1 = new system.windows.forms.groupbox();
this.btndisplay = new system.windows.forms.button();
this.txtinfo = new system.windows.forms.textbox();
this.groupbox1.suspendlayout();
this.suspendlayout();
//
// pbox
//
this.pbox.borderstyle = system.windows.forms.borderstyle.fixedsingle;
this.pbox.location = new system.drawing.point(8, 8);
this.pbox.name = "pbox";
this.pbox.size = new system.drawing.size(264, 272);
this.pbox.tabindex = 0;
this.pbox.tabstop = false;
this.pbox.sizemode = system.windows.forms.pictureboxsizemode.stretchimage;
//
// groupbox1
//
this.groupbox1.controls.addrange(new system.windows.forms.control[] {
this.txtinfo,
this.btndisplay});
this.groupbox1.location = new system.drawing.point(288, 8);
this.groupbox1.name = "groupbox1";
this.groupbox1.size = new system.drawing.size(192, 264);
this.groupbox1.tabindex = 1;
this.groupbox1.tabstop = false;
//
// btndisplay
//
this.btndisplay.location = new system.drawing.point(48, 24);
this.btndisplay.name = "btndisplay";
this.btndisplay.size = new system.drawing.size(96, 23);
this.btndisplay.tabindex = 0;
this.btndisplay.text = "display picture";
this.btndisplay.click += new system.eventhandler(this.button1_click);
//
// txtinfo
//
this.txtinfo.location = new system.drawing.point(8, 56);
this.txtinfo.multiline = true;
this.txtinfo.name = "txtinfo";
this.txtinfo.readonly = true;
this.txtinfo.size = new system.drawing.size(176, 200);
this.txtinfo.tabindex = 2;
this.txtinfo.text = "txtinfo";
//
// form1
//
this.autoscalebasesize = new system.drawing.size(5, 13);
this.clientsize = new system.drawing.size(496, 293);
this.controls.addrange(new system.windows.forms.control[] {
this.groupbox1,
this.pbox});
this.name = "form1";
this.text = "form1";
this.load += new system.eventhandler(this.form1_load);
this.groupbox1.resumelayout(false);
this.resumelayout(false);
}
#endregion
/// <summary>
/// the main entry point for the application.
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void button1_click(object sender, system.eventargs e)
{
// go to a random picture in our arraylist and
// display it
random generator = new random();
bitmap bmp = pics[ generator.next(pics.count) ] as bitmap;
if(!(null==bmp))
{
pbox.image = bmp;
}
bmp = null;
generator = null;
}
private void form1_load(object sender, system.eventargs e)
{
stream imgstream = null;
bitmap bmp = null;
// get a reference to the current assembly
assembly a = assembly.getexecutingassembly();
// get a list of resource names from the manifest
string [] resnames = a.getmanifestresourcenames();
// populate the textbox with information about our resources
// also look for images and put them in our arraylist
txtinfo.clear();
txtinfo.text += string.format("found {0} resourcesrn", resnames.length);
txtinfo.text += "----------rn";
foreach(string s in resnames)
{
txtinfo.text += s + "rn";
if(s.endswith(".bmp"))
{
// attach to stream to the resource in the manifest
imgstream = a.getmanifestresourcestream(s);
if( !(null==imgstream) )
{
// create a new bitmap from this stream and
// add it to the arraylist
bmp = bitmap.fromstream( imgstream ) as bitmap;
if( !(null==bmp) )
{
pics.add( bmp );
}
bmp = null;
imgstream.close();
imgstream = null;
}
}
}
txtinfo.text += "----------rn";
txtinfo.text += string.format("found {0} bitmapsrn",
pics.count);
}
}
}
[/size]
页:
[1]