Winform 单例模式 如果有相同的程序在运行 notifyIcon气泡提示 “程序正在运行,我在这儿”
2012-01-18 10:01
513 查看
如题 Winform 单例模式
如果用相同的程序在运行 notifyIcon系统托盘图标 气泡提示 “程序正在运行,我在这儿”
重点是 如何实现 : 当检测到有程序运行时 如何获取该窗口 并调用notifyIcon的气泡提示
用 windows API实现 或用 反射 只要能实现就行。。。
昵称: surfshark5 时间: 2010-06-20 20:50:01
其实不需要直接调用API,也不需要反射:)
C# code
// Program.cs
//
using System;
using System.Threading;
using System.Windows.Forms;
static class Program
{
public static EventWaitHandle ProgramStarted;
[STAThread]
static void Main()
{
// 尝试创建一个命名事件
bool createNew;
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyStartEvent", out createNew);
// 如果该命名事件已经存在(存在有前一个运行实例),则发事件通知并退出
if (!createNew)
{
ProgramStarted.Set();
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
C# code
// Form1.cs
//
using System;
using System.Windows.Forms;
using System.Threading;
public partial class Form1 : Form
{
NotifyIcon notifyIcon1 = new NotifyIcon();
public Form1()
{
//InitializeComponent();
this.notifyIcon1.Text = "Double click me to show window";
this.notifyIcon1.Icon = System.Drawing.SystemIcons.Information;
this.notifyIcon1.DoubleClick += OnNotifyIconDoubleClicked;
this.SizeChanged += OnSizeChanged;
ThreadPool.RegisterWaitForSingleObject(Program.ProgramStarted, OnProgramStarted, null, -1, false);
}
// 当最小化时,放到系统托盘。
void OnSizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.Visible = false;
}
}
// 当双击托盘图标时,恢复窗口显示
void OnNotifyIconDoubleClicked(object sender, EventArgs e)
{
this.Visible = true;
this.notifyIcon1.Visible = false;
this.WindowState = FormWindowState.Normal;
}
// 当收到第二个进程的通知时,显示气球消息
void OnProgramStarted(object state, bool timeout)
{
this.notifyIcon1.ShowBalloonTip(2000, "Hello", "I am here...", ToolTipIcon.Info);
}
}
讨论见原帖(如何操作我的程序的另一个实例(进程)):
http://topic.csdn.net/u/20081221/01/457bb3b1-2f19-47e2-9621-cf4117ee45ce.html 昵称: gomoku 时间: 2010-06-21 06:22:23
引用 1 楼 gomoku 的回复:
其实不需要直接调用API,也不需要反射:)
C# code
// Program.cs
//
using System;
using System.Threading;
using System.Windows.Forms;
static class Program
{
public static EventWaitHandle ProgramStarted;……
昵称: xiaoqiu1234 时间: 2010-06-21 07:59:41
直接使用VB.NET的部分API也可以实现,
http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicrosoftVisualBasicdll.aspx
昵称: lextm 时间: 2010-06-21 14:16:01
非常感谢 gomoku 写的很好 问题解决了
但其中涉及到 命名事件 和 多线程的 一些类 以前没有接触过 好像不太好懂
查了些资料 都很生涩
能不能简单的用比较通俗的语言讲一下 这个程序中用到的 方法 尤其是各参数的意思
算是为了解更多入个门吧 多谢了
C# code
ThreadPool.RegisterWaitForSingleObject(Program.ProgramStarted, OnProgramStarted, null, -1, false);
C# code
// 尝试创建一个命名事件
bool createNew;
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyStartEvent", out createNew);
// 如果该命名事件已经存在(存在有前一个运行实例),则发事件通知并退出
if (!createNew)
{
ProgramStarted.Set();
return;
}
昵称: surfshark5 时间: 2010-06-21 16:58:17
引用 1 楼 gomoku 的回复:
其实不需要直接调用API,也不需要反射:)
C# code
// Program.cs
//
using System;
using System.Threading;
using System.Windows.Forms;
static class Program
{
public static EventWaitHandle ProgramStarted;
[STA……
e。。。nb 我的思路还是便利所有的线程 然后通过ProcessName判断呢。。
如果用相同的程序在运行 notifyIcon系统托盘图标 气泡提示 “程序正在运行,我在这儿”
重点是 如何实现 : 当检测到有程序运行时 如何获取该窗口 并调用notifyIcon的气泡提示
用 windows API实现 或用 反射 只要能实现就行。。。
昵称: surfshark5 时间: 2010-06-20 20:50:01
其实不需要直接调用API,也不需要反射:)
C# code
// Program.cs
//
using System;
using System.Threading;
using System.Windows.Forms;
static class Program
{
public static EventWaitHandle ProgramStarted;
[STAThread]
static void Main()
{
// 尝试创建一个命名事件
bool createNew;
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyStartEvent", out createNew);
// 如果该命名事件已经存在(存在有前一个运行实例),则发事件通知并退出
if (!createNew)
{
ProgramStarted.Set();
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
C# code
// Form1.cs
//
using System;
using System.Windows.Forms;
using System.Threading;
public partial class Form1 : Form
{
NotifyIcon notifyIcon1 = new NotifyIcon();
public Form1()
{
//InitializeComponent();
this.notifyIcon1.Text = "Double click me to show window";
this.notifyIcon1.Icon = System.Drawing.SystemIcons.Information;
this.notifyIcon1.DoubleClick += OnNotifyIconDoubleClicked;
this.SizeChanged += OnSizeChanged;
ThreadPool.RegisterWaitForSingleObject(Program.ProgramStarted, OnProgramStarted, null, -1, false);
}
// 当最小化时,放到系统托盘。
void OnSizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.Visible = false;
}
}
// 当双击托盘图标时,恢复窗口显示
void OnNotifyIconDoubleClicked(object sender, EventArgs e)
{
this.Visible = true;
this.notifyIcon1.Visible = false;
this.WindowState = FormWindowState.Normal;
}
// 当收到第二个进程的通知时,显示气球消息
void OnProgramStarted(object state, bool timeout)
{
this.notifyIcon1.ShowBalloonTip(2000, "Hello", "I am here...", ToolTipIcon.Info);
}
}
讨论见原帖(如何操作我的程序的另一个实例(进程)):
http://topic.csdn.net/u/20081221/01/457bb3b1-2f19-47e2-9621-cf4117ee45ce.html 昵称: gomoku 时间: 2010-06-21 06:22:23
引用 1 楼 gomoku 的回复:
其实不需要直接调用API,也不需要反射:)
C# code
// Program.cs
//
using System;
using System.Threading;
using System.Windows.Forms;
static class Program
{
public static EventWaitHandle ProgramStarted;……
昵称: xiaoqiu1234 时间: 2010-06-21 07:59:41
直接使用VB.NET的部分API也可以实现,
http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicrosoftVisualBasicdll.aspx
昵称: lextm 时间: 2010-06-21 14:16:01
非常感谢 gomoku 写的很好 问题解决了
但其中涉及到 命名事件 和 多线程的 一些类 以前没有接触过 好像不太好懂
查了些资料 都很生涩
能不能简单的用比较通俗的语言讲一下 这个程序中用到的 方法 尤其是各参数的意思
算是为了解更多入个门吧 多谢了
C# code
ThreadPool.RegisterWaitForSingleObject(Program.ProgramStarted, OnProgramStarted, null, -1, false);
C# code
// 尝试创建一个命名事件
bool createNew;
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyStartEvent", out createNew);
// 如果该命名事件已经存在(存在有前一个运行实例),则发事件通知并退出
if (!createNew)
{
ProgramStarted.Set();
return;
}
昵称: surfshark5 时间: 2010-06-21 16:58:17
引用 1 楼 gomoku 的回复:
其实不需要直接调用API,也不需要反射:)
C# code
// Program.cs
//
using System;
using System.Threading;
using System.Windows.Forms;
static class Program
{
public static EventWaitHandle ProgramStarted;
[STA……
e。。。nb 我的思路还是便利所有的线程 然后通过ProcessName判断呢。。
相关文章推荐
- Winform 单例模式 如果有相同的程序在运行 notifyIcon气泡提示 “程序正在运行,我在这儿”
- C#之使用NotifyIcon实现任务栏托盘菜单,图标闪烁效果及气泡提示 很多程序是只需要后台运行的,甚至不需要自己的应用界面。NotifyIcon提供了程序在任务栏的显示功能 程序下载链接如下
- 移动硬盘分区变为RAW,chkdsk提示“因另一个程序正在使用这个卷,无法运行CHKDSK“ 的解决办法 .
- 在运行程序时报错:"如果在 Code First 模式下使用,则使用 T4 模板为 Database First 和 Model First 开发生成的代码可能无法 正常运行。若要继续使用 Database First 或 Model First,请确保在执行应用程序的 config 文件中指 定 Entity Framework 连接字符串。若要将这些从 Database First 或 Mod
- c# winform程序右下角显示气泡提示效果
- Chrome提示:"请停用以开发者模式运行的扩展程序"的解决办法
- 使用mysql的图形工具MySQL-Front时弹出提示:“程序注册时间到期 程序将被限制模式下运行”
- CentOS5.8下用“软件包安装工具安装”总是提示:另一个目前运行的程序正在访问软件信息。
- 关闭Chrome安装第三方插件之后反复提示“请停用以开发者模式运行的扩展程序”
- 关于VS2013连接Oracle数据库提示:“尝试加载oracle客户端时引发badimage,如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题”的解决方案。
- winform 判断程序是否已经运行,提示“只允许运行一个程序”
- java——获取一个应用程序运行的次数,如果超过5次,给出使用次数已到请注册的提示,并不要再运行程序
- VS2008 Debug模式下编译完程序运行时提示"没有找到MSVCR90D.dll..."的解决办法
- 在窗口中执行mysql命令时,如果提示:mysql不是内部或外部命令,也不是可运行的程序或批处理文件
- Mac OS X 下 Chrome 去掉第三方扩展提示“请停用以开发者模式运行的扩展程序”的方法
- win7上运行某程序的时候,如果这个程序需要以管理员的身份运行,那么就会有“用户帐户控制”的提示:“您想允许来自...发布者的以下程序对此计算机进行更改吗?”请问,win7如何知道哪些程序需要以管理员的身份运行?
- Wix CloseApplication 安装时提示xxx程序正在运行
- 使用mysql的图形工具SQL-front时弹出提示:“程序注册时间到期 程序将被限制模式下运行”
- [转]关闭 Chrome 浏览器的启动时提示 - 请停用以开发者模式运行的扩展程序
- 忽然想到的 —— 如果我们生活的世界是一个正在运行的程序