正常的C#的WinForm程序里面加上调用API好用,但是给FORM一指定图标就出现这个问题了!!!!

解决方案 »

  1.   

    resource 有没有加到项目中?
      

  2.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.InteropServices;
    class callAPICls 
    {  
    [DllImport("kernel32.dll", SetLastError=true)] static extern int AllocConsole(); 
    [DllImport("kernel32.dll", SetLastError=true)] static extern int FreeConsole(); 
    public static int Show()  

    return AllocConsole();  

    public static int ShowClose()
    {
    return FreeConsole();
    }
    public static void write(string str)
    {
    System.Console.Write(str);
    }

    namespace WindowsApplication1
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // 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, 273);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {

    }
    }
    }
    以上是我用来测试的程序,如果不给Form指定图标是好用的,指定图标就出错
      

  3.   

    我没看明白,你在设计时把图标直接加到FORM上,但在程序里却是从资源文件里去拿这个图标.
    资源文件里并没有这个图标,错误是出在这里的.这和你调用API没有关系."但是项目一加上图标就不行了."
    加的是这个notifyIcon1.Icon吗?
      

  4.   

    TO (铁拳 2004) 
    我不知道怎么回事,如果不加这个API调用运行是正常的,加上API调用就出错了.你可以看看我后贴出来的代码,比较简单,就是在WinForm程序里面加了调用API,如果不给Form指定图标是好用的,指定图标就出错
      

  5.   

    未能在给定的程序集中找到任何适合于指定的区域性(或非特定区域性)的资源。请确保已将“Form1.resources”正确嵌入或链接到程序集“MYGUI”。
    这不是已经提示出来了么?
      

  6.   

    我没有资源文件呀,新建项目时根本就不给任何资源文件
    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
    就这么一句,是C#自已生成的
      

  7.   

    http://blog.csdn.net/lizanhong/archive/2004/09/10/100812.aspx
      

  8.   

    我的思路是自己建一个资源文件,将所用的图标放入所见的资源文件中,然后再在你的这个程序中引用这个资源文件的图标。下列代码没有经过测试,只是设想。创建资源文件(用控制台程序创建就行,所得的资源文件拷贝到你的程序文件中):
    ResourceWriter rw=new ResourceWriter("demo.resources");
    Icon icon=new Icon("填写完整的文件路径名和文件名");
    rw.AddResource("取个名字",icon);
    rw.Close();在程序中用资源文件:
    private ResourceManager rm;
    public Form1()

     Assembly asse=Assembly.GetExecutingAssembly();
     rm=new ResourceManager("所创建资源文件路径,不要扩展名",asse); this.notifyIcon1.Icon = (Icon)rm.GetObject("取的名字");
    }