zoukankan      html  css  js  c++  java
  • Delphi 防止程序多次运行《转》

    //防止多次打开
    unit
    MultInst; interface uses Windows ,Messages, SysUtils, Classes, Forms; implementation const STR_UNIQUE = '{2BE6D96E-827F-4BF9-B33E-8740412CDE96}'; MI_ACTIVEAPP = 1; {激活应用程序} MI_GETHANDLE = 2; {取得句柄} var iMessageID : Integer; OldWProc : TFNWndProc; MutHandle : THandle; BSMRecipients : DWORD; function NewWndProc(Handle: HWND; Msg: Integer; wParam, lParam: Longint): Longint; stdcall; begin Result := 0; if Msg = iMessageID then begin case wParam of MI_ACTIVEAPP: {激活应用程序} if lParam<>0 then begin {收到消息的激活前一个实例} {为什么要在另一个程序中激活?} {因为在同一个进程中SetForegroundWindow并不能把窗体提到最前} if IsIconic(lParam) then OpenIcon(lParam) else SetForegroundWindow(lParam); Application.Terminate; {终止本实例} end; MI_GETHANDLE: {取得程序句柄} begin PostMessage(HWND(lParam), iMessageID, MI_ACTIVEAPP, Application.Handle); end; end; end else Result := CallWindowProc(OldWProc, Handle, Msg, wParam, lParam); end; procedure InitInstance; begin {取代应用程序的消息处理} OldWProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC, Longint(@NewWndProc))); {打开互斥对象} MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, STR_UNIQUE); if MutHandle = 0 then begin {建立互斥对象} MutHandle := CreateMutex(nil, False, STR_UNIQUE); end else begin Application.ShowMainForm := False; {已经有程序实例,广播消息取得实例句柄} BSMRecipients := BSM_APPLICATIONS; BroadcastSystemMessage(BSF_POSTMESSAGE or BSF_POSTMESSAGE,@BSMRecipients,iMessageID,MI_GETHANDLE,Application.Handle); //BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE, @BSMRecipients, iMessageID, MI_GETHANDLE,Application.Handle); end; end; initialization {注册消息} iMessageID := RegisterWindowMessage(STR_UNIQUE); InitInstance; finalization {还原消息处理过程} if OldWProc <> Nil then SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(OldWProc)); {关闭互斥对象} if MutHandle <> 0 then CloseHandle(MutHandle); end.

    在XE7下测试通过.

  • 相关阅读:
    学习优化:训练深度神经网络进行无线资源管理
    (经典文章uplink)Information capacity and power control in single-cell multiuser communications(1995)
    Python运行出错
    2019之VLC3.071版本Ubuntu 18-win32-64为编译经验记录
    Ansys-CHEMKIN-pro表面反应机制输入(Surface Kinetics Input)规则
    tomcat9配置https
    tomcat和iis共用80端口的简明手册
    ANSYS经典APDL编程
    在ANSYS WORKBENCH中使用APDL命令的例子
    现代工程仿真CAE技术介绍
  • 原文地址:https://www.cnblogs.com/LceMeaning/p/5655523.html
Copyright © 2011-2022 走看看