Fix GNU Hurd interpreter path
[tinycc.git] / win32 / examples / hello_win.c
blob96546e415f9d6097107249314cd8edb20efd67b9
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 const char *pWindowText;
15 void CenterWindow(HWND hWnd);
17 //+---------------------------------------------------------------------------
19 // Function: WndProc
21 // Synopsis: very unusual type of function - gets called by system to
22 // process windows messages.
24 // Arguments: same as always.
25 //----------------------------------------------------------------------------
27 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
29 switch (message) {
31 // ----------------------- first and last
32 case WM_CREATE:
33 CenterWindow(hwnd);
34 break;
36 case WM_DESTROY:
37 PostQuitMessage(0);
38 break;
40 // ----------------------- get out of it...
41 case WM_RBUTTONUP:
42 DestroyWindow(hwnd);
43 break;
45 case WM_KEYDOWN:
46 if (VK_ESCAPE == wParam)
47 DestroyWindow(hwnd);
48 break;
50 // ----------------------- display our minimal info
51 case WM_PAINT:
53 PAINTSTRUCT ps;
54 HDC hdc;
55 RECT rc;
56 hdc = BeginPaint(hwnd, &ps);
58 GetClientRect(hwnd, &rc);
59 SetTextColor(hdc, RGB(240,240,96));
60 SetBkMode(hdc, TRANSPARENT);
61 DrawText(hdc, pWindowText, -1, &rc, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
63 EndPaint(hwnd, &ps);
64 break;
67 // ----------------------- let windows do all other stuff
68 default:
69 return DefWindowProc(hwnd, message, wParam, lParam);
71 return 0;
74 //+---------------------------------------------------------------------------
76 // Function: WinMain
78 // Synopsis: standard entrypoint for GUI Win32 apps
80 //----------------------------------------------------------------------------
81 int APIENTRY WinMain(
82 HINSTANCE hInstance,
83 HINSTANCE hPrevInstance,
84 LPSTR lpCmdLine,
85 int nCmdShow
88 MSG msg;
89 WNDCLASS wc;
90 HWND hwnd;
92 pWindowText = lpCmdLine[0] ? lpCmdLine : "Hello Windows!";
94 // Fill in window class structure with parameters that describe
95 // the main window.
97 ZeroMemory(&wc, sizeof wc);
98 wc.hInstance = hInstance;
99 wc.lpszClassName = szAppName;
100 wc.lpfnWndProc = (WNDPROC)WndProc;
101 wc.style = CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW;
102 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
103 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
104 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
106 if (FALSE == RegisterClass(&wc))
107 return 0;
109 // create the browser
110 hwnd = CreateWindow(
111 szAppName,
112 szTitle,
113 WS_OVERLAPPEDWINDOW|WS_VISIBLE,
114 CW_USEDEFAULT,
115 CW_USEDEFAULT,
116 360,//CW_USEDEFAULT,
117 240,//CW_USEDEFAULT,
120 hInstance,
123 if (NULL == hwnd)
124 return 0;
126 // Main message loop:
127 while (GetMessage(&msg, NULL, 0, 0) > 0) {
128 TranslateMessage(&msg);
129 DispatchMessage(&msg);
132 return msg.wParam;
135 //+---------------------------------------------------------------------------
137 //+---------------------------------------------------------------------------
139 void CenterWindow(HWND hwnd_self)
141 HWND hwnd_parent;
142 RECT rw_self, rc_parent, rw_parent;
143 int xpos, ypos;
145 hwnd_parent = GetParent(hwnd_self);
146 if (NULL == hwnd_parent)
147 hwnd_parent = GetDesktopWindow();
149 GetWindowRect(hwnd_parent, &rw_parent);
150 GetClientRect(hwnd_parent, &rc_parent);
151 GetWindowRect(hwnd_self, &rw_self);
153 xpos = rw_parent.left + (rc_parent.right + rw_self.left - rw_self.right) / 2;
154 ypos = rw_parent.top + (rc_parent.bottom + rw_self.top - rw_self.bottom) / 2;
156 SetWindowPos(
157 hwnd_self, NULL,
158 xpos, ypos, 0, 0,
159 SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE
163 //+---------------------------------------------------------------------------