绘图基础

#include <Windows.h>
#include <stdio.h>
//#include "resource.h"
//#include <afxwin.h>
#include <math.h>

#define TWO_PI (2.0*3.1415926)
LRESULT CALLBACK MyWindowProc(
							  HWND hwnd, 
							  UINT uMsg, 
							  WPARAM wParam, 
							  LPARAM lParam 
							  ); 
void DrawRectangle(HWND hwnd);
int cxClient,cyClient;
int WINAPI WinMain(  HINSTANCE hInstance,  HINSTANCE hPrevInstance, LPSTR lpCmdLine,  int nShowCmd )
{
	HWND hwnd;
	WNDCLASS wndclass;
	MSG msg;
	static TCHAR MyAppName[]=TEXT("My first windows app");
	wndclass.style=CS_HREDRAW|CS_VREDRAW;
	wndclass.lpfnWndProc=MyWindowProc;
	wndclass.cbClsExtra=0;//预留的额外空间
	wndclass.cbWndExtra=0;
	wndclass.hInstance=hInstance;
	wndclass.hIcon=::LoadIcon(NULL,IDI_INFORMATION);
	wndclass.hCursor=::LoadCursor(NULL,IDC_ARROW);//::LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR1));
	wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName=NULL;
	wndclass.lpszClassName=TEXT("My first windows app");
	if (!RegisterClass(&wndclass))
	{
		int i=GetLastError();
		//char str[25]={0};
		wchar_t chResult[128]={0};
		_itow(i,chResult,10);
		//sprintf(str,TEXT("%d"),i);
		//sprintf(buffer,L"Key State = 0X%X  ",key_state);
		MessageBox(NULL,chResult,TEXT("Error"),MB_OK);
		//AfxMessageBox(TEXT("Error") );
	}
	hwnd=CreateWindow( MyAppName,
		TEXT("My first windows"),
		WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL,
		CW_USEDEFAULT ,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		NULL,NULL,
		hInstance,
		NULL
		);
	ShowWindow(hwnd,SW_SHOWNORMAL); 
	//ShowWindow(hwnd,iCmdShow);
	UpdateWindow(hwnd);
	while(TRUE)
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if(msg.message==WM_QUIT)
				break;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
			DrawRectangle(hwnd);
	}
	
	return msg.wParam;
}
LRESULT CALLBACK MyWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	static HRGN hRgnClip;
	//static int cxClient,cyClient;
	double fAngle,fRadius;
	HCURSOR hCursor;
	HDC hdc;
	HRGN hRgnTem[6];
	int i;
	PAINTSTRUCT ps;
	switch(uMsg)
	{
	case WM_SIZE:
		cxClient=LOWORD(lParam);
		cyClient=HIWORD(lParam);

		hCursor=SetCursor(LoadCursor(NULL,IDC_WAIT));
		ShowCursor(TRUE);

		if (hRgnClip)
		{
			DeleteObject(hRgnClip);
		}
		hRgnTem[0]=CreateEllipticRgn(0,cyClient/3,cxClient/2,2*cyClient/3);//创建椭圆区域
		hRgnTem[1]=CreateEllipticRgn(cxClient/2,cyClient/3,cxClient,2*cyClient/3);
		hRgnTem[2]=CreateEllipticRgn(cxClient/3,0,2*cxClient/3,cyClient/2);
		hRgnTem[3]=CreateEllipticRgn(cxClient/3,cyClient/2,2*cxClient/3,cyClient);
		hRgnTem[4]=CreateRectRgn(0,0,1,1);//初始化一个矩形区域
		hRgnTem[5]=CreateRectRgn(0,0,1,1);
		hRgnClip=CreateRectRgn(0,0,1,1);

		CombineRgn(hRgnTem[4],hRgnTem[0],hRgnTem[1],RGN_XOR);//合并并赋给hRgnTem[4]
		CombineRgn(hRgnTem[5],hRgnTem[2],hRgnTem[3],RGN_XOR);
		CombineRgn(hRgnClip,hRgnTem[4],hRgnTem[5],RGN_XOR);
		for (i=0;i<6;i++)
		{
			DeleteObject(hRgnTem[i]);
		}
		SetCursor(hCursor);
		ShowCursor(FALSE);
		return 0;
	case WM_PAINT:
		hdc=BeginPaint(hwnd,&ps);
		SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);//将视点坐标移至客户区中心位置
		SelectClipRgn(hdc,hRgnClip);//将区域选入DC

		fRadius=_hypot(cxClient/2.0,cyClient/2.0);
		for (fAngle=0.0;fAngle<TWO_PI;fAngle+=TWO_PI/360)
		{
			MoveToEx(hdc,0,0,NULL);
			LineTo(hdc,(int)(fRadius*cos(fAngle)+0.5),(int)(-fRadius*sin(fAngle)+0.5));
		}
		EndPaint(hwnd,&ps);
		return 0;
	case WM_DESTROY:
		DeleteObject(hRgnClip);
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
void DrawRectangle(HWND hwnd)
{
	RECT rect;
	HBRUSH hBrush;
	if (cxClient==0||cyClient==0)
	{
		return;
	}
	SetRect(&rect,rand()%cxClient,rand()%cyClient,rand()%cxClient,rand()%cyClient);
	hBrush=CreateSolidBrush(RGB(rand()%256,rand()%256,rand()%256));
	HDC hdc=GetDC(hwnd);
	FillRect(hdc,&rect,hBrush);
	ReleaseDC(hwnd,hdc);
	DeleteObject(hBrush);
}

  

原文地址:https://www.cnblogs.com/xzlq/p/3116084.html