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