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