用C#编写了一个服务, 怎么让它在托盘上出现一个图标,当点击这个图标时,显示出一个窗体?

241219 2003-10-20 10:30:21

我是在onStart中加入了一个NotifyIcon,指定属性.
在工程中加入了一个windows窗体.
在NotifyIcon事件中显示该窗体,但是总是不成功.
该怎么办呢?
...全文
121 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
LeafK 2003-10-22
  • 打赏
  • 举报
回复
上面的,高手呀,厉害,正在研究中!!
cauchyyy 2003-10-22
  • 打赏
  • 举报
回复
i tried it, it works! but maybe you need to change it a little bit!
cauchyyy 2003-10-22
  • 打赏
  • 举报
回复
public class FormInvisible : System.Windows.Forms.Form
{
private System.Windows.Forms.NotifyIcon notifyIcon;
private System.ComponentModel.IContainer components;

public FormInvisible()
{
InitializeComponent();

string tip = "test notify icon ";
notifyIcon.Text = tip;
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormInvisible));
this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
//
// notifyIcon
//
this.notifyIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon.Icon")));
this.notifyIcon.Text = "test";
this.notifyIcon.Visible = true;
this.notifyIcon.MouseDown += new System.Windows.Forms.MouseEventHandler(this.notifyIcon_MouseDown);
this.notifyIcon.MouseUp += new System.Windows.Forms.MouseEventHandler(this.notifyIcon_MouseUp);
//
// FormInvisible
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(168, 160);
this.ControlBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FormInvisible";
this.ShowInTaskbar = false;
this.Text = "FormInvisible";
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
this.Load += new System.EventHandler(this.FormInvisible_Load);

}
private void ExitServer()
{
Close();
Application.Exit();
}

MouseButtons buttonDown = MouseButtons.None;

private void vkService_ExitCommand(object sender, System.EventArgs e)
{
ExitServer();
}

private void notifyIcon_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
buttonDown = e.Button;
}

private void notifyIcon_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && buttonDown == MouseButtons.Right)
ExitServer();

if (e.Button == MouseButtons.Left && buttonDown == MouseButtons.Left)
Form2().show();// to show another form

buttonDown = MouseButtons.None;
}

private void FormInvisible_Load(object sender, System.EventArgs e)
{
Hide();
}
}
cauchyyy 2003-10-22
  • 打赏
  • 举报
回复
no, this could be used on windows service, infact i already used it in windows service, and it worked very good!
williamq 2003-10-22
  • 打赏
  • 举报
回复
问题有点复杂,十恶讲得没错,可惜他的code只能用在form上,windows service是不行的。除非enable allow serivce interact with desktop. 那你就要在install中加一个action to enable it. 或者自己编一个install.
chmj718 2003-10-21
  • 打赏
  • 举报
回复
up

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧