您的位置:首页 > 其它

利用 GDI+ 绘制旋转的太极

2013-05-24 14:37 363 查看
1 // TaiJi.cpp : 定义应用程序的入口点。

2 //

3

4 #include "stdafx.h"

5 #include "TaiJi.h"

6

7 /*

8 * 由于需要用GDI+进行绘图,所以需要先含GDI+库

9 * 首先,包含Gdiplus.h头文件, 引入Gdiplus命名空间, 包含gdiplus.lib库

10 * 在stdafx.h头文件中,取消 WIN32_LEAN_AND_MEAN 宏的定义,如果不取消这个宏定义,编译时会报错.

11 */

12 #include <GdiPlus.h>

13 using namespace Gdiplus;

14 #pragma comment(lib, "gdiplus.lib")

15

16 #define MAX_LOADSTRING 100

17

18 // 全局变量:

19 HINSTANCE hInst; // 当前实例

20 TCHAR szTitle[MAX_LOADSTRING] = TEXT("旋转的太极"); // 标题栏文本

21 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名

22

23 ULONG_PTR GdiplusToken;

24

25 // 此代码模块中包含的函数的前向声明:

26 ATOM MyRegisterClass(HINSTANCE hInstance);

27 BOOL InitInstance(HINSTANCE, int);

28 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

29 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

30

31 int APIENTRY _tWinMain(HINSTANCE hInstance,

32 HINSTANCE hPrevInstance,

33 LPTSTR lpCmdLine,

34 int nCmdShow)

35 {

36 UNREFERENCED_PARAMETER(hPrevInstance);

37 UNREFERENCED_PARAMETER(lpCmdLine);

38

39 // TODO: 在此放置代码。

40 MSG msg;

41 HACCEL hAccelTable;

42

43 // 初始化全局字符串

44 LoadString(hInstance, IDC_TAIJI, szWindowClass, MAX_LOADSTRING);

45 MyRegisterClass(hInstance);

46

47 // 执行应用程序初始化:

48 if (!InitInstance (hInstance, nCmdShow))

49 {

50 return FALSE;

51 }

52

53 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TAIJI));

54

55 // 主消息循环:

56 while (GetMessage(&msg, NULL, 0, 0))

57 {

58 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))

59 {

60 TranslateMessage(&msg);

61 DispatchMessage(&msg);

62 }

63 }

64

65 return (int) msg.wParam;

66 }

67

68

69 ATOM MyRegisterClass(HINSTANCE hInstance)

70 {

71 WNDCLASSEX wcex;

72

73 wcex.cbSize = sizeof(WNDCLASSEX);

74

75 wcex.style = CS_HREDRAW | CS_VREDRAW;

76 wcex.lpfnWndProc = WndProc;

77 wcex.cbClsExtra = 0;

78 wcex.cbWndExtra = 0;

79 wcex.hInstance = hInstance;

80 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TAIJI));

81 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);

82 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

83 wcex.lpszMenuName = NULL;

84 wcex.lpszClassName = szWindowClass;

85 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

86

87 return RegisterClassEx(&wcex);

88 }

89

90 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

91 {

92 HWND hWnd;

93

94 hInst = hInstance; // 将实例句柄存储在全局变量中

95

96 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,

97 CW_USEDEFAULT, 0, 600, 400, NULL, NULL, hInstance, NULL);

98

99 if (!hWnd)

{

return FALSE;

}

ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

return TRUE;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

PAINTSTRUCT ps;

HDC hdc;

GdiplusStartupInput gdiplusStartupInput;

const int width = 300;

const int height = 300;

static RECT rect;

static Bitmap *pBufferBmp;

static Graphics *pGrphBmp;

static SolidBrush* pBrsh1;

static SolidBrush* pBrsh2;

switch (message)

{

case WM_CREATE:

//初始化GDI+

GdiplusStartup(&GdiplusToken, &gdiplusStartupInput, NULL);

//获取客户区的大小,计算位图在客户区居中显示的位置

GetClientRect(hWnd, &rect);

rect.left = (rect.right - width) / 2;

rect.right = rect.left + width;

rect.top = (rect.bottom - height) / 2;

rect.bottom = rect.top + height;

//创建一个黑色画刷

pBrsh1 = new SolidBrush(Color::Black);

//创建一个白色画刷

pBrsh2 = new SolidBrush(Color::White);

//创建一个宽高300像素的正方形位图

pBufferBmp = new Bitmap(width, height);

pGrphBmp = Graphics::FromImage(pBufferBmp);

//设置绘制图形时消除锯齿

pGrphBmp->SetSmoothingMode(SmoothingModeAntiAlias);

//设置坐标原点位于位图的中心

pGrphBmp->TranslateTransform(float(width/2), float(height/2));

SetTimer(hWnd, 1, 30, NULL);

break;

case WM_TIMER:

//逆时针每次旋转坐标5度

pGrphBmp->RotateTransform(-5);

//用黄绿色填充位图背景

pGrphBmp->Clear(Color::YellowGreen);

//用黑色画刷绘制左侧的黑色半圆

pGrphBmp->FillPie(pBrsh1, -100, -100, 200, 200, 90, 180);

//用白色画刷绘制右侧的白色半圆

pGrphBmp->FillPie(pBrsh2, -100, -100, 200, 200, 90, -180);

//绘制黑色阴阳鱼的鱼头

pGrphBmp->FillPie(pBrsh1, -51, 0, 100, 100, 90, -180);

//绘制白色阴阳鱼的鱼头

pGrphBmp->FillPie(pBrsh2, -49, -100, 100, 100, 90, 180);

//绘制黑色阴阳鱼的眼睛

pGrphBmp->FillEllipse(pBrsh1, -10, -60, 20, 20);

//绘制白色阴阳鱼的眼睛

pGrphBmp->FillEllipse(pBrsh2, -10, 40, 20, 20);

InvalidateRect(hWnd, &rect, FALSE);

break;

case WM_PAINT:

{

hdc = BeginPaint(hWnd, &ps);

Graphics *pGrph = Graphics::FromHDC(hdc);

pGrph->DrawImage(pBufferBmp, rect.left, rect.top);

EndPaint(hWnd, &ps);

break;

}

case WM_DESTROY:

KillTimer(hWnd, 1);

delete pBrsh1;

delete pBrsh2;

delete pGrphBmp;

delete pBufferBmp;

//释放GDI+

GdiplusShutdown(GdiplusToken);

PostQuitMessage(0);

break;

default:

return DefWindowProc(hWnd, message, wParam, lParam);

}

return 0;

}



源码: http://pan.baidu.com/share/link?shareid=535102&uk=1678089569
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: