begin
If FindWindow('TForm1','SingleApp')=0 then
Begin
Application.Initialize;
Application.CreateForm(Tform1,Form1);
Application.Run;
end
else
begin
MessageBox(0,'程序已经运行,请注意系统托盘','',mb_OK);
Application.Terminate;
end
end.希望达到的效果:当程序运行时,如果再次激发程序,则自动弹出消息框“程序已经运行,请注意系统托盘”,当单击消息框的确定按钮后,除自动显示程序窗体Form1外,无其他操作。
我遇到的问题:当把这一组begin~end放在所有procedure之前时,因为遇到“end.”所以后面的procedure全都无效;当我把end后的“.”变成“;”时,提示“[Error]: '.' expected but ';' found”;当我把这一组begin~end放在所有procedure之后时,这组begin~end一点儿作用都起不到。
这段程序到底怎么处理啊?
If FindWindow('TForm1','SingleApp')=0 then
Begin
Application.Initialize;
Application.CreateForm(Tform1,Form1);
Application.Run;
end
else
begin
MessageBox(0,'程序已经运行,请注意系统托盘','',mb_OK);
Application.Terminate;
end
end.希望达到的效果:当程序运行时,如果再次激发程序,则自动弹出消息框“程序已经运行,请注意系统托盘”,当单击消息框的确定按钮后,除自动显示程序窗体Form1外,无其他操作。
我遇到的问题:当把这一组begin~end放在所有procedure之前时,因为遇到“end.”所以后面的procedure全都无效;当我把end后的“.”变成“;”时,提示“[Error]: '.' expected but ';' found”;当我把这一组begin~end放在所有procedure之后时,这组begin~end一点儿作用都起不到。
这段程序到底怎么处理啊?
解决方案 »
- DELPHI7 调用外部dll问题?
- 使用oleContainer遇到一个奇怪的异常
- 关于Form的ShowModal问题!
- 我现在想把数据一下子放到某一个对应表中,而不是一条一条的检索着往里放,请问QUERY有这个功能吗?
- 100分求《奇思异想编程序 delphi篇》一书!得到即给分!!!!万谢!!!1
- 刚接到一项目:公共汽车调度系统,不知从何下手?请各位前辈给点资料……
- 请问如何解决delphi中动画刷新的问题?????
- 如何计算代码行数?
- DBlookupcombobox等的问题?
- 考验真正的delphi程序员高手!!!
- 找人帮忙写一个架设socks代理服务器的模块,支付宝
- 如何在文件中寻找特定字符的位置?
begin
If FindWindow('TForm1','SingleApp')=0 then
Begin
Application.Initialize;
Application.CreateForm(Tform1,Form1);
Application.Run;
end
else
在判断FindWindow('TForm1','SingleApp')这个肯定不会=0的,因为这个你的程序已经运行起来,
begin
ShowWindow(FindWindowEx(0,0,nil,'提示'), SW_RESTORE);
exit;
end;
你试试可行?
我贴一段源码给你,希望对你有帮助
在program文件中
const //for singleton(it must uses messages,windows)
CM_RESTORE = WM_USER + $1000; {自定义的"恢复"消息}
MYAPPNAME = 'PearlMIS';
{$R *.res}
var
pHandle:Hwnd;
begin
//singleton
pHandle := FindWindow(MYAPPNAME, NIL); //checkout if there is an instance existing already
if pHandle > 0 then //if exists
begin
PostMessage(pHandle, CM_RESTORE, 0, 0); //restore it
Exit;
end;
Application.Initialize;
Application.CreateForm(TfrmControl, frmControl);
Application.ShowMainForm:=false;
Application.CreateForm(TfrmSplash, frmSplash);
frmSplash.Show;
frmSplash.Update;
Sleep(1500);
frmSplash.Hide;
frmSplash.Free;
Application.ShowMainForm:=true;
Application.Run;
end.
在主窗体中需要添加两个消息处理
const //for singleton (it must uses shellAPI)
CM_RESTORE = WM_USER + $1000; {自定义的"恢复"消息}
MYAPPNAME = 'PearlMIS'; procedure CreateParams(var Params: TCreateParams); override;
Procedure RestoreRequest(var message: TMessage); message CM_RESTORE;procedure TfrmMain.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.WinClassName := MYAPPNAME;
end;procedure TfrmMain.RestoreRequest(var message: TMessage);
begin
if IsIconic(Application.Handle) = TRUE then
Application.Restore
else
Application.BringToFront;
end;
var
MHandle : Cardinal;
begin
MHandle := CreateMutex(nil,true,'DataTransfer');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
Application.MessageBox(pchar('DataTransfer Application is Running!!!!'),'Warning',MB_OK+MB_ICONERROR);
Application.Terminate;
exit;
end;
If FindWindow('TForm1','SingleApp')=0 then
Begin
Application.Initialize;
Application.CreateForm(Tform1,Form1);
Application.Run;
end
else
begin
MessageBox(0,'程序已经运行,请注意系统托盘','',mb_OK);
Application.Terminate;
end;
end. else begin ...end后面少了一个分号
怎么建啊???
还是不知道怎么处理这段程序和procedure之间的关系,能给个完整的程序么,从uses开始的???
我这不是单实例啊。
unit PrevInst;interfaceuses
Windows, Classes, SysUtils;type
TMethod = ( mdAuto, mdManual );
TMethodStr = array [0..1] of string; TPrevInst = class(TObject)
private
{ Private declarations }
FhSem: THandle; // Semaphore handle
protected
{ Protected declarations }
public
{ Public declarations }
destructor Destroy(); override;
function CreateAtom(Semaphore: string): THandle;
procedure FreeAtom();
end;implementationdestructor TPrevInst.Destroy;
begin
if FhSem <> 0 then
CloseHandle(FhSem);
inherited;
end;procedure TPrevInst.FreeAtom();
begin
if FhSem <> 0 then
begin
CloseHandle(FhSem);
FhSem := 0;
end;
end;function TPrevInst.CreateAtom(Semaphore: string): THandle; // return handle
var // return 0 if found prev instance
thSem: THandle;
// hWndMe, hWndPrev: HWnd;
SemName: array [0..255] of Char;
begin
StrPCopy(SemName, Semaphore);
Result := 0;//fail
thSem := CreateSemaphore(nil, 0, 1, semName);
if thSem <> 0 then
if GetLastError() = ERROR_ALREADY_EXISTS then
begin
CloseHandle(thSem);
Result := 0;//fail
end
else
begin
FhSem := thSem;
Result := thSem;//success
end;
end;end.
用法:
var
prev: TPrevInst;begin
prev := TPrevInst.Create();
if prev.CreateAtom('TNetBarCltSema') = 0 then
begin
// 已经运行
end
else
begin
// 还没运行
end
end.
现成的演示互斥的例子
frmMain: TfrmMain;
Mutex: HWnd; {全局变量,钩子作用}function CreateMutex: Boolean;
procedure DestroyMutex;implementationfunction CreateMutex: Boolean;
var
PrevInstHandle: THandle;
AppTitle: Pchar;
begin
AppTitle := StrAlloc(100);
StrPCopy(AppTitle, Application.Title);
Result := True;
Mutex := Windows.CreateMutex(nil, False, AppTitle);
if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then
begin
Result := False;
SetWindowText(Application.Handle, '');
PrevInstHandle := FindWindow(nil, AppTitle);
if PrevInstHandle <> 0 then
begin
if IsIconic(PrevInstHandle) then
ShowWindow(PrevInstHandle, SW_RESTORE)
else
BringWindowToTop(PrevInstHandle);
SetForegroundWindow(PrevInstHandle);
end; if Mutex <> 0 then
Mutex := 0;
end; StrDispose(AppTitle);end;procedure DestroyMutex;
begin
if Mutex <> 0 then
CloseHandle(Mutex);
end;program 中begin
Application.Initialize;
Application.Title := '你的标题';
if CreateMutex then
begin
Application.CreateForm(TfrmMain, frmMain);
Application.Run;
end
else
begin
// Application.Messagebox('本程序正在运行当中,请检查任务栏!', '提示', MB_OK + MB_ICONINFORMATION); //提示程序已运行
DestroyMutex;
end;
代码是通用的,复制到你想要的程序里即可
var
hMutex: HWND;
Ret: Integer;begin
Application.Initialize;
Application.Title := '标题;
hMutex := CreateMutex(nil, False, '标题');
Ret := GetLastError;
if Ret = ERROR_ALREADY_EXISTS then
begin
showmessage('提示的信息');
ReleaseMutex(hMutex);
Application.Terminate;
exit;
end;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
uses里加Dialogs;
If FindWindow('TForm1','SingleApp')=0 then
中'SingleApp'应该为'Form1',
我已经验证过