各位大侠!我最近在做毕业设计!在制作皮肤的时候遇到一个问题!首先是创建一个资源文件名为:Skin.resources,然后正确嵌入到工程项目中!这些都应该没什么问题!就是当我使用资源文件时问题来了!资源文件里的内容都是一些图片,图片格式为.png的,这些图片是用来制作界面皮肤的我暂时只写了一个画边框的方法,但运行时出现:<未处理的“System.NullReferenceException”类型的异常出现在 system.windows.forms.dll 中。其他信息: 未将对象引用设置到对象的实例。>!
具体参考资料是在:http://www.pconline.com.cn/pcedu/empolder/net/cs/0506/639941.html
具体代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Resources;
using System.Reflection;namespace Win
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components=null;
private ResourceManager rm;
private Image Bottom_Left;
private Image Bottom_Middle;
private Image Middle_Left;
private Image right_bottom;
private Image right_middle;
private Image Top_LeftActive;
private Image Top_MiddleActive;
private Image Top_RightActive;
private Image SysButton_Close;
private Image SysButton_Max;
private Image SysButton_Min;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); rm=new ResourceManager("Win.Skin", Assembly.GetExecutingAssembly());
//Bottom_Left=new Image();
this.Bottom_Left=(Bitmap)rm.GetObject("Bottom_Left");
this.Bottom_Middle=(Bitmap)rm.GetObject("Bottom_Middle");
this.Middle_Left=(Image)rm.GetObject("Middle_Left");
this.right_bottom=(Bitmap)rm.GetObject("right_bottom");
this.right_middle=(Bitmap)rm.GetObject("right_middle");
this.Top_LeftActive=(Bitmap)rm.GetObject("Top_LeftActive");
this.Top_MiddleActive=(Bitmap)rm.GetObject("Top_MiddleActive");
this.Top_RightActive=(Bitmap)rm.GetObject("Top_RightActive");
this.SysButton_Close=(Bitmap)rm.GetObject("SysButton_Close");
this.SysButton_Max=(Bitmap)rm.GetObject("SysButton_Max");
this.SysButton_Min=(Bitmap)rm.GetObject("SysButton_Min");
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form1";
this.Text = "Form1"; }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
DrawMiddle_Left(g);                                  //画左边框
          DrawBottom_Middle(g);                                //画下边框
          DrawMiddle_Right(g);                                 //画右边框
          DrawBottom_Left(g);                                  //画左下角
          DrawBottom_Right(g);                                 //画右下角
          DrawTop_Left(g);                                      //画标题栏左边
          DrawTop_Right(g);                                    //画标题栏右边
          DrawTop_Middle(g);                                   //画标题栏中间
          DrawSys_Button(g);                                   //画系统按纽
}
private void DrawMiddle_Left(Graphics g)
{
Brush brush=new TextureBrush(Middle_Left, new Rectangle(0,0,Middle_Left.Width,Middle_Left.Height));
g.FillRectangle(brush,0,Top_LeftActive.Height,Middle_Left.Width,Height-Bottom_Middle.Height-Top_LeftActive.Width);
}
private void DrawBottom_Middle(Graphics g)
{
}
private void DrawMiddle_Right(Graphics g)
{
}
private void DrawBottom_Left(Graphics g)
{
}
private void DrawBottom_Right(Graphics g)
{
}
private void DrawTop_Left(Graphics g)
{
}
private void DrawTop_Right(Graphics g)
{
}
private void DrawTop_Middle(Graphics g)
{
}
private void DrawSys_Button(Graphics g)
{
}
}
}
希望哪位大侠能帮帮我!谢谢了!

解决方案 »

  1.   

    你单步一下看哪里出错了,偶也在做毕设的说,null引用的问题偶也遇到过,不过一般还是蛮好搞定的说:)
      

  2.   

    不好意思啊!确实有点长!具体提示出错在
    static void Main() 
    {
         Application.Run(new Form1());
    }//这里出错!可是我认为肯定不在这里啊!
    我想大侠可以帮我看看这里是否出错了:首先在类里定义了:
    private Image Middle_Left;
    然后在本类的构造方法中调用资源文件中的资源图片:
    this.Middle_Left=(Image)rm.GetObject("Middle_Left");
    然后在实现方法:
    private void DrawMiddle_Left(Graphics g)
    {
    Brush brush=new TextureBrush(Middle_Left, new Rectangle  (0,0,Middle_Left.Width,Middle_Left.Height));
    g.FillRectangle(brush,0,Top_LeftActive.Height,Middle_Left.Width,Height-Bottom_Middle.Height-Top_LeftActive.Width);
    }//这是一个画窗体左边框的方法!
    还有就是我在重载Form1的OnPaint()方法时是直接在代码里添加如下代码的:
    protected override void OnPaint(PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    DrawMiddle_Left(g);        //画左边框
                               //其他的代码暂时省略!也就是一些方法调用!
    }还有就是资源文件我就简要说一下,在创建资源文件时,获取图片和添加图片如下:
    class CreatResource
    {
    public static void Main()
    {
       ResourceWriter rw=new ResourceWriter("Skin.resources");
       Image Middle_Left=Image.FromFile("Middle_Left.png");
       //其他图片获取跟上面一样;
       rw.AddResource("Middle_Left.png",Middle_Left);
       //其他图片添加和上面也一样
       rw.Generate();
       rw.Close();
    }
    在说我资源文件编译也成功了!
      

  3.   

    各位搞软件开发(.NET  SQL JAVA C++ 等)的朋友有兴趣可能加
    18144686
    在这里我们真正的做到只讨论技术!
    做广告者勿扰!谢谢!