对获取设备描述表的简单总结

twitter logo GDI绘制

bachue posted @ 2010年9月27日 07:09 in Win32 C Programming with tags windows c Win32 logo twitter api , 2335 阅读

这次的作业,用GDI绘制任意卡通图,我不会画什么卡通图,于是就画了个twitter的logo。

按1(MM_TEXT),7(MM_ISOTROPIC),8(MM_ANISOTROPIC)会有不同的GDI映射。缩放窗口可以看出他们的区别。

#include<Windows.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
VOID DrawTwitter(HDC&);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
	static TCHAR szAppName[]=TEXT("GDIPaint");
	HWND hwnd;
	MSG msg;
	WNDCLASS wndclass;
	HBRUSH hBkBrush=CreateSolidBrush(RGB(0,204,255));
	
	wndclass.style=CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc=WndProc;
	wndclass.cbClsExtra=0;
	wndclass.cbWndExtra=0;
	wndclass.hInstance=hInstance;
	wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndclass.hbrBackground=hBkBrush;
	wndclass.lpszMenuName=NULL;
	wndclass.lpszClassName=szAppName;
	
	if(!RegisterClass(&wndclass))
	{
		MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
		return 0;
	}

	hwnd=CreateWindow(szAppName,TEXT("The GDI Paint"),WS_OVERLAPPEDWINDOW,
					  CW_USEDEFAULT,CW_USEDEFAULT,1050,450,
					  NULL,NULL,hInstance,NULL);
	ShowWindow(hwnd,iCmdShow);
	UpdateWindow(hwnd);
	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	
	DeleteObject(hBkBrush);
	return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
	static HDC hdc;
	static PAINTSTRUCT ps;
	static RECT rect;
	static HPEN hCyanPen,hWhitePen,hOldPen;
	static LOGPEN logpen;
	static INT iMode=8;
	switch(message)
	{
		case WM_PAINT:
			hdc=BeginPaint(hwnd,&ps);
			GetWindowRect(hwnd,&rect);
			SetMapMode(hdc, iMode);
			SetViewportExtEx(hdc,rect.right-rect.left,rect.bottom-rect.top,NULL);
			SetWindowExtEx(hdc,1050,450,NULL);

			logpen.lopnColor=RGB(255,255,255);
			logpen.lopnWidth.x=70;
			logpen.lopnWidth.y=70;
			hWhitePen=CreatePenIndirect(&logpen);
			logpen.lopnColor=RGB(0,204,255);
			logpen.lopnWidth.x=35;
			logpen.lopnWidth.y=35;
			hCyanPen=CreatePenIndirect(&logpen);
			SelectObject(hdc,hWhitePen);
			hOldPen=(HPEN)SelectObject(hdc,hWhitePen);
			DrawTwitter(hdc);
			Ellipse(hdc,105,195,145,235);
			Ellipse(hdc,175,150,215,245);
			Ellipse(hdc,275,150,315,245);
			Ellipse(hdc,375,150,415,245);
			Ellipse(hdc,560,200,580,220);
			Ellipse(hdc,660,200,680,220);
			SelectObject(hdc,hCyanPen);
			DrawTwitter(hdc);
			SelectObject(hdc,hOldPen);
			DeleteObject(hCyanPen);
			DeleteObject(hWhitePen);
			EndPaint(hwnd,&ps);
			return 0;
		case WM_KEYDOWN:
			iMode=wParam-0x30;
			InvalidateRect(hwnd,NULL,TRUE);
			return 0;
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
	}
	return DefWindowProc(hwnd,message,wParam,lParam);
}
VOID DrawTwitter(HDC& hdc)
{
	//"t"
		MoveToEx(hdc,50,100,NULL);
		LineTo(hdc,50,200);
		Arc(hdc,50,125,200,275,50,200,125,275);
		MoveToEx(hdc,125,275,NULL);
		LineTo(hdc,140,275);
		MoveToEx(hdc,50,150,NULL);
		LineTo(hdc,140,150);

		//"w"
		MoveToEx(hdc,190,150,NULL);
		LineTo(hdc,190,200);
		Arc(hdc,190,125,290,275,190,200,290,200);
		MoveToEx(hdc,290,150,NULL);
		LineTo(hdc,290,200);
		Arc(hdc,290,125,390,275,290,200,390,200);
		MoveToEx(hdc,390,150,NULL);
		LineTo(hdc,390,200);

		//"i"
		MoveToEx(hdc,440,170,NULL);
		LineTo(hdc,440,275);
		Ellipse(hdc,435,110,445,120);

		//"t"
		MoveToEx(hdc,490,100,NULL);
		LineTo(hdc,490,200);
		Arc(hdc,490,125,640,275,490,200,565,275);
		MoveToEx(hdc,490,175,NULL);
		LineTo(hdc,565,175);

		//"t"
		MoveToEx(hdc,605,100,NULL);
		LineTo(hdc,605,200);
		Arc(hdc,605,125,810,275,605,200,680,275);
		MoveToEx(hdc,605,175,NULL);
		LineTo(hdc,680,175);

		//"e"
		MoveToEx(hdc,730,210,NULL);
		LineTo(hdc,840,210);
		Arc(hdc,730,155,850,275,840,210,810,240);

		//"r"
		MoveToEx(hdc,890,210,NULL);
		LineTo(hdc,890,275);
		Arc(hdc,890,155,980,265,935,155,890,210);
		MoveToEx(hdc,935,155,NULL);
		LineTo(hdc,1000,155);
}

 

Avatar_small
Mike Ma 说:
2011年7月21日 03:25

你为什么偏偏要用 GDI 啊。。。Linux 下用不了啊,用 clutter, cairo 和 OpenGL 都可以啊,怨念。。。

Avatar_small
bachue 说:
2011年7月28日 15:16

@Mike Ma: 说了是作业,规定的。


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter