已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
添加嵌入的资源若要将文本的文件和图像文件作为嵌入资源添加到项目中,请按照下列步骤操作: 1. 启动 Microsoft Visual S 2003年或 Microsoft Visual Studio 2005。2. 在 文件 菜单上指向 新建,然后单击 项目。3. 在 项目类型 框中,单击 Visual c + + 项目,然后单击 模板 下的 Windows 窗体应用程序 (.NET)。注意可视有关 Studio 的 2005 年在 项目类型 框中,单击 Visual c + +,然后单击 模板 下的 Windows 窗体应用程序。4. 在 名称 框中键入 所有 MyApplication,然后单击 确定。1. 在解决方案资源管理器中,用鼠标右键单击 所有 MyApplication,指向 添加,然后再单击 添加新项。2. 添加新项 对话框中的 模板,下单击 文本文件 (.txt)。 3. 命名该文件作为 MyTextFile.txt,然后单击 打开。注意在 Visual 有关 Studio 的 2005 年单击 添加。4. 当在编辑器中打开该文件时,该文本文件添加某些文本。5. 保存,然后关闭该文件。6. 重复步骤 5 和 6 到您的项目,但而不是单击 文本文件 (.txt) 的中添加位图图像、 单击 位图文件 (.bmp),然后将该文件的名称更改为 MyImage.bmp。7. 当在编辑器中打开新的映像时,请在图像上绘制某些内容。8. 保存,然后关闭该文件。9. 在解决方案资源管理器中,用鼠标右键单击 所有 MyApplication,然后单击 属性。10. 所有 MyApplication 属性页 对话框中展开 链接器,然后单击 输入。11. 在 嵌入托管资源文件 字段中键入您最近添加到项目的文件名。例如对于您可以键入 MyTextFile.txt ; MyImage.bmp。生成项目时,在下一次时,编译器将这些文件添加到您的程序集。12. 单击 确定。注意资源文件的名称是区分大小写的。当您访问的资源时, 必须使用完全相同的拼写和大小写的文件的名称。如果您不使用精确拼写和大小写的文件名访问 ManifestResourceStream 流将方法调用返回 空,和 $ 系统则不引发异常。如果要验证的资源名称,则可以使用 Microsoft 中间语言 (MSIL) 反汇编程序 (Ildasm.exe) 查看清单数据。将清单数据列出包含的资源。回到顶端访问资源若要进行您具有嵌入在程序集的清单中的资源,请按照下列步骤操作: 1. 导入 System:IO 和 System:Reflection 命名空间。要这样做下面的代码在代码编辑器中后添加其他 using 指令: 2. using namespace System:IO;3. using namespace System:Reflection;该 System:IO 命名空间提供的流定义和 System:Reflection 命名空间定义此 程序集 类提供了访问程序集中嵌入的资源的方法。4. 在 Form1 类中的变量声明以下成员: 5. private: Assembly *_assembly;6. private: Stream *_imageStream;7. private: StreamReader *_textStreamReader;8. 若要进行 Load 事件的窗体在代码编辑器中双击在设计模式下的窗体。9. 若要读取从执行当前代码的程序集的资源,您必须获取该程序集的实例。若要执行此操作使用 GetExecutingAssembly 方法的程序集在 Form1_Load 事件处理程序,如下所示: _assembly = Assembly:GetExecutingAssembly();10. GetManifestResourceStream 方法的方法调用执行流从资源读取信息。传递给此方法的参数是资源的要对其进行访问的名称。然后在这两个资源执行窗体的 Load 事件时读取其相应的流。若要执行此操作将下面的代码添加到 Form1_Load 事件处理程序: 11. _imageStream = _assembly-GetManifestResourceStream(SMyImage.bmp);12. _textStreamReader = new StreamReader(_assembly-GetManifestResourceStream(SMyTextFile.txt);13. 设置了异常处理机制,用于在 Form1_Load 事件处理程序代码通过 尝试 和 catch 块,如下所示: 14. try15. 16. _assembly = Assembly:GetExecutingAssembly();17. _imageStream = _assembly-GetManifestResourceStream(SMyImage.bmp);18. _textStreamReader = new StreamReader(_assembly-GetManifestResourceStream(SMyTextFile.txt);19. 20. catch(Exception *ex)21. 22. MessageBox:Show(Error accessing resources!,ex-Message);23. 称为结构化错误处理在 Visual c + +.net 中,该 try-catch 块用于捕获该 程序集 类的实例访问该资源时可能发生的任何错误。回到顶端显示资源本示例使用两个 按钮 控件显示嵌入的资源。当您单击第一个按钮时,基于读取从程序集的资源的位图图像创建,并在窗体的 图片框 控件中显示。当您单击了第二个按钮时,从文本资源读取文本,并在文本框中显示的文本。若要显示嵌入的资源,请按照下列步骤操作: 1. 将 图片框 控件添加到窗体。 2. 将一个 按钮 控件添加到该的表单,然后将其 Text 属性改为 显示图像。3. 双击以打开其 Click 事件在代码查看器中 显示图像,然后在该事件处理程序中粘贴以下代码: 4. try5. 6. pictureBox1-Image = new Bitmap(_imageStream);7. 8. catch(Exception *ex)9. 10. MessageBox:Show(Error creating image!,ex-Message); 11. 此代码生成基于窗体的 Load 事件中被读取的资源流的位图的新实例。12. 切换到设计模式,然后添加到窗体的 文本框 控件。13. 将另一个 按钮 控件添加到该的表单,然后 获取文本 以更改其 Text 属性。14. 双击要打开按钮的 Click 事件的 获取文本 并在事件处理程序中粘贴下面的代码: 15. try16. 17. if(_textStreamReader-Peek() != 1) 18. 19. textBox1-Text = _textStreamReader-ReadLine();20.21. 22. 23. catch(Exception *ex)24. 25. MessageBox:Show(Error writing text!,ex-Message); 26. 27. 请按 CTRL + B 来构建解决方案。28. 按 CTRL + F5 运行程序。回到顶端完整的代码#pragma oncenamespace MyApplication using namespace System; using namespace System:ComponentModel; using namespace System:Collections; using namespace System:Windows:Forms; using namespace System:Data; using namespace System:Drawing; using namespace System:IO; using namespace System:Reflection; / / Summary for Form1 / / WARNING: If you change the name of this class, you will need to change the / Resource File Name property for the managed resource compiler tool / associated with all .resx files this class depends on. Otherwise, / the designers will not be able to interact properly with localized / resources associated with this form. / public _gc class Form1 : public System:Windows:Forms:Form private: Assembly *_assembly; private: Stream *_imageStream; private: StreamReader *_textStreamReader; private: System:Windows:Forms:PictureBox * pictureBox1; private: System:Windows:Forms:Button* button1; private: System:Windows:Forms:TextBox* textBox1; private: System:Windows:Forms:Button* button2; public: Form1(void) InitializeComponent(); protected: void Dispose(Boolean disposing) if (disposing & components) components-Dispose(); _super:Dispose(disposing); private: / / Required designer variable. / System:ComponentModel:Container * components; / / Required method for Designer support - do not modify / the contents of this method with the code editor. / void InitializeComponent(void) this-pictureBox1 = new System:Windows:Forms:PictureBox(); this-button1 = new System:Windows:Forms:Button(); this-textBox1 = new System:Windows:Forms:TextBox(); this-button2 = new System:Windows:Forms:Button(); this-SuspendLayout(); / / pictureBox1 / this-pictureBox1-Location = System:Drawing:Point(8, 8); this-pictureBox1-Name = SpictureBox1; this-pictureBox1-Size = System:Drawing:Size(280, 152); this-pictureBox1-TabIndex = 0; this-pictureBox1-TabStop = false; / / button1 / this-button1-Location = System:Drawing:Point(8, 184); this-button1-Name = Sbutton1; this-button1-TabIndex = 1; this-button1-Text = SShow Image; this-button1-Click += new System:EventHandler(this, button1_Click); / / textBox1 / this-textBox1-Location = System:Drawing:Point(96, 224); this-textBox1-Name = StextBox1; this-textBox1-Size = System:Drawing:Size(184, 20); this-textBox1-TabIndex = 2; this-textBox1-Text = StextBox1; / / button2 / this-button2-Location = System:Drawing:Point(8, 224); this-button2-Name = Sbutton2; this-button2-TabIndex = 3; this-button2-Text = SGet Text; this-button2-Click += new System:EventHandler(this, button2_Click); / / Form1 / this-AutoScaleBaseSize = System:Drawing:Size(5, 13); this-ClientSize = System:Drawing:Size(292, 266); this-Controls-Add(this-button2); this-Controls-Add(this-textBox1); this-Controls-Add(this-button1); this-Controls-Add(this-pictureBox1); this-Name = SForm1; this-Text = SForm1; this-Load += new System:EventHandler(this, Form1_Load); this-ResumeLayout(false); private: System:Void Form1_Load(System:Object * sender, System:EventArgs * e) try _assembly = Assembly:GetExecutingAssembly(); _imageStream = _assembly-GetManifestResourceStream(SMyImage.bmp); _textStreamReader = new StreamReader(_assembly-GetManifestResourceStream(SMyTextFile.txt); catch(Exception *ex) MessageBox:Show(Error accessing resources!,ex-Message); private: System:Void button1_Click(System:Object * sender, System:EventArgs * e) try pictureBox1-Image = new Bitmap(_imageStream); catch(Exception *ex) MessageBox:Show(Error creating image!,ex-Message); private: System:Void button
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 网络游戏开发与运营成本控制研究
- 2025海南省建设集团限公司校园招聘50人易考易错模拟试题(共500题)试卷后附参考答案
- 2025年上学期高一化学珠算文化背景试题
- 2025年小升初数学试题手册
- 十五五规划纲要:农村土地制度改革的深化与产权保护
- 企业年度工作计划与目标分解实施方案
- 公司战略部主管工作计划及企业战略规划方案
- 仓储管理员试用期工作总结
- 元数据指标分析师数据资产评估报告
- IT系统工程师敏捷开发方法论指南
- 2024人教版八年级英语上册Unit5 大单元教学设计
- 2025陕西安康汉滨区招聘城镇社区专职工作人员121人考试参考题库及答案解析
- 口腔门诊护士培训课件
- 电力施工安全风险评估报告
- 牡丹江市烟草公司2025秋招综合管理类岗位面试模拟题及答案
- 轮机安全操作培训内容课件
- 标本错误不良事件课件
- 废品回收消防安全培训课件
- trips协定课件教学课件
- 2025西安市简约租房合同范本下载
- 2025年沈阳市事业单位教师招聘考试教育心理学试题
评论
0/150
提交评论