]> git.lyx.org Git - features.git/blob - development/Win32/packaging/AltInstaller/specials/Launcher/lyxLauncher.dpr
f91d1b91cd525c27275f391b92fc3a847b7f01fe
[features.git] / development / Win32 / packaging / AltInstaller / specials / Launcher / lyxLauncher.dpr
1 program lyxLauncher;
2 // this program opens LyX and hides its console window
3 // author: Uwe Stöhr
4
5 {The problematic is the following:
6  When the lyx.exe is started, also a console window is shown.
7  But closing the console window, also closes LyX,
8  therefore the console will be hidden by this program.}
9
10 {$APPTYPE CONSOLE}
11
12 uses
13   Windows, SysUtils, ShellApi, Dialogs;
14
15 var Path,FileName : string;
16     hConsole : THandle;
17
18
19 procedure StartLyX(hConsole: THandle; FileName,Path: string);
20 // starts LyX
21
22 var Params : PChar;
23     hLyX : THandle;
24 begin
25
26  // if a filename is given, convert it to a PChar; needed for the ShellExecute
27  if FileName <> '' then
28   Params:= PChar(FileName)
29  else
30   Params:= nil;
31
32  // start LyX
33  hLyX:= ShellExecute(hConsole,PChar('open'),
34                      PChar(Path),Params,nil,SW_SHOWNORMAL);
35  if hLyX = ERROR_FILE_NOT_FOUND  then
36  begin
37   MessageDLG('The file'#13#10 + Path + #13#10
38              + 'could not be found!',mtError,[mbOK],0);
39   exit;
40  end;
41  if hLyX = SE_ERR_ACCESSDENIED  then
42  begin
43   MessageDLG('Windows denied access on the file'#13#10 + Path,
44              mtError,[mbOK],0);
45   exit;
46  end;
47
48 end; // end procedure
49
50
51 procedure HideWindow(ProgWin: string);
52 // hides a given program window
53
54 var Handle : THandle;
55 begin
56
57  // find handle of the program window
58  // Repeat until the handle is available
59  // because Lyx needs some time to start
60  Repeat
61   Sleep(1000); // wait 1 second to give LyX time to open
62   Handle := FindWindow(nil,Pchar(ProgWin));
63  Until Handle <> 0;
64
65  // hide the window from taskbar
66  ShowWindow(Handle, SW_HIDE);
67
68 end; //end procedure
69
70
71 begin //begin program
72
73  //Read path to this application
74  Path:= ParamStr(0);
75
76  //get handle of this console window
77  // This application is called by the lyx.bat with the name "LyX"
78  hConsole := FindWindow(nil,Pchar('LyX'));
79  // hide the window of this console application
80  ShowWindow(hConsole,SW_HIDE);
81
82  // do the same for the real name of this console application
83  // because it depends on the computer speed if the "LyX" console window
84  // was closed before it could be processed
85  hConsole := FindWindow(nil,Pchar(Path));
86  ShowWindow(hConsole,SW_HIDE);
87
88  // replace in the path "lyxLauncher.exe" by "lyx.exe"
89  Path:= StringReplace(Path, 'lyxLauncher', 'lyx', [rfIgnoreCase]);
90
91  // read given filename of a LyX-document
92  FileName:= ParamStr(1);
93
94  // start LyX
95  StartLyX(hConsole,FileName,Path);
96
97  // hide console window of lyx.exe
98  HideWindow(Path);
99
100 end. //end program
101