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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| #include <windows.h>
/* Quelques messages que NOUS définissons */
#define WM_SPLASH_READY (WM_USER + 1)
#define WM_SPLASH_SET_STATUS (WM_USER + 2)
#define WM_SPLASH_EXIT (WM_USER + 3)
LRESULT CALLBACK FenPrincipaleProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
DWORD WINAPI SplashThreadProc(LPVOID lpParams);
LRESULT CALLBACK SplashScreenProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
struct {
HINSTANCE hInstance;
DWORD dwMainThreadId;
int ScreenWidth, ScreenHeight;
} MyApp;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc;
HWND hWnd;
MSG msg;
HANDLE hSplashThread;
DWORD dwSplashThreadId;
BOOL SplashScreenReady = FALSE;
MyApp.hInstance = hInstance;
MyApp.dwMainThreadId = GetCurrentThreadId();
MyApp.ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
MyApp.ScreenHeight = GetSystemMetrics(SM_CYSCREEN);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = FenPrincipaleProc;
wc.lpszClassName = "Fenêtre principale";
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
/* Créer un thread pour faire apparaître le beau splash screen qui patientera l'utilisateur */
/* pendant que nous allons initialiser le programme. */
hSplashThread = CreateThread(NULL, 0, SplashThreadProc, NULL, 0, &dwSplashThreadId);
/* Attendre que le splash screen ait bien été créé. */
while (!SplashScreenReady)
{
GetMessage(&msg, NULL, 0, 0);
SplashScreenReady = (msg.message == WM_SPLASH_READY);
}
/* Début des initialisations. */
PostThreadMessage(dwSplashThreadId, WM_SPLASH_SET_STATUS, 0, (LPARAM)"Initialisation 1");
Sleep(1000); /* Exemple de traitement qui requiert 1 seconde (1000 ms). */
PostThreadMessage(dwSplashThreadId, WM_SPLASH_SET_STATUS, 0, (LPARAM)"Initialisation 2");
Sleep(2000); /* Exemple de traitement qui requiert 2 secondes. */
PostThreadMessage(dwSplashThreadId, WM_SPLASH_SET_STATUS, 0, (LPARAM)"Initialisation 3");
Sleep(500); /* Exemple de traitement qui requiert 0.5 seconde. */
/* Fin des initialisations. On peut désormais créer la fenêtre principale de l'application. */
/* Fermons alors le splash screen. */
PostThreadMessage(dwSplashThreadId, WM_SPLASH_EXIT, 0, 0);
/* Attendre que le thread du splash screen s'est terminé avant de continuer. */
WaitForSingleObject(hSplashThread, INFINITE);
hWnd = CreateWindow("Fenêtre principale",
"Notre fenêtre principale",
WS_OVERLAPPEDWINDOW,
MyApp.ScreenWidth / 5,
MyApp.ScreenHeight / 5,
3 * MyApp.ScreenWidth / 5,
3 * MyApp.ScreenHeight / 5,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, SW_MINIMIZE);
ShowWindow(hWnd, nCmdShow);
SetForegroundWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK FenPrincipaleProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0L;
}
DWORD WINAPI SplashThreadProc(LPVOID lpParams)
{
/* Pratiquement un autre WinMain ... */
WNDCLASS wc;
HWND hWnd;
MSG msg;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = MyApp.hInstance;
wc.lpfnWndProc = SplashScreenProc;
wc.lpszClassName = "Splash Screen";
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
hWnd = CreateWindow("Splash Screen",
"Notre splash screen",
WS_POPUP,
MyApp.ScreenWidth / 4,
MyApp.ScreenHeight / 4,
MyApp.ScreenWidth / 2,
MyApp.ScreenHeight / 2,
NULL,
NULL,
MyApp.hInstance,
NULL);
ShowWindow(hWnd, SW_SHOW);
PostThreadMessage(MyApp.dwMainThreadId, WM_SPLASH_READY, 0, 0);
while (GetMessage(&msg, NULL, 0, 0))
{
switch(msg.message)
{
case WM_SPLASH_SET_STATUS:
SetWindowText(hWnd, (const char *)msg.lParam);
InvalidateRect(hWnd, NULL, TRUE);
break;
case WM_SPLASH_EXIT:
DestroyWindow(hWnd);
break;
default:
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
LRESULT CALLBACK SplashScreenProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static char lpText[256];
static int TextLength;
static HDC hDC;
PAINTSTRUCT ps;
static RECT r;
switch(message)
{
case WM_CREATE:
hDC = GetDC(hwnd);
SetBkMode(hDC, TRANSPARENT);
SetBkColor(hDC, RGB(0, 0, 0));
GetClientRect(hwnd, &r);
break;
case WM_SETTEXT:
strcpy(lpText, (const char *)lParam);
TextLength = strlen(lpText);
break;
case WM_PAINT:
BeginPaint(hwnd, &ps);
DrawTextA(hDC, lpText, TextLength, &r, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0L;
} |
Partager