-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToDoList.dpr
80 lines (65 loc) · 1.56 KB
/
ToDoList.dpr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// アプリケーションの多重起動を禁止して起動中なら切り替える
// http://owlsperspective.blogspot.com/2008/11/avoiding-multiple-instance.html
program ToDoList;
uses
// FastMM4,
Windows,
SysUtils,
Messages,
Vcl.Forms,
ToDoList_Unit1 in 'ToDoList_Unit1.pas' {Form1},
Setting_Unit2 in 'Setting_Unit2.pas' {Form2};
{$R *.res}
var
{ Mutex name }
CMutexName: String;
hMutex: THandle;
Wnd: HWnd;
AppWnd: HWnd;
begin
CMutexName := 'arigayas_ToDoList';
{$IFDEF DEBUG}
ReportMemoryLeaksOnShutdown := True;
CMutexName := CMutexName + '_DEBUG';
{$ENDIF}
Application.Initialize;
Application.MainFormOnTaskbar := True;
{ Create mutex }
SetLastError(0);
hMutex := CreateMutex(nil, False, PChar(CMutexName));
if hMutex = 0 then
begin
RaiseLastOSError;
end;
try
if GetLastError = ERROR_ALREADY_EXISTS then
begin
{ Search main form }
Wnd := FindWindow(PChar('TForm1'), nil); // Class name of the main form
if Wnd = 0 then
begin
Exit;
end;
{ Bring foreground and activate }
SetForegroundWindow(Wnd);
{ Get window handle of TApplication }
AppWnd := GetWindowLong(Wnd, GWL_HWNDPARENT);
if AppWnd <> 0 then
begin
Wnd := AppWnd;
end;
{ Restore if iconized }
if IsIconic(Wnd) then
begin
SendMessage(Wnd, WM_SYSCOMMAND, SC_RESTORE, -1);
end;
Exit;
end;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2);
Application.Run;
finally
{ Close mutex }
CloseHandle(hMutex);
end;
end.