root/release-kits/shared/search4j/head_src/guihead/guihead.c @ 15093

Revision 15093, 4.7 KB (checked in by oranfry, 12 years ago)

importing the search4j tool I wrote for the release kits

Line 
1/*
2    Launch4j (http://launch4j.sourceforge.net/)
3    Cross-platform Java application wrapper for creating Windows native executables.
4
5    Copyright (C) 2004, 2006 Grzegorz Kowal
6
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Lesser General Public
9    License as published by the Free Software Foundation; either
10    version 2.1 of the License, or (at your option) any later version.
11
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with this library; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21
22    Compiled with Mingw port of GCC,
23    Bloodshed Dev-C++ IDE (http://www.bloodshed.net/devcpp.html)
24*/
25
26#include "../resource.h"
27#include "../head.h"
28#include "guihead.h"
29
30extern PROCESS_INFORMATION pi;
31
32HWND hWnd;
33DWORD dwExitCode = 0;
34BOOL stayAlive = FALSE;
35BOOL splash = FALSE;
36BOOL splashTimeoutErr;
37BOOL waitForWindow;
38int splashTimeout;      // tick count (s)
39
40int APIENTRY WinMain(HINSTANCE hInstance,
41                     HINSTANCE hPrevInstance,
42                     LPSTR     lpCmdLine,
43                     int       nCmdShow) {
44   
45    HMODULE hLibrary = NULL;
46    if (!prepare(hLibrary, lpCmdLine, "run")) {
47        if (hLibrary != NULL) {
48            FreeLibrary(hLibrary);
49        }
50        return 1;
51    }
52
53    splash = loadBoolString(hLibrary, SHOW_SPLASH);
54    stayAlive = loadBoolString(hLibrary, GUI_HEADER_STAYS_ALIVE);
55    if (splash || stayAlive) {
56        hWnd = CreateWindowEx(WS_EX_TOOLWINDOW, "STATIC", "",
57                WS_POPUP | SS_BITMAP,
58                0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
59        if (splash) {
60            char timeout[10] = {0};
61            if (!loadString(hLibrary, SPLASH_TIMEOUT, timeout)) {
62                msgBox("Cannot load splash timeout property.");
63                return 1;
64            }
65            splashTimeout = atoi(timeout);
66            if (splashTimeout <= 0 || splashTimeout > 60 * 15 /* 15 minutes */) {
67                msgBox("Invalid splash timeout property.");
68                return 1;
69            }
70            splashTimeoutErr = loadBoolString(hLibrary, SPLASH_TIMEOUT_ERR);
71            waitForWindow = loadBoolString(hLibrary, SPLASH_WAITS_FOR_WINDOW);
72            HANDLE hImage = LoadImage(hInstance,    // handle of the instance containing the image
73                    MAKEINTRESOURCE(SPLASH_BITMAP), // name or identifier of image
74                    IMAGE_BITMAP,                   // type of image
75                    0,                              // desired width
76                    0,                              // desired height
77                    LR_DEFAULTSIZE);
78            if (hImage == NULL) {
79                msgBox("Cannot load splash screen.");
80                return 1;
81            }
82            SendMessage(hWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hImage);
83            RECT rect;
84            GetWindowRect(hWnd, &rect);
85            int x = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2;
86            int y = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2;
87            SetWindowPos(hWnd, HWND_TOP, x, y, 0, 0, SWP_NOSIZE);
88            ShowWindow(hWnd, nCmdShow);
89            UpdateWindow (hWnd);
90        }
91        if (!SetTimer (hWnd, ID_TIMER, 1000 /* 1s */, TimerProc)) {
92            return 1;
93        }
94    }
95    FreeLibrary(hLibrary);
96    if (execute(FALSE) == -1) {
97        char errMsg[BIG_STR];
98        strcpy(errMsg, "Error occured while starting the application...\n");
99        strcat(errMsg, strerror(errno));
100        msgBox(errMsg);
101        return 1;
102    }
103    if (!(splash || stayAlive)) {
104        closeHandles();
105        return 0;
106    }
107    MSG msg;
108    while (GetMessage(&msg, NULL, 0, 0)) {
109        TranslateMessage(&msg);
110        DispatchMessage(&msg);
111    }
112    closeHandles();
113    return dwExitCode;
114}
115
116BOOL CALLBACK enumwndfn(HWND hwnd, LPARAM lParam) {
117    DWORD processId;
118    GetWindowThreadProcessId(hwnd, &processId);
119    if (pi.dwProcessId == processId) {
120        LONG styles = GetWindowLong(hwnd, GWL_STYLE);
121        if ((styles & WS_VISIBLE) != 0) {
122            splash = FALSE;
123            ShowWindow(hWnd, SW_HIDE);
124            return FALSE;
125        }
126    }
127    return TRUE;
128}
129
130VOID CALLBACK TimerProc(
131    HWND hwnd,          // handle of window for timer messages
132    UINT uMsg,          // WM_TIMER message
133    UINT idEvent,       // timer identifier
134    DWORD dwTime) {     // current system time
135   
136    if (splash) {
137        if (splashTimeout == 0) {
138            splash = FALSE;
139            ShowWindow(hWnd, SW_HIDE);
140            if (waitForWindow && splashTimeoutErr) {
141                KillTimer(hwnd, ID_TIMER);
142                msgBox("Could not start the application, operation timed out.");
143                PostQuitMessage(0);
144            }
145        } else {
146            splashTimeout--;
147            if (waitForWindow) {
148                EnumWindows(enumwndfn, 0);
149            }
150        }
151    }
152    GetExitCodeProcess(pi.hProcess, &dwExitCode);
153    if (dwExitCode != STILL_ACTIVE
154            || !(splash || stayAlive)) {
155        KillTimer(hWnd, ID_TIMER);
156        PostQuitMessage(0);
157        return;
158    }
159}
Note: See TracBrowser for help on using the browser.