Code:
//gcc prog.c -mwindows -o prog.exe
#include <windows.h>
DWORD dwStyle = WS_POPUP|WS_CAPTION|WS_SYSMENU;
//DWORD dwStyle = WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX;
int nLines = 10;
int nCols = 40;
HINSTANCE hInst;
HWND hwnd;
HFONT hFont;
int cxChar,cyChar;
int initMainWindow(void);
int initWindowPos(void);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
/*************************************************************************/
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrev,LPSTR args,int nShow)
{
MSG msg ;
hInst = hInstance;
initMainWindow();
initWindowPos();
ShowWindow(hwnd, nShow);
UpdateWindow(hwnd);
while( GetMessage(&msg,0,0,0) > 0 ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
/*************************************************************************/
int initMainWindow(void) {
WNDCLASS wc = {0};
wc.lpszClassName = "myClass";
wc.hInstance = hInst ;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);
RegisterClass(&wc);
hwnd = CreateWindow("myClass","test",
dwStyle,
0,0,0,0,
0,0,hInst,0);
return 0;
}
/*************************************************************************/
int initWindowPos(void) {
HDC hdc;
TEXTMETRIC tm;
int cx,cy;
RECT rc;
hFont = CreateFont(
16,0,0,0,FW_DONTCARE,
0,0,0,ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE,
"Courier New");
// Extract font dimensions from the text metrics.
hdc = GetDC(hwnd);
SelectObject(hdc,hFont);
GetTextMetrics(hdc,&tm);
ReleaseDC(hwnd,hdc);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
cx = nCols * cxChar;
cy = nLines * cyChar;
rc.left = 0;
rc.top = 0;
rc.right = cx;
rc.bottom = cy;
AdjustWindowRect( &rc, dwStyle, FALSE );
SetWindowPos(hwnd,HWND_TOPMOST,0,0,rc.right-rc.left,rc.bottom-rc.top,SWP_NOZORDER);
return 0;
}
/*************************************************************************/
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
int ii;
char abc[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
switch(msg) {
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
SelectObject(hdc,hFont);
for (ii=0;ii<nLines;ii++) {
TextOut(hdc,0,ii*cyChar,abc,strlen(abc));
}
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
}