History

win32: reformat examples, crt etc

grischka committed 17 years ago

Commit 97738d11 parent bb5e0df4 files · +167 −153Browse files

Unified · Split

4 files
  1. win32/examples/dll.c +1 −4
  2. win32/examples/hello_win.c +114 −110
  3. win32/lib/crt1.c +8 −8
  4. win32/lib/wincrt1.c +44 −31
win32/examples/dll.c+1−4
@@ -4,12 +4,9 @@ · 3 unmodified lines
4//4//
5 5
6#include <windows.h>6#include <windows.h>
7
8#define DLL_EXPORT __declspec(dllexport)7#define DLL_EXPORT __declspec(dllexport)
9 8
10
11DLL_EXPORT void HelloWorld (void)9DLL_EXPORT void HelloWorld (void)
12{10{
13 MessageBox (0, "Hello World!", "From DLL", MB_ICONINFORMATION);11 MessageBox (0, "Hello World!", "From DLL", MB_ICONINFORMATION);
14}12}
15
win32/examples/hello_win.c+114−110
@@ -10,9 +10,7 @@ · 9 unmodified lines
10 10
11char szAppName[] = APPNAME; // The name of this application11char szAppName[] = APPNAME; // The name of this application
12char szTitle[] = APPNAME; // The title bar text12char szTitle[] = APPNAME; // The title bar text
13char *pWindowText;13const char *pWindowText;
14
15HINSTANCE g_hInst; // current instance
16 14
17void CenterWindow(HWND hWnd);15void CenterWindow(HWND hWnd);
18 16
@@ -28,51 +26,49 @@ void CenterWindow(HWND hWnd); · 9 unmodified lines
28 26
29LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)27LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
30{28{
31 switch (message)29 switch (message) {
32 {30
33 // ----------------------- first and last31 // ----------------------- first and last
34 case WM_CREATE:32 case WM_CREATE:
35 CenterWindow(hwnd);33 CenterWindow(hwnd);
36 break;34 break;
37 35
38 case WM_DESTROY:36 case WM_DESTROY:
39 PostQuitMessage(0);37 PostQuitMessage(0);
40 break;38 break;
41 39
42 40 // ----------------------- get out of it...
43 // ----------------------- get out of it...41 case WM_RBUTTONUP:
44 case WM_RBUTTONUP:42 DestroyWindow(hwnd);
45 DestroyWindow(hwnd);43 break;
46 break;44
47 45 case WM_KEYDOWN:
48 case WM_KEYDOWN:46 if (VK_ESCAPE == wParam)
49 if (VK_ESCAPE == wParam)47 DestroyWindow(hwnd);
50 DestroyWindow(hwnd);48 break;
51 break;49
52 50 // ----------------------- display our minimal info
53 51 case WM_PAINT:
54 // ----------------------- display our minimal info52 {
55 case WM_PAINT:53 PAINTSTRUCT ps;
56 {54 HDC hdc;
57 PAINTSTRUCT ps;55 RECT rc;
58 HDC hdc;56 hdc = BeginPaint(hwnd, &ps);
59 RECT rc;57
60 hdc = BeginPaint(hwnd, &ps);58 GetClientRect(hwnd, &rc);
61 59 SetTextColor(hdc, RGB(240,240,96));
62 GetClientRect(hwnd, &rc);60 SetBkMode(hdc, TRANSPARENT);
63 SetTextColor(hdc, RGB(240,240,96));61 DrawText(hdc, pWindowText, -1, &rc, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
64 SetBkMode(hdc, TRANSPARENT);62
65 DrawText(hdc, pWindowText, -1, &rc, DT_CENTER|DT_SINGLELINE|DT_VCENTER);63 EndPaint(hwnd, &ps);
66 64 break;
67 EndPaint(hwnd, &ps);65 }
68 break;66
69 }67 // ----------------------- let windows do all other stuff
70 68 default:
71 // ----------------------- let windows do all other stuff69 return DefWindowProc(hwnd, message, wParam, lParam);
72 default:70 }
73 return DefWindowProc(hwnd, message, wParam, lParam);71 return 0;
74 }
75 return 0;
76}72}
77 73
78//+---------------------------------------------------------------------------74//+---------------------------------------------------------------------------
@@ -83,57 +79,57 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) · 4 unmodified lines
83//79//
84//----------------------------------------------------------------------------80//----------------------------------------------------------------------------
85int APIENTRY WinMain(81int APIENTRY WinMain(
86 HINSTANCE hInstance,82 HINSTANCE hInstance,
87 HINSTANCE hPrevInstance,83 HINSTANCE hPrevInstance,
88 LPSTR lpCmdLine,84 LPSTR lpCmdLine,
89 int nCmdShow)85 int nCmdShow
86 )
90{87{
91 MSG msg;88 MSG msg;
92 89 WNDCLASS wc;
93 WNDCLASS wc;90 HWND hwnd;
94 91
95 HWND hwnd;92 pWindowText = lpCmdLine[0] ? lpCmdLine : "Hello Windows!";
96 93
97 // Fill in window class structure with parameters that describe94 // Fill in window class structure with parameters that describe
98 // the main window.95 // the main window.
99 96
100 ZeroMemory(&wc, sizeof wc);97 ZeroMemory(&wc, sizeof wc);
101 wc.hInstance = hInstance;98 wc.hInstance = hInstance;
102 wc.lpszClassName = szAppName;99 wc.lpszClassName = szAppName;
103 wc.lpfnWndProc = (WNDPROC)WndProc;100 wc.lpfnWndProc = (WNDPROC)WndProc;
104 wc.style = CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW;101 wc.style = CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW;
105 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);102 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
106 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);103 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
107 wc.hCursor = LoadCursor(NULL, IDC_ARROW);104 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
108 105
109 if (FALSE == RegisterClass(&wc)) return 0;106 if (FALSE == RegisterClass(&wc))
110 107 return 0;
111 // create the browser108
112 hwnd = CreateWindow(109 // create the browser
113 szAppName,110 hwnd = CreateWindow(
114 szTitle,111 szAppName,
115 WS_OVERLAPPEDWINDOW|WS_VISIBLE,112 szTitle,
116 CW_USEDEFAULT,113 WS_OVERLAPPEDWINDOW|WS_VISIBLE,
117 CW_USEDEFAULT,114 CW_USEDEFAULT,
118 360,//CW_USEDEFAULT,115 CW_USEDEFAULT,
119 240,//CW_USEDEFAULT,116 360,//CW_USEDEFAULT,
120 0,117 240,//CW_USEDEFAULT,
121 0,118 0,
122 g_hInst,119 0,
123 0);120 hInstance,
124 121 0);
125 if (NULL == hwnd) return 0;122
126 123 if (NULL == hwnd)
127 pWindowText = lpCmdLine[0] ? lpCmdLine : "Hello Windows!";124 return 0;
128 125
129 // Main message loop:126 // Main message loop:
130 while (GetMessage(&msg, NULL, 0, 0) > 0)127 while (GetMessage(&msg, NULL, 0, 0) > 0) {
131 {128 TranslateMessage(&msg);
132 TranslateMessage(&msg);129 DispatchMessage(&msg);
133 DispatchMessage(&msg);130 }
134 }131
135 132 return msg.wParam;
136 return msg.wParam;
137}133}
138 134
139//+---------------------------------------------------------------------------135//+---------------------------------------------------------------------------
@@ -142,18 +138,26 @@ int APIENTRY WinMain( · 2 unmodified lines
142 138
143void CenterWindow(HWND hwnd_self)139void CenterWindow(HWND hwnd_self)
144{140{
145 RECT rw_self, rc_parent, rw_parent; HWND hwnd_parent;141 HWND hwnd_parent;
146 hwnd_parent = GetParent(hwnd_self);142 RECT rw_self, rc_parent, rw_parent;
147 if (NULL==hwnd_parent) hwnd_parent = GetDesktopWindow();143 int xpos, ypos;
148 GetWindowRect(hwnd_parent, &rw_parent);144
149 GetClientRect(hwnd_parent, &rc_parent);145 hwnd_parent = GetParent(hwnd_self);
150 GetWindowRect(hwnd_self, &rw_self);146 if (NULL == hwnd_parent)
151 SetWindowPos(hwnd_self, NULL,147 hwnd_parent = GetDesktopWindow();
152 rw_parent.left + (rc_parent.right + rw_self.left - rw_self.right) / 2,148
153 rw_parent.top + (rc_parent.bottom + rw_self.top - rw_self.bottom) / 2,149 GetWindowRect(hwnd_parent, &rw_parent);
154 0, 0,150 GetClientRect(hwnd_parent, &rc_parent);
155 SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE151 GetWindowRect(hwnd_self, &rw_self);
156 );152
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;
155
156 SetWindowPos(
157 hwnd_self, NULL,
158 xpos, ypos, 0, 0,
159 SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE
160 );
157}161}
158 162
159//+---------------------------------------------------------------------------163//+---------------------------------------------------------------------------
win32/lib/crt1.c+8−8
@@ -11,7 +11,7 @@ void _controlfp(unsigned a, unsigned b); · 10 unmodified lines
11 11
12typedef struct12typedef struct
13{13{
14 int newmode;14 int newmode;
15} _startupinfo;15} _startupinfo;
16 16
17void __getmainargs(int *pargc, char ***pargv, char ***penv, int globb, _startupinfo*);17void __getmainargs(int *pargc, char ***pargv, char ***penv, int globb, _startupinfo*);
@@ -20,15 +20,15 @@ int main(int argc, char **argv, char **env); · 2 unmodified lines
20 20
21int _start(void)21int _start(void)
22{22{
23 int argc; char **argv; char **env; int ret;23 int argc; char **argv; char **env; int ret;
24 _startupinfo start_info = {0};24 _startupinfo start_info = {0};
25 25
26 _controlfp(0x10000, 0x30000);26 _controlfp(0x10000, 0x30000);
27 __set_app_type(__CONSOLE_APP);27 __set_app_type(__CONSOLE_APP);
28 __getmainargs(&argc, &argv, &env, 0, &start_info);28 __getmainargs(&argc, &argv, &env, 0, &start_info);
29 29
30 ret = main(argc, argv, env);30 ret = main(argc, argv, env);
31 exit(ret);31 exit(ret);
32}32}
33 33
34// =============================================34// =============================================
win32/lib/wincrt1.c+44−31
@@ -10,40 +10,53 @@ void _controlfp(unsigned a, unsigned b); · 9 unmodified lines
10 10
11int _winstart(void)11int _winstart(void)
12{12{
13 char *szCmd; STARTUPINFO startinfo;13 char *szCmd;
14 14 STARTUPINFO startinfo;
15 __set_app_type(__GUI_APP);15 int fShow;
16 _controlfp(0x10000, 0x30000);16 int ret;
17 17
18 szCmd = GetCommandLine();18 __set_app_type(__GUI_APP);
19 if (szCmd)19 _controlfp(0x10000, 0x30000);
20 {20
21 while (' ' == *szCmd) szCmd++;21 szCmd = GetCommandLine();
22 if ('\"' == *szCmd)22 if (szCmd) {
23 {23 while (' ' == *szCmd)
24 while (*++szCmd)24 szCmd++;
25 if ('\"' == *szCmd) { szCmd++; break; }25 if ('\"' == *szCmd) {
26 }26 while (*++szCmd)
27 else27 if ('\"' == *szCmd) {
28 {28 szCmd++;
29 while (*szCmd && ' ' != *szCmd) szCmd++;29 break;
30 }30 }
31 while (' ' == *szCmd) szCmd++;31 } else {
32 }32 while (*szCmd && ' ' != *szCmd)
33 33 szCmd++;
34 GetStartupInfo(&startinfo);34 }
35 exit(WinMain(GetModuleHandle(NULL), NULL, szCmd,35 while (' ' == *szCmd)
36 (startinfo.dwFlags & STARTF_USESHOWWINDOW) ?36 szCmd++;
37 startinfo.wShowWindow : SW_SHOWDEFAULT));37 }
38
39 GetStartupInfo(&startinfo);
40 fShow = startinfo.wShowWindow;
41 if (0 == (startinfo.dwFlags & STARTF_USESHOWWINDOW))
42 fShow = SW_SHOWDEFAULT;
43
44 ret = WinMain(GetModuleHandle(NULL), NULL, szCmd, fShow);
45 exit(ret);
38}46}
39 47
40int _runwinmain(int argc, char **argv)48int _runwinmain(int argc, char **argv)
41{49{
42 char *szCmd = NULL;50 char *szCmd, *p;
43 char *p = GetCommandLine();51
44 if (argc > 1) szCmd = strstr(p, argv[1]);52 p = GetCommandLine();
45 if (NULL == szCmd) szCmd = "";53 szCmd = NULL;
46 else if (szCmd > p && szCmd[-1] == '\"') --szCmd;54 if (argc > 1)
47 return WinMain(GetModuleHandle(NULL), NULL, szCmd, SW_SHOWDEFAULT);55 szCmd = strstr(p, argv[1]);
56 if (NULL == szCmd)
57 szCmd = "";
58 else if (szCmd > p && szCmd[-1] == '\"')
59 --szCmd;
60 return WinMain(GetModuleHandle(NULL), NULL, szCmd, SW_SHOWDEFAULT);
48}61}
49 62

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t