想学做一个托盘闪动的程序,可出问题请高人帮助感谢,如下三个问题程序目录
we
48X48.ico
Hello.cs
Resources.resx
无标题 - 1.ico
-------------------------
Hello.cs
-------------using System;
using System.Drawing;//托盘小标用
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace we
{
/*==============主程序入口========================*/
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Console.ReadKey();
}
}
/*==========form1类--理解为窗口层===================*/
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//当然这里也可以直接写InitializeComponent()方法,现在是写到了partial class Form1中
/*加入托盘图片开始*/
int i = 0;
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true; }
/*
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
this.notifyIcon2.Icon = ((System.Drawing.Icon)(Resources._48X48));
}*/ //★有问题处1,这的Resources对吗?
private void timer1_Tick(object sender, EventArgs e)
{
if (i == 0)
{
this.notifyIcon2.Icon = ((System.Drawing.Icon)(Resources.无标题___1));
i = 1;
}
else
{
this.notifyIcon2.Icon = ((System.Drawing.Icon)(Resources._48X48));
i = 0;
}
}
//托盘图标事件结束
}
/*-==========form1类----窗口程序本体===========*/
partial class Form1
{
//主体变量名
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
//按扭变量名
private System.Windows.Forms.Button buttoww = new System.Windows.Forms.Button();
private System.Windows.Forms.Button button1= new System.Windows.Forms.Button();
//时间变理名
private System.Windows.Forms.Timer timer1;
//ico文件变量
public System.Windows.Forms.NotifyIcon notifyIcon2;
//窗体中显示
private void InitializeComponent()
{
//窗体
components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
notifyIcon2 = new System.Windows.Forms.NotifyIcon(this.components);
timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout(); // notifyIcon2
//★有问题处2,这里的resources好像不能用
notifyIcon2.Icon =((System.Drawing.Icon)(resources.GetObject("notifyIcon2.Icon")));
notifyIcon2.Text = "notifyIcon2";
notifyIcon2.Visible = true;
//1,bnt
buttoww.Location = new System.Drawing.Point(174, 205);
buttoww.Name = "checkBox1";
buttoww.Size = new System.Drawing.Size(75,23);
buttoww.TabIndex = 1;
buttoww.Text = "关上";
buttoww.UseVisualStyleBackColor = true;
buttoww.Click += new System.EventHandler(buttoww_Click);
//2,bnt
button1.Location = new System.Drawing.Point(12, 12);
button1.Name = "button1";
button1.Size = new System.Drawing.Size(100, 23);
button1.TabIndex = 0;
button1.Text = "闪动开始";
button1.UseVisualStyleBackColor = true;
button1.Click += new System.EventHandler(button1_Click);
//
// timer1
timer1.Interval = 400;
timer1.Tick += new System.EventHandler(timer1_Tick);
Text = "我的第一个程序";
//写入按扭
Controls.Add(button1);
Controls.Add(buttoww);
this.ResumeLayout(false);
}
//按扭响应事件
private void buttoww_Click(object sender, EventArgs e)
{
MessageBox.Show("还没有加数据");
}
}
//★有问题处3:如下写法不知道能不能用
//============Resources类读取Resources.resx文件再写入属性========================
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "2.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources()
{
//这类的头
}
//读取Resources.resx文件
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("we.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// 重写当前线程的 CurrentUICulture 属性。
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
//图标1
internal static System.Drawing.Icon _48X48 {
get {
object obj = ResourceManager.GetObject("_48X48", resourceCulture);
return ((System.Drawing.Icon)(obj));
}
}
//图标2
internal static System.Drawing.Icon 无标题___1 {
get {
object obj = ResourceManager.GetObject("无标题___1", resourceCulture);
return ((System.Drawing.Icon)(obj));
}
}
}
//============类结束========================
}
we
48X48.ico
Hello.cs
Resources.resx
无标题 - 1.ico
-------------------------
Hello.cs
-------------using System;
using System.Drawing;//托盘小标用
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace we
{
/*==============主程序入口========================*/
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Console.ReadKey();
}
}
/*==========form1类--理解为窗口层===================*/
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//当然这里也可以直接写InitializeComponent()方法,现在是写到了partial class Form1中
/*加入托盘图片开始*/
int i = 0;
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true; }
/*
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
this.notifyIcon2.Icon = ((System.Drawing.Icon)(Resources._48X48));
}*/ //★有问题处1,这的Resources对吗?
private void timer1_Tick(object sender, EventArgs e)
{
if (i == 0)
{
this.notifyIcon2.Icon = ((System.Drawing.Icon)(Resources.无标题___1));
i = 1;
}
else
{
this.notifyIcon2.Icon = ((System.Drawing.Icon)(Resources._48X48));
i = 0;
}
}
//托盘图标事件结束
}
/*-==========form1类----窗口程序本体===========*/
partial class Form1
{
//主体变量名
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
//按扭变量名
private System.Windows.Forms.Button buttoww = new System.Windows.Forms.Button();
private System.Windows.Forms.Button button1= new System.Windows.Forms.Button();
//时间变理名
private System.Windows.Forms.Timer timer1;
//ico文件变量
public System.Windows.Forms.NotifyIcon notifyIcon2;
//窗体中显示
private void InitializeComponent()
{
//窗体
components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
notifyIcon2 = new System.Windows.Forms.NotifyIcon(this.components);
timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout(); // notifyIcon2
//★有问题处2,这里的resources好像不能用
notifyIcon2.Icon =((System.Drawing.Icon)(resources.GetObject("notifyIcon2.Icon")));
notifyIcon2.Text = "notifyIcon2";
notifyIcon2.Visible = true;
//1,bnt
buttoww.Location = new System.Drawing.Point(174, 205);
buttoww.Name = "checkBox1";
buttoww.Size = new System.Drawing.Size(75,23);
buttoww.TabIndex = 1;
buttoww.Text = "关上";
buttoww.UseVisualStyleBackColor = true;
buttoww.Click += new System.EventHandler(buttoww_Click);
//2,bnt
button1.Location = new System.Drawing.Point(12, 12);
button1.Name = "button1";
button1.Size = new System.Drawing.Size(100, 23);
button1.TabIndex = 0;
button1.Text = "闪动开始";
button1.UseVisualStyleBackColor = true;
button1.Click += new System.EventHandler(button1_Click);
//
// timer1
timer1.Interval = 400;
timer1.Tick += new System.EventHandler(timer1_Tick);
Text = "我的第一个程序";
//写入按扭
Controls.Add(button1);
Controls.Add(buttoww);
this.ResumeLayout(false);
}
//按扭响应事件
private void buttoww_Click(object sender, EventArgs e)
{
MessageBox.Show("还没有加数据");
}
}
//★有问题处3:如下写法不知道能不能用
//============Resources类读取Resources.resx文件再写入属性========================
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "2.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources()
{
//这类的头
}
//读取Resources.resx文件
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("we.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// 重写当前线程的 CurrentUICulture 属性。
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
//图标1
internal static System.Drawing.Icon _48X48 {
get {
object obj = ResourceManager.GetObject("_48X48", resourceCulture);
return ((System.Drawing.Icon)(obj));
}
}
//图标2
internal static System.Drawing.Icon 无标题___1 {
get {
object obj = ResourceManager.GetObject("无标题___1", resourceCulture);
return ((System.Drawing.Icon)(obj));
}
}
}
//============类结束========================
}
private void timer1_Tick(object sender, EventArgs e)
{
if (i == 0)
{
this.notifyIcon2.Icon = ((System.Drawing.Icon)(Resources.无标题___1));
i = 1;
}
else
{
this.notifyIcon2.Icon = ((System.Drawing.Icon)(Resources._48X48));
i = 0;
}
}
確實有問題!
你在resource加入兩張圖片.ico,然後來回切換就行了。不需要去轉類型!
this.notifyIcon2.Icon = WindowsApplication1.Properties.Resources.icon2;就行了
----------------<?xml version="1.0" encoding="utf-8"?>
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<data name="_48X48" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>48x48.ico;System.Drawing.Icon, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="无标题___1" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>无标题 - 1.ico;System.Drawing.Icon, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>