pp: return newline after directive
[tinycc.git] / win32 / examples / hello_win.c
blob294b7279a09a7a8fe8073a417b9eb5fdeb0719fc
1 //+---------------------------------------------------------------------------
2 //
3 // HELLO_WIN.C - Windows GUI 'Hello World!' Example
4 //
5 //+---------------------------------------------------------------------------
7 #include <windows.h>
9 #define APPNAME "HELLO_WIN"
11 char szAppName[] = APPNAME; // The name of this application
12 char szTitle[] = APPNAME; // The title bar text
13 char *pWindowText;
15 HINSTANCE g_hInst; // current instance
17 void CenterWindow(HWND hWnd);
19 //+---------------------------------------------------------------------------
21 // Function: WndProc
23 // Synopsis: very unusual type of function - gets called by system to
24 // process windows messages.
26 // Arguments: same as always.
27 //----------------------------------------------------------------------------
29 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
31 switch (message)
33 // ----------------------- first and last
34 case WM_CREATE:
35 CenterWindow(hwnd);
36 break;
38 case WM_DESTROY:
39 PostQuitMessage(0);
40 break;
43 // ----------------------- get out of it...
44 case WM_RBUTTONUP:
45 DestroyWindow(hwnd);
46 break;
48 case WM_KEYDOWN:
49 if (VK_ESCAPE == wParam)
50 DestroyWindow(hwnd);
51 break;
54 // ----------------------- display our minimal info
55 case WM_PAINT:
57 PAINTSTRUCT ps;
58 HDC hdc;
59 RECT rc;
60 hdc = BeginPaint(hwnd, &ps);
62 GetClientRect(hwnd, &rc);
63 SetTextColor(hdc, RGB(240,240,96));
64 SetBkMode(hdc, TRANSPARENT);
65 DrawText(hdc, pWindowText, -1, &rc, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
67 EndPaint(hwnd, &ps);
68 break;
71 // ----------------------- let windows do all other stuff
72 default:
73 return DefWindowProc(hwnd, message, wParam, lParam);
75 return 0;
78 //+---------------------------------------------------------------------------
80 // Function: WinMain
82 // Synopsis: standard entrypoint for GUI Win32 apps
84 //----------------------------------------------------------------------------
85 int APIENTRY WinMain(
86 HINSTANCE hInstance,
87 HINSTANCE hPrevInstance,
88 LPSTR lpCmdLine,
89 int nCmdShow)
91 MSG msg;
93 WNDCLASS wc;
95 HWND hwnd;
97 // Fill in window class structure with parameters that describe
98 // the main window.
100 ZeroMemory(&wc, sizeof wc);
101 wc.hInstance = hInstance;
102 wc.lpszClassName = szAppName;
103 wc.lpfnWndProc = (WNDPROC)WndProc;
104 wc.style = CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW;
105 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
106 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
107 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
109 if (FALSE == RegisterClass(&wc)) return 0;
111 // create the browser
112 hwnd = CreateWindow(
113 szAppName,
114 szTitle,
115 WS_OVERLAPPEDWINDOW|WS_VISIBLE,
116 CW_USEDEFAULT,
117 CW_USEDEFAULT,
118 360,//CW_USEDEFAULT,
119 240,//CW_USEDEFAULT,
122 g_hInst,
125 if (NULL == hwnd) return 0;
127 pWindowText = lpCmdLine[0] ? lpCmdLine : "Hello Windows!";
129 // Main message loop:
130 while (GetMessage(&msg, NULL, 0, 0) > 0)
132 TranslateMessage(&msg);
133 DispatchMessage(&msg);
136 return msg.wParam;
139 //+---------------------------------------------------------------------------
141 //+---------------------------------------------------------------------------
143 void CenterWindow(HWND hwnd_self)
145 RECT rw_self, rc_parent, rw_parent; HWND hwnd_parent;
146 hwnd_parent = GetParent(hwnd_self);
147 if (NULL==hwnd_parent) hwnd_parent = GetDesktopWindow();
148 GetWindowRect(hwnd_parent, &rw_parent);
149 GetClientRect(hwnd_parent, &rc_parent);
150 GetWindowRect(hwnd_self, &rw_self);
151 SetWindowPos(hwnd_self, NULL,
152 rw_parent.left + (rc_parent.right + rw_self.left - rw_self.right) / 2,
153 rw_parent.top + (rc_parent.bottom + rw_self.top - rw_self.bottom) / 2,
154 0, 0,
155 SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE
159 //+---------------------------------------------------------------------------