怎么样做到只能打开一个程序实例?
编译成可执行程序后,
可以打开好多个。
请问怎么做才能做到只能打开一个程序实例
问题点数:0、回复次数:12Top
1 楼cjdbd()回复于 2003-06-01 12:47:37 得分 0
请问怎么做到呢?
我运行了一次dd.exe,打开这个dd
我又运行一次dd.exe ,又打开这个dd
那么就出现了两个dd了?
我现在只想让它运行一个??
Top
2 楼cjdbd()回复于 2003-06-01 13:42:08 得分 0
请问各位到底怎么才能实现呢?Top
3 楼cjdbd()回复于 2003-06-01 13:50:48 得分 0
请问能不能实现呢????
还是没有办法实现?Top
4 楼delphimo((误入歧途))回复于 2003-06-01 13:53:45 得分 0
搜索一下帖子太多了!
下次记着给分,大家很势利阿!Top
5 楼cjdbd()回复于 2003-06-01 13:58:30 得分 0
我也很想给的啊。
但是我都给完了。
要到明天才能有分。
我是有分必给的。Top
6 楼pilicat(Delphi迷)回复于 2003-06-01 14:04:46 得分 0
能帮就帮你一把,请参阅下文:
http://lzw222.myrice.com/1-BianChengJiQiao/BCJQ-WZ/BCJQ20.htm
Top
7 楼ahyf(笨笨)回复于 2003-06-01 15:10:35 得分 0
参考这个
Var
hWindow : HWND;
begin
Application.Initialize;
hWindow:=FindWindow(nil,pchar('myup-eHR人力资源管理系统'));
if isWindow(hWindow) then
Begin
hwindow:=getwindow(hwindow,GW_OWNER);
hwindow:=getlastactivepopup(hWindow);
if isiconic(hwindow) then
openicon(hwindow);
setforegroundwindow(hwindow);
ShowMessage('程序已经运行');
application.terminate;
exit;
end
else
Begin
Application.CreateForm(TTuhrMain, TuhrMain);
Application.CreateForm(TF_ChangeUser, F_ChangeUser);
Application.CreateForm(TLogin, Login);
Application.CreateForm(TAboutBox, AboutBox);
Application.Title:='myup-eHR人力资源管理系统';
Application.Run;
end;
end.Top
8 楼byc6352(雪原)回复于 2003-06-01 16:04:50 得分 0
在程序开始运行时,用:
FindWindow(nil,pchar(‘主程序窗体标题’))
查找操作系统中有无本程序在运行,如果有:
application.terminate;
exit;
这段代码可放在项目文件开始或主程序窗体创建中。
Top
9 楼yayx(yayx)回复于 2003-06-01 17:06:30 得分 0
用互斥对象
这个以前太多了 你又不给分 所以你自己搜索一下吧Top
10 楼meica(赵一人)回复于 2003-06-01 17:11:57 得分 0
用控件,如justone等Top
11 楼eastnofail(Char *)回复于 2003-06-01 17:26:36 得分 0
用互斥对象:mutex,用windows的api函数createMutex函数创建一个互斥对象,每次程序一开始运行的时候判断这个mutex是否存在,如果存在则用findwindow找出程序的窗口,然后显示出来,如果不存在,则.....
你看看msdn,上面有craetemutex的用法Top
12 楼luo521(黑暗中的舞者)回复于 2003-06-01 17:33:10 得分 0
去看delphi5开发人员指南
把下面的程序加入到你的程序里就行了
unit MultInst;
interface
const
MI_QUERYWINDOWHANDLE = 1;
MI_RESPONDWINDOWHANDLE = 2;
MI_ERROR_NONE = 0;
MI_ERROR_FAILSUBCLASS = 1;
MI_ERROR_CREATINGMUTEX = 2;
// 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 := 0;
// 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);
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 = 0 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,
@BSMRecipients, MessageID, MI_QUERYWINDOWHANDLE,
Application.Handle);
end;
procedure InitInstance;
begin
SubClassApplication; // hook application message loop
MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, UniqueAppStr);
if MutHandle = 0 then
// Mutex object has not yet been created, meaning that no previous
// instance has been created.
DoFirstInstance
else
BroadcastFocusMessage;
end;
initialization
MessageID := RegisterWindowMessage(UniqueAppStr);
InitInstance;
finalization
// Restore old application window procedure
if WProc <> Nil then
SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(WProc));
if MutHandle <> 0 then CloseHandle(MutHandle); // Free mutex
end.
Top




