A more "complete" example:
Code:
/* winapp.c.in */
/*
Windows App template, in C.
To compile (on Linux, using MinG64), substitute the @vars@ first (for example):
$ sed "s/@appclass@/MyWinAppClass/;
s/@uniqueid@/`uuidgen -r`/;
s/@width@/640/;
s/@height/480/" winapp.c.in > winapp.c
Then:
$ x86_64-w64-mingw32-gcc -O2 -D__SINGLE_INSTANCE__ -c -o winapp.o winapp.c
$ x86_64-w64_mingw32-windres winapp.rc winappres.o
$ x86_64-w64-mingw32-gcc -Wl,-subsystem=windows -o winapp.exe winapp.o winappres.o -lgdi32
*/
#include <windows.h>
LRESULT CALLBACK WindowMessageHandler(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
HWND hWnd;
MSG msg;
static const char *className = "@appclass@";
WNDCLASS wc = {};
/* __SINGLE_INSTANCE__ must be defined if you want only one
instance of this windows running. */
#ifdef __SINGLE_INSTANCE__
/* Changed at compile time. */
#define MUTEX_NAME "@uniqueid@"
CreateMutex(NULL, TRUE, MUTEX_NAME);
/* If named mutex already exists, exit. */
if (GetLastError() != NO_ERROR)
return 0;
#endif
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = WindowMessageHandler;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, "WINAPP_ICON"); /* WINAPP_ICON defined in resource file. */
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = className;
RegisterClass(&wc);
if ((hWnd = CreateWindowEx(
0,
className,
"@apptitle@", /* Changed at compile time */
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
@width@, @height@, /* Changed at compile time */
NULL,
NULL,
hInstance,
NULL
)) == NULL)
{
MessageBox(NULL,
"Error creating window!",
"Error",
MB_OK | MB_ICONERROR);
return 0;
}
UpdateWindow(hWnd);
ShowWindow(hWnd, nCmdShow);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/* Can be any value... 100 is a good one! */
#define ID_STATIC1 100
LRESULT CALLBACK WindowMessageHandler(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
RECT rc;
HDC hDC;
HWND hStaticWnd;
switch (uMsg)
{
case WM_CREATE:
/* Don't remember if this is the correct way! */
GetClientRect(hWnd, &rc);
/* This will not fail!
Don't remember if SS_SIMPLE has VCENTER and HCENTER attribs. */
hStaticWnd = CreateWindowEx(
0,
"static",
"Hello, Windows!",
WS_VISIBLE | WS_CHILD | SS_SIMPLE,
rc.left, rc.top,
rc.right - rc.left, rc.bottom - rc.top,
hWnd,
(HMENU)ID_STATIC1,
((LPCREATESTRUCT)lParam)->hInstance,
NULL
);
/* Set Windows default font to child window */
SendMessage( hStaticWnd,
WM_SETFONT,
(WPARAM)GetStockObject(DEFAULT_GUI_FONT),
TRUE );
break;
/* Don't need to handle WM_CLOSE here! */
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
Code:
# Makefile
CC = x86_64-w64-mingw32-gcc
RC = x86_64-w64-mingw32-windres
CFLAGS = -O2 -mtune=native -D__SINGLE_INSTANCE__
winapp.exe: winapp.o winappres.o
$(CC) -o $@ $^ -Wl,-subsystem=windows -lgdi32
winapp.o: winapp.c
winappres.o: winapp.rc
$(RC) $< $@
winapp.c: winapp.c.in
sed "s/@uniqueid@/$$(uuidgen -r)/;s/@appclass@/MyAppClass/;s/@width@/640/;s/@height@/480/" winapp.c.in > winapp.c
.PHONY: clean
clean:
-rm *.c *.o *.exe
Code:
/* winapp.rc */
CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "winapp.manifest"
WINAPP_ICON ICON "winapp.ico"
Code:
<!-- winapp.manifest -->
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
<dependency>
<dependentAssembly>
<assemblyIdentity type="Win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0"
processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
</dependentAssembly>
</dependency>
</assembly>
The winapp.ico is an small icon file (choose yours)... 32x32 pixels is enough
Just call 'make' (on Linux, x86-64, with MinGW-64 installed)...