激活前一个程序(注册全局消息,使用Mutex探测,如果已经占用就广播消息通知第一个程序,然后第一个程序做出响应)
unit MultInst; interface const
MI_QUERYWINDOWHANDLE = ;
MI_RESPONDWINDOWHANDLE = ; MI_ERROR_NONE = ;
MI_ERROR_FAILSUBCLASS = ;
MI_ERROR_CREATINGMUTEX = ; // Call this function to determine if error occurred in startup.
// Value will be one or more of the MI_ERROR_* error flags.
function GetMIError: Integer; implementation uses Forms, Windows, SysUtils; const
UniqueAppStr = 'DDG.I_am_the_Eggman!'; var
MessageId: Integer;
WProc: TFNWndProc;
MutHandle: THandle;
MIError: Integer; function GetMIError: Integer;
begin
Result := MIError;
end; function NewWndProc(Handle: HWND; Msg: Integer; wParam, lParam: Longint):
Longint; stdcall;
begin
Result := ;
// If this is the registered message...
if Msg = MessageID then
begin
case wParam of
MI_QUERYWINDOWHANDLE:
// A new instance is asking for main window handle in order
// to focus the main window, so normalize app and send back
// message with main window handle.
begin
if IsIconic(Application.Handle) then
begin
Application.MainForm.WindowState := wsNormal;
Application.Restore;
end;
PostMessage(HWND(lParam), MessageID, MI_RESPONDWINDOWHANDLE,
Application.MainForm.Handle);
end;
MI_RESPONDWINDOWHANDLE:
// The running instance has returned its main window handle,
// so we need to focus it and go away.
begin
SetForegroundWindow(HWND(lParam));
Application.Terminate;
end;
end;
end
// Otherwise, pass message on to old window proc
else
Result := CallWindowProc(WProc, Handle, Msg, wParam, lParam); // API
end; procedure SubClassApplication;
begin
// We subclass Application window procedure so that
// Application.OnMessage remains available for user.
WProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC,
Longint(@NewWndProc)));
// Set appropriate error flag if error condition occurred
if WProc = nil then
MIError := MIError or MI_ERROR_FAILSUBCLASS;
end; procedure DoFirstInstance;
// This is called only for the first instance of the application
begin
// Create the mutex with the (hopefully) unique string
MutHandle := CreateMutex(nil, False, UniqueAppStr);
if MutHandle = then
MIError := MIError or MI_ERROR_CREATINGMUTEX;
end; procedure BroadcastFocusMessage;
// This is called when there is already an instance running.
var
BSMRecipients: DWORD;
begin
// Prevent main form from flashing
Application.ShowMainForm := False;
// Post message to try to establish a dialogue with previous instance
BSMRecipients := BSM_APPLICATIONS;
BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE, // API
@BSMRecipients, MessageID, MI_QUERYWINDOWHANDLE,
Application.Handle);
end; procedure InitInstance;
begin
SubClassApplication; // hook application message loop
MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, UniqueAppStr);
if MutHandle = then
// Mutex object has not yet been created, meaning that no previous
// instance has been created.
DoFirstInstance
else
BroadcastFocusMessage;
end; initialization
MessageID := RegisterWindowMessage(UniqueAppStr); // API
InitInstance;
finalization
// Restore old application window procedure
if WProc <> Nil then
SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(WProc));
if MutHandle <> then CloseHandle(MutHandle); // Free mutex
end.
激活前一个程序(注册全局消息,使用Mutex探测,如果已经占用就广播消息通知第一个程序,然后第一个程序做出响应)的更多相关文章
- Android应用程序注册广播接收器(registerReceiver)的过程分析
前 面我们介绍了Android系统的广播机制,从本质来说,它是一种消息订阅/发布机制,因此,使用这种消息驱动模型的第一步便是订阅消息:而对 Android应用程序来说,订阅消息其实就是注册广播接收器, ...
- 微信小程序:全局配置app.json
微信小程序:全局配置app.json 一.全局配置app.json app.json文件用来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 等. 以下是一个包 ...
- 微信小程序注册和简单配置
微信小程序注册 1.直接搜索微信小程序,按照流程进行注册 2.如果有微信公众号,可以在公众号内部点小程序,进入注册流程 小程序中的概念 开发设置 在开发设置中获取AppID和AppSecret App ...
- 使用wrapper将java程序注册程windows服务后不生效
使用wrapper将java程序注册程windows服务后不生效 使用add.bat或test***.bat测试通过了, 然后使用install***.bat注册后cmd显示注册成功. 但是程序到了运 ...
- MySQL-Front 出现“程序注册时间到期 程序将被限制模式下运行”解决方式
MySQL-Front 出现“程序注册时间到期 程序将被限制模式下运行”解决方式 在用mysql-front的时候遇到显示:程序注册时间到期程序将被限制模式下运行.可以在“帮助”菜单下的点“登记”-- ...
- 微信小程序开发系列(二)小程序的全局文件
其实你已经知道了小程序的文件结构 上一节讲到,小程序的页面由三部分组成: 视图(.wxml).逻辑(.js).样式(.wxss). 我们这次重新展开文件结构: 小程序用到的文件类型只有四种,正如你所看 ...
- Windows 程序注册成服务的方法
Windows 程序注册成服务的方法 将windows 程序注册成服务这个是很多后台程序需要实现的功能,注册成服务后,你的程序就可以像windows 服务一样随系统启动,并且隐藏你的控制台界面.下面介 ...
- 中国版Office 365 应用程序注册
作者:陈希章 发表于 2017年3月23日 中国版Office 365是由世纪互联进行运营的一个云服务,单纯从技术角度来看的话,它基本保持了与国际版的同步.但是由于两个版本本质上是完全独立的,其中最关 ...
- 微信小程序设置全局字体
微信小程序设置全局css,需要在app.wxss文件中设置page的样式 page { font-family:"PingFangSC-Thin"; font-size:32rpx ...
随机推荐
- cygwin安装与使用
cygwin安装很简单,下载运行setup.exe程序,一步一步就可以了. 具体安装细节参考:http://www.33lc.com/article/7276.html 安装完成后有如下问题: 在cm ...
- /proc/uptime
在Linux中,我们常常会使用到uptime命令去看看系统的运行时间,它与一个文件有关,就是/proc/uptime.这个文件里的两个参数所代表的意义如下. [root@app ~]#cat /pro ...
- c#提出中文首字母
; i < len; i++) { myStr += getSpell(strText.Substring(i, )); ...
- js 写table 函数
//创建 table函数 function table(row,col,b,w) { document.write('<table border='+b+'>'); for(var i=0 ...
- Linux各种包安装及命令
1.Locate yum -y install mlocate 若出现问题: locate: can not stat () `/var/lib/mlocate/mlocate.db': 没有那个文件 ...
- python高级编程(第12章:优化学习)3
#微观剖析 ''' 当找到速度很慢函数时,有时还需要做到测试某个部分函数剖析工作,这需要通过手动对一部分代码速度测试完成 ''' """ import tempfile, ...
- IOS基于新浪微博开放平台微博APP
1.基于新浪微博开放平台APP源码 2.gitHub源代码下载地址 https://github.com/whzhaochao/SinaWeiBoOpen 3.用到的第三放开源库 3.1 RTLab ...
- NET基础课--泛型(NET之美)
1.泛型,类型或方法的一种抽象概括. 2.泛型类:在类型名后面加一个<>,其中传递占位符,也就是类型参数.where是类型约束 可以再查资料 public class SortHelper ...
- CSS---------------之文本颜色
CSS2支持如下名字的颜色 注意点: 你的浏览器有可能支持更多名字,但是在实际用的过程中尽量少使用名字的,因为各个浏览器对颜色的会存在差异:查看颜色可以参考 对于更多地颜色,你可以使用代表红,绿,蓝三 ...
- ScrollView 的使用(非原创)
1.ScrollView滚动视图是指当拥有很多内容,屏幕显示不完时,需要通过滚动跳转来显示的视图.ScrollView只支持垂直滚动 <ScrollView xmlns:android=&quo ...