#include<windows.h>
#include<stdlib.h>
#include<string.h>
long WINAPI WndProc(
HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam
);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);
HWND hWndMain;
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
MSG Message;
if(!InitWindowsClass(hInstance))
return FALSE;
if(!InitWindows(hInstance, nCmdShow))
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(
HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam
)
{
static long nXChar, nChar, nCaps, nYChar;
int pointx, pointy;
int i,j;
HDC hDC;
TEXTMETRIC tm;
PAINTSTRUCT PtStr;
static char *textbuf[4][7]=
{
{"故人西辞黄鹤楼"},
{"烟花三月下扬州"},
{"孤帆远影碧空尽"},
{"唯见长江天际流"}
};
switch(iMessage)
{
case WM_CREATE:
hDC=GetDC(hWnd);
GetTextMetrics(hDC,&tm);
nXChar=tm.tmAveCharWidth;
nYChar=tm.tmHeight+tm.tmExternalLeading;
nCaps=(tm.tmPitchAndFamily&1?3:2)*nXChar/2;
ReleaseDC(hWnd,hDC);
return 0;
case WM_PAINT:
hDC=BeginPaint(hWnd,&PtStr);
for(i=4; i>0; i--)
{
for(j=0; j<7; j++)
{
pointx=100+i*nXChar*5;
pointy=50+j*(nYChar+nCaps);
TextOut(hDC,pointx,pointy,textbuf[4-i][7]+j*2, 2);
}
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS WndClass;
WndClass.cbClsExtra=0;
WndClass.cbClsExtra=0;
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,"END");
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=WndProc;
WndClass.lpszClassName="WinText";
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW|CS_VREDRAW;
return RegisterClass(&WndClass);
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
char lpszName[]="WinText";
hWnd=CreateWindow(lpszName,
"文本显示",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)
return FALSE;
hWndMain=hWnd;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}