#include <Windows.h>
#include <math.h>
#include <time.h>
#ifndef _countof
#define _countof(a) (sizeof(a)/sizeof(a[0]))
#endif
HRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void DrawStar(HDC hdc,int cx,int cy);
void DrawCharS(HDC hdc,int xLeft,int yTop,int xRight,int yBottom,int nThickness);
void DrawCharHalfS(HDC hdc,int xLeft,int yTop,int xRight,int yBottom,int nThickness);
void DrawStringFishc(HDC hdc,int xLeft,int yTop,int xRight,int yBottom,int nThickness);
void DrawCharC(HDC hdc,int xLeft,int yTop,int xRight,int yBottom,int nThickness);
void DrawFishc(HDC hdc);
static int cxClient,cyClient;
HRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd,&ps);
DrawFishc(hdc);
EndPaint(hWnd,&ps);
break;
case WM_TIMER:
GetClientRect(hWnd,&rect);
InvalidateRect(hWnd,&rect,TRUE);
break;
case WM_CREATE:
SetTimer(hWnd,1,400,NULL);
srand(time(NULL));
break;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_DESTROY:
KillTimer(hWnd,1);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
void DrawStar( HDC hdc,int cx,int cy )
{
XFORM xForm;
int i,savedState = SaveDC(hdc);
double theta = 0;
xForm.eDx = 0.0;
xForm.eDy = 0.0;
SetMapMode(hdc,MM_ISOTROPIC);
SetViewportOrgEx(hdc,cx/2,cy/2,NULL);
SetWindowExtEx(hdc,100,-100,NULL);
SetViewportExtEx(hdc,cx/2,cy/2,NULL);
SetGraphicsMode(hdc,GM_ADVANCED);
for(i=0;i<5;i++)
{
xForm.eM11 = (FLOAT) cos(theta);
xForm.eM12 = (FLOAT) sin(theta);
xForm.eM21 = -xForm.eM12;
xForm.eM22 = xForm.eM11;
SetWorldTransform(hdc, &xForm);
Rectangle(hdc,-10,90,10,0);
theta += 72*3.1415926/180;
}
RestoreDC(hdc,savedState);
}
void DrawStringFishc( HDC hdc,int xLeft,int yTop,int xRight,int yBottom,int thick )
{
XFORM xForm={0};
int savedState = SaveDC(hdc);
int w = (xRight-xLeft-2*thick)/7,h=yBottom-yTop-thick*2;
POINT ptF[]={0,0,w,0,w,thick,thick,thick,thick,h/2-thick/2,w,h/2-thick/2,w,h/2+thick/2,thick,h/2+thick/2,thick,h,0,h};
POINT ptI[]={0,0,w,0,w,thick,(w-thick)/2+thick,thick,(w-thick)/2+thick,thick+(h-2*thick),w,thick+(h-2*thick),w,h,
0,h,0,h-thick,(w-thick)/2,h-thick,(w-thick)/2,thick,0,thick};
POINT ptH[]={0,0,thick,0,thick,(h-thick)/2,w-thick,(h-thick)/2,w-thick,0,w,0,w,h,
w-thick,h,w-thick,(h+thick)/2,thick,(h+thick)/2,thick,h,0,h};
OffsetViewportOrgEx(hdc,xLeft,yTop,NULL);
OffsetViewportOrgEx(hdc,thick,thick,NULL);
//Óöà±ßÐλæÖÆ×ÖĸF¡£
Polygon(hdc,ptF,_countof(ptF));
OffsetViewportOrgEx(hdc,w*3/2,0,NULL);
//Óöà±ßÐλæÖÆ×ÖĸI¡£
Polygon(hdc,ptI,_countof(ptI));
//»æÖÆ×ÖĸS¡£
OffsetViewportOrgEx(hdc,w*3/2,-thick/2,NULL);
DrawCharS(hdc,0,0,w,h+thick,thick);
OffsetViewportOrgEx(hdc,w*3/2,thick/2,NULL);
//Óöà±ßÐλæÖÆ×ÖĸH¡£
Polygon(hdc,ptH,_countof(ptH));
OffsetViewportOrgEx(hdc,w*3/2,0,NULL);
//»æÖÆ×ÖĸC¡£
DrawCharC(hdc,0,0,w,h,thick);
RestoreDC(hdc,savedState);
}
void DrawCharS( HDC hdc,int xLeft,int yTop,int xRight,int yBottom,int nThickness )
{
int nSavedDC;
XFORM xForm={0};
nSavedDC = SaveDC(hdc);
xForm.eM11 = xForm.eM22 = -1;//cos(¦Ð)
OffsetViewportOrgEx(hdc,0,nThickness/2,NULL);
DrawCharHalfS(hdc,xLeft,yTop,xRight,(yTop+yBottom)/2,nThickness);
OffsetViewportOrgEx(hdc,0,(yBottom-yTop)/2-nThickness,NULL);
SetGraphicsMode(hdc,GM_ADVANCED);
SetWorldTransform(hdc,&xForm);
DrawCharHalfS(hdc,xLeft,yTop,xRight,(yTop+yBottom)/2,nThickness);
RestoreDC(hdc,nSavedDC);
}
void DrawCharHalfS( HDC hdc,int xLeft,int yTop,int xRight,int yBottom,int nThickness )
{
HBRUSH hBrush;
LOGPEN pen;
HRGN hRgnLargeEllipse,hRgnSmallEllipse,hRgnRect,hRgn,hRgnTemp;
int rx,ry,nSavedDC = SaveDC(hdc);
rx = (xRight-xLeft)/2;
ry = (yBottom-yTop)/2;
OffsetViewportOrgEx(hdc,rx,ry,NULL);
hRgnLargeEllipse = CreateEllipticRgn(-rx,-ry,rx,ry);
hRgnSmallEllipse = CreateEllipticRgn(-(rx-nThickness),-(ry-nThickness),rx-nThickness,ry-nThickness);
hRgnRect = CreateRectRgn(0,-ry/4,rx,ry);
hRgn = CreateRectRgn(0,0,1,1);
hRgnTemp = CreateRectRgn(0,0,1,1);
CombineRgn(hRgnTemp,hRgnLargeEllipse,hRgnRect,RGN_DIFF);
CombineRgn(hRgn,hRgnTemp,hRgnSmallEllipse,RGN_DIFF);
PaintRgn(hdc,hRgn);
GetObject(GetCurrentObject(hdc,OBJ_PEN),sizeof(pen),&pen);
if(pen.lopnStyle != PS_NULL)
{
hBrush = CreateSolidBrush(pen.lopnColor);
FrameRgn(hdc,hRgn,hBrush,1,1);
DeleteObject(hBrush);
}
DeleteObject(hRgnLargeEllipse);
DeleteObject(hRgnSmallEllipse);
DeleteObject(hRgnRect);
DeleteObject(hRgn);
DeleteObject(hRgnTemp);
RestoreDC(hdc,nSavedDC);
}
void DrawCharC( HDC hdc,int xLeft,int yTop,int xRight,int yBottom,int nThickness )
{
HBRUSH hBrush;
LOGPEN pen;
HRGN hRgnLargeEllipse,hRgnSmallEllipse,hRgnRect,hRgn,hRgnTemp;
int rx,ry,nSavedDC = SaveDC(hdc);
rx = (xRight-xLeft)/2;
ry = (yBottom-yTop)/2;
OffsetViewportOrgEx(hdc,rx,ry,NULL);
hRgnLargeEllipse = CreateEllipticRgn(-rx,-ry,rx,ry);
hRgnSmallEllipse = CreateEllipticRgn(-(rx-nThickness),-(ry-nThickness),rx-nThickness,ry-nThickness);
hRgnRect = CreateRectRgn(0,-ry/2,rx,ry/2);
hRgn = CreateRectRgn(0,0,1,1);
hRgnTemp = CreateRectRgn(0,0,1,1);
CombineRgn(hRgnTemp,hRgnLargeEllipse,hRgnRect,RGN_DIFF);
CombineRgn(hRgn,hRgnTemp,hRgnSmallEllipse,RGN_DIFF);
PaintRgn(hdc,hRgn);
GetObject(GetCurrentObject(hdc,OBJ_PEN),sizeof(pen),&pen);
if(pen.lopnStyle != PS_NULL)
{
hBrush = CreateSolidBrush(pen.lopnColor);
FrameRgn(hdc,hRgn,hBrush,1,1);
DeleteObject(hBrush);
}
DeleteObject(hRgnLargeEllipse);
DeleteObject(hRgnSmallEllipse);
DeleteObject(hRgnRect);
DeleteObject(hRgn);
DeleteObject(hRgnTemp);
RestoreDC(hdc,nSavedDC);
}
void DrawFishc(HDC hdc)
{
HDC hdcMem;
HBRUSH hBrush;
HBITMAP hBitmap;
int a;
RECT rect,rcClient={0,0,cxClient,cyClient};
int nMinW=cxClient/3,nMinH=cyClient/3;
FillRect(hdc,&rcClient,GetStockObject(WHITE_BRUSH));
rect.left = rand()%(cxClient-nMinW);
rect.top = rand()%(cyClient-nMinH);
rect.right = nMinW+rand()%(cxClient-nMinW-rect.left+1);//¿í¶È¡£
a = (2+rand()%3);//2-4
rect.bottom = rect.right/a;//¸ß¶È¡£
if(rect.bottom>cyClient-rect.top)
{
rect.bottom = nMinH+rand()%(cyClient-nMinH-rect.top+1);//¸ß¶È¡£
rect.right = rect.bottom*a;
}
else if(rect.bottom<nMinH)
{
rect.bottom = nMinH;
rect.right = nMinW;
}
rect.right += rect.left;
rect.bottom += rect.top;
a = (int)hypot(rect.right-rect.left,rect.bottom-rect.top)/30;
hdcMem = CreateCompatibleDC(hdc);
hBitmap = CreateCompatibleBitmap(hdc,a,a);
SelectObject(hdcMem,hBitmap);
SetRect(&rcClient,0,0,a,a);
InvertRect(hdcMem,&rcClient);
hBrush = CreateSolidBrush(RGB(rand()%256,rand()%256,rand()%256));
SelectObject(hdcMem,hBrush);
DrawStar(hdcMem,a,a);
SelectObject(hdc,CreatePatternBrush(hBitmap));
DrawStringFishc(hdc,rect.left,rect.top,rect.right,rect.bottom,a);
DeleteDC(hdcMem);
DeleteObject(hBrush);
DeleteObject(hBitmap);
DeleteObject(SelectObject(hdc,GetStockObject(WHITE_BRUSH)));
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd )
{
WNDCLASS wndcls;
HWND hWnd;
TCHAR *szClassName = TEXT("MyWin32App");
MSG msg;
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor = LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndcls.hInstance = hInstance;
wndcls.lpfnWndProc = WindowProc;
wndcls.lpszClassName = szClassName;
wndcls.lpszMenuName = NULL;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
if(!RegisterClass(&wndcls))
{
MessageBox(NULL,TEXT("×¢²á´°¿ÚÀàʧ°Ü£¡"),NULL,MB_ICONERROR);
return -1;
}
hWnd = CreateWindow(szClassName,TEXT("CÓïÑÔ Win32 ³ÌÐò"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,0);
ShowWindow(hWnd,nShowCmd);
UpdateWindow(hWnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}