码迷,mamicode.com
首页 > Windows程序 > 详细

Windows编程——万花筒绘制

时间:2015-05-10 09:54:44      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:vc++   windows绘图   windows编程   

#include<windows.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define Pi 3.1415926
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 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)
{
	HDC hDC;   					
	HPEN hPen;					
	PAINTSTRUCT PtStr; 		
	int n = 25;
	POINT points[25];
	double angel = 2 * Pi / n;
	for (int i = 0; i < n; i++)
	{
		points[i].x = static_cast<long>(320 + 180 * cos(i*angel));
		points[i].y = static_cast<long>(250 + 180 * sin(i*angel));
	}
	switch (iMessage)  			
	{
	case WM_PAINT:    		
		hDC = BeginPaint(hWnd, &PtStr);
		for (int i = 0; i < 25; i++)
		{
            hPen = (HPEN)GetStockObject(NULL_PEN); 
		    SelectObject(hDC, hPen);    	
			LineTo(hDC, points[i].x, points[i].y);  		
			DeleteObject(hPen); 		
			if (i == 0)
			{
				for (int j = 1; j < n; j++)
				{
					switch (j % 6)
					{
					case 1:
					case 2:
						hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); 
						break;
					case 3:
					case 4:
						hPen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0)); 
						break;
					case 5:
					case 0:
						hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); 
						break;
					}
					SelectObject(hDC, hPen);  	
					LineTo(hDC, points[j].x, points[j].y);
					MoveToEx(hDC, points[i].x, points[i].y, NULL);
					DeleteObject(hPen);    
				}
			}
			else
			{
				for (int j = i + 1; j < n; j++)
				{
					switch (j % 6)
					{
					case 1:
					case 2:
						hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
						break;
					case 3:
					case 4:
						hPen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
						break;
					case 5:
					case 0:
						hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); 
						break;
					}
					SelectObject(hDC, hPen);  	
					LineTo(hDC, points[j].x, points[j].y);
					MoveToEx(hDC, points[i].x, points[i].y, NULL);
					DeleteObject(hPen);    
					Sleep(50);
				}
			}
		}

		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",  		 	 
		"                                                                         万花筒",
		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);
}
<img src="http://img.blog.csdn.net/20150509193843627?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTm9vYl9m/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<img src="http://img.blog.csdn.net/20150509193702193" alt="" />
<img src="http://img.blog.csdn.net/20150509193713784" alt="" />
<img src="http://img.blog.csdn.net/20150509193913860" alt="" />
<img src="http://img.blog.csdn.net/20150509193919148" alt="" />
<img src="http://img.blog.csdn.net/20150509193924530" alt="" />
<img src="http://img.blog.csdn.net/20150509193929210" alt="" />
<img src="http://img.blog.csdn.net/20150509193934530" alt="" />
<img src="http://img.blog.csdn.net/20150509193939818" alt="" />
<img src="http://img.blog.csdn.net/20150509193948102" alt="" />
<img src="http://img.blog.csdn.net/20150509193953219" alt="" />
<img src="http://img.blog.csdn.net/20150509193803688" alt="" />
<img src="http://img.blog.csdn.net/20150509193807775" alt="" />
<img src="http://img.blog.csdn.net/20150509193811785" alt="" />
<img src="http://img.blog.csdn.net/20150509193815482" alt="" />
<img src="http://img.blog.csdn.net/20150509194016666" alt="" />


Windows编程——万花筒绘制

标签:vc++   windows绘图   windows编程   

原文地址:http://blog.csdn.net/noob_f/article/details/45603809

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!