#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);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,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)
{
HDC hDC;
HBRUSH hBrush;
HPEN hPen;
PAINTSTRUCT PtStr;
static long nXChar,nCaps,nYChar,pointx,pointy;
TEXTMETRIC tm;
static char *textbuf[]=
{
"故人西辞黄鹤楼",
"烟花三月下扬州",
"孤帆远影碧空尽",
"唯见长江天际流",
};
switch(iMessage)
{
case WM_CREATE:
hDC=GetDC(hWnd);
GetTextMetrics(hDC,&tm);
nXChar=tm.tmAveCharWidth;
nYChar=tm.tmHeight+tm.tmExternalLeading;
ReleaseDC(hWnd,hDC);
return 0;
case WM_PAINT:
hDC=BeginPaint(hWnd,&PtStr);
SetMapMode(hDC,MM_ANISOTROPIC);
hPen=CreatePen(PS_SOLID,2,RGB(255,0,0));
SelectObject(hDC,hPen);
POINT point1[3];
point1[0].x=50;
point1[0].y=50;
point1[1].x=100;
point1[1].y=50;
point1[2].x=75;
point1[2].y=100;
Polygon(hDC,point1,3);
Ellipse(hDC,50,100,100,150);
POINT point2[5];
point2[0].x=75;
point2[0].y=150;
point2[1].x=50;
point2[1].y=175;
point2[2].x=50;
point2[2].y=225;
point2[3].x=100;
point2[3].y=225;
point2[4].x=100;
point2[4].y=175;
Polygon(hDC,point2,5);
hBrush=(HBRUSH)GetStockObject(BLACK_BRUSH);
SelectObject(hDC,hBrush);
RoundRect(hDC,200,50,250,100,15,15);
Pie(hDC,200,100,250,150,215,100,230,100);
Rectangle(hDC,200,150,250,200);
int i,j;
for(i=4;i>0;i--)
for(j=0;j<7;j++)
{
pointx=500+i*nXChar*5;
pointy=50+j*(nYChar+nCaps);
TextOut(hDC, pointx, pointy,textbuf[4-i]+j*2,2);
}
EndPaint(hWnd,&PtStr);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
}
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow("WinFill",
"VC++ 实习一",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)
return FALSE;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS WndClass;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=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="WinFill";
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW|CS_VREDRAW;
return RegisterClass(&WndClass);
}