win32: reformat examples, crt etc

This commit is contained in:
grischka 2009-07-18 22:07:10 +02:00
parent bb5e0df79a
commit 97738d1ae9
4 changed files with 148 additions and 134 deletions

View file

@ -4,12 +4,9 @@
// //
#include <windows.h> #include <windows.h>
#define DLL_EXPORT __declspec(dllexport) #define DLL_EXPORT __declspec(dllexport)
DLL_EXPORT void HelloWorld (void) DLL_EXPORT void HelloWorld (void)
{ {
MessageBox (0, "Hello World!", "From DLL", MB_ICONINFORMATION); MessageBox (0, "Hello World!", "From DLL", MB_ICONINFORMATION);
} }

View file

@ -10,9 +10,7 @@
char szAppName[] = APPNAME; // The name of this application char szAppName[] = APPNAME; // The name of this application
char szTitle[] = APPNAME; // The title bar text char szTitle[] = APPNAME; // The title bar text
char *pWindowText; const char *pWindowText;
HINSTANCE g_hInst; // current instance
void CenterWindow(HWND hWnd); void CenterWindow(HWND hWnd);
@ -28,51 +26,49 @@ void CenterWindow(HWND hWnd);
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{ {
switch (message) switch (message) {
{
// ----------------------- first and last
case WM_CREATE:
CenterWindow(hwnd);
break;
case WM_DESTROY: // ----------------------- first and last
PostQuitMessage(0); case WM_CREATE:
break; CenterWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
// ----------------------- get out of it... // ----------------------- get out of it...
case WM_RBUTTONUP: case WM_RBUTTONUP:
DestroyWindow(hwnd); DestroyWindow(hwnd);
break; break;
case WM_KEYDOWN: case WM_KEYDOWN:
if (VK_ESCAPE == wParam) if (VK_ESCAPE == wParam)
DestroyWindow(hwnd); DestroyWindow(hwnd);
break; break;
// ----------------------- display our minimal info
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc;
RECT rc;
hdc = BeginPaint(hwnd, &ps);
// ----------------------- display our minimal info GetClientRect(hwnd, &rc);
case WM_PAINT: SetTextColor(hdc, RGB(240,240,96));
{ SetBkMode(hdc, TRANSPARENT);
PAINTSTRUCT ps; DrawText(hdc, pWindowText, -1, &rc, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
HDC hdc;
RECT rc;
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc); EndPaint(hwnd, &ps);
SetTextColor(hdc, RGB(240,240,96)); break;
SetBkMode(hdc, TRANSPARENT); }
DrawText(hdc, pWindowText, -1, &rc, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
EndPaint(hwnd, &ps); // ----------------------- let windows do all other stuff
break; default:
} return DefWindowProc(hwnd, message, wParam, lParam);
}
// ----------------------- let windows do all other stuff return 0;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
} }
//+--------------------------------------------------------------------------- //+---------------------------------------------------------------------------
@ -83,57 +79,57 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
// //
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
int APIENTRY WinMain( int APIENTRY WinMain(
HINSTANCE hInstance, HINSTANCE hInstance,
HINSTANCE hPrevInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, LPSTR lpCmdLine,
int nCmdShow) int nCmdShow
)
{ {
MSG msg; MSG msg;
WNDCLASS wc;
HWND hwnd;
WNDCLASS wc; pWindowText = lpCmdLine[0] ? lpCmdLine : "Hello Windows!";
HWND hwnd; // Fill in window class structure with parameters that describe
// the main window.
// Fill in window class structure with parameters that describe ZeroMemory(&wc, sizeof wc);
// the main window. wc.hInstance = hInstance;
wc.lpszClassName = szAppName;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.style = CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
ZeroMemory(&wc, sizeof wc); if (FALSE == RegisterClass(&wc))
wc.hInstance = hInstance; return 0;
wc.lpszClassName = szAppName;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.style = CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
if (FALSE == RegisterClass(&wc)) return 0; // create the browser
hwnd = CreateWindow(
szAppName,
szTitle,
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
360,//CW_USEDEFAULT,
240,//CW_USEDEFAULT,
0,
0,
hInstance,
0);
// create the browser if (NULL == hwnd)
hwnd = CreateWindow( return 0;
szAppName,
szTitle,
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
360,//CW_USEDEFAULT,
240,//CW_USEDEFAULT,
0,
0,
g_hInst,
0);
if (NULL == hwnd) return 0; // Main message loop:
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
pWindowText = lpCmdLine[0] ? lpCmdLine : "Hello Windows!"; return msg.wParam;
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
} }
//+--------------------------------------------------------------------------- //+---------------------------------------------------------------------------
@ -142,18 +138,26 @@ int APIENTRY WinMain(
void CenterWindow(HWND hwnd_self) void CenterWindow(HWND hwnd_self)
{ {
RECT rw_self, rc_parent, rw_parent; HWND hwnd_parent; HWND hwnd_parent;
hwnd_parent = GetParent(hwnd_self); RECT rw_self, rc_parent, rw_parent;
if (NULL==hwnd_parent) hwnd_parent = GetDesktopWindow(); int xpos, ypos;
GetWindowRect(hwnd_parent, &rw_parent);
GetClientRect(hwnd_parent, &rc_parent); hwnd_parent = GetParent(hwnd_self);
GetWindowRect(hwnd_self, &rw_self); if (NULL == hwnd_parent)
SetWindowPos(hwnd_self, NULL, hwnd_parent = GetDesktopWindow();
rw_parent.left + (rc_parent.right + rw_self.left - rw_self.right) / 2,
rw_parent.top + (rc_parent.bottom + rw_self.top - rw_self.bottom) / 2, GetWindowRect(hwnd_parent, &rw_parent);
0, 0, GetClientRect(hwnd_parent, &rc_parent);
SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE GetWindowRect(hwnd_self, &rw_self);
);
xpos = rw_parent.left + (rc_parent.right + rw_self.left - rw_self.right) / 2;
ypos = rw_parent.top + (rc_parent.bottom + rw_self.top - rw_self.bottom) / 2;
SetWindowPos(
hwnd_self, NULL,
xpos, ypos, 0, 0,
SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE
);
} }
//+--------------------------------------------------------------------------- //+---------------------------------------------------------------------------

View file

@ -11,7 +11,7 @@ void _controlfp(unsigned a, unsigned b);
typedef struct typedef struct
{ {
int newmode; int newmode;
} _startupinfo; } _startupinfo;
void __getmainargs(int *pargc, char ***pargv, char ***penv, int globb, _startupinfo*); void __getmainargs(int *pargc, char ***pargv, char ***penv, int globb, _startupinfo*);
@ -20,15 +20,15 @@ int main(int argc, char **argv, char **env);
int _start(void) int _start(void)
{ {
int argc; char **argv; char **env; int ret; int argc; char **argv; char **env; int ret;
_startupinfo start_info = {0}; _startupinfo start_info = {0};
_controlfp(0x10000, 0x30000); _controlfp(0x10000, 0x30000);
__set_app_type(__CONSOLE_APP); __set_app_type(__CONSOLE_APP);
__getmainargs(&argc, &argv, &env, 0, &start_info); __getmainargs(&argc, &argv, &env, 0, &start_info);
ret = main(argc, argv, env); ret = main(argc, argv, env);
exit(ret); exit(ret);
} }
// ============================================= // =============================================

View file

@ -10,40 +10,53 @@ void _controlfp(unsigned a, unsigned b);
int _winstart(void) int _winstart(void)
{ {
char *szCmd; STARTUPINFO startinfo; char *szCmd;
STARTUPINFO startinfo;
int fShow;
int ret;
__set_app_type(__GUI_APP); __set_app_type(__GUI_APP);
_controlfp(0x10000, 0x30000); _controlfp(0x10000, 0x30000);
szCmd = GetCommandLine(); szCmd = GetCommandLine();
if (szCmd) if (szCmd) {
{ while (' ' == *szCmd)
while (' ' == *szCmd) szCmd++; szCmd++;
if ('\"' == *szCmd) if ('\"' == *szCmd) {
{ while (*++szCmd)
while (*++szCmd) if ('\"' == *szCmd) {
if ('\"' == *szCmd) { szCmd++; break; } szCmd++;
} break;
else }
{ } else {
while (*szCmd && ' ' != *szCmd) szCmd++; while (*szCmd && ' ' != *szCmd)
} szCmd++;
while (' ' == *szCmd) szCmd++; }
} while (' ' == *szCmd)
szCmd++;
}
GetStartupInfo(&startinfo); GetStartupInfo(&startinfo);
exit(WinMain(GetModuleHandle(NULL), NULL, szCmd, fShow = startinfo.wShowWindow;
(startinfo.dwFlags & STARTF_USESHOWWINDOW) ? if (0 == (startinfo.dwFlags & STARTF_USESHOWWINDOW))
startinfo.wShowWindow : SW_SHOWDEFAULT)); fShow = SW_SHOWDEFAULT;
ret = WinMain(GetModuleHandle(NULL), NULL, szCmd, fShow);
exit(ret);
} }
int _runwinmain(int argc, char **argv) int _runwinmain(int argc, char **argv)
{ {
char *szCmd = NULL; char *szCmd, *p;
char *p = GetCommandLine();
if (argc > 1) szCmd = strstr(p, argv[1]); p = GetCommandLine();
if (NULL == szCmd) szCmd = ""; szCmd = NULL;
else if (szCmd > p && szCmd[-1] == '\"') --szCmd; if (argc > 1)
return WinMain(GetModuleHandle(NULL), NULL, szCmd, SW_SHOWDEFAULT); szCmd = strstr(p, argv[1]);
if (NULL == szCmd)
szCmd = "";
else if (szCmd > p && szCmd[-1] == '\"')
--szCmd;
return WinMain(GetModuleHandle(NULL), NULL, szCmd, SW_SHOWDEFAULT);
} }