VC++面向对象与可视化程序设计(上)三次作业

第一次作业

// Kaleidoscope.cpp : Defines the entry point for the application.
//

#include "framework.h"
#include "Kaleidoscope.h"
#include <math.h>
#include <strsafe.h>

#define MAX_LOADSTRING 100
#define NUM 20             // 点的数量
#define CNUM 3             // 颜色的数量
#define PI 3.1415          // 圆周率
#define R 200              // 万花筒的半径

// Global Variables:
HINSTANCE hInst;                                // current instance
WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_KALEIDOSCOPE, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_KALEIDOSCOPE));

    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_KALEIDOSCOPE));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_KALEIDOSCOPE);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE: Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    // 保存所有点的坐标
    POINT points[NUM];
    // 客户区的宽高
    static int cxClient, cyClient;
    // 颜色数组
    COLORREF colors[CNUM] = {RGB(255,0,0), RGB(0,255,0), RGB(0,0,255)};
    int i, j, k;


    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        
        break;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);

            // 计算所有点的坐标
            for (i = 0; i < NUM; i++) {
                points[i].x = cxClient / 2 + cos(i * 2 * PI / NUM) * R;
                points[i].y = cyClient / 2 - sin(i * 2 * PI / NUM) * R;
            }
            
            // 绘图
            for (i = 0; i < NUM; i++) {
                for (j = 0; j < NUM; j++) {
                    if (j != i) {
                        // 切换颜色
                        for (k = 0; k < CNUM; k++) {
                            if (j % CNUM == k) {
                                SelectObject(hdc, CreatePen(PS_SOLID, 1, colors[k]));
                            }
                        }
                        MoveToEx(hdc, points[i].x, points[i].y, NULL);
                        LineTo(hdc, points[j].x, points[j].y);
                        Sleep(30);
                    }
                }
            }

            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

第二次作业

// CreateFontDemo.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "CreateFontDemo.h"
#include <math.h>

#define MAX_LOADSTRING 100
#define PI 3.1415926

// 全局变量:
HINSTANCE hInst;                                // 当前实例
TCHAR szTitle[MAX_LOADSTRING];                    // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

     // TODO: 在此放置代码。
    MSG msg;
    HACCEL hAccelTable;

    // 初始化全局字符串
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_CREATEFONTDEMO, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 执行应用程序初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CREATEFONTDEMO));

    // 主消息循环:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
//
//  注释:
//
//    仅当希望
//    此代码与添加到 Windows 95 中的“RegisterClassEx”
//    函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
//    这样应用程序就可以获得关联的
//    “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style            = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CREATEFONTDEMO));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = MAKEINTRESOURCE(IDC_CREATEFONTDEMO);
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

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

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

// 根据给定的宽度和高度自定义字体
HFONT MyCreateFont(int charHeight, int charWidth){
    HFONT hFont;
    hFont = CreateFont(
        charHeight,
        charWidth,
        0,
        0,
        FW_NORMAL,
        0,
        0,
        0,
        DEFAULT_CHARSET,
        OUT_DEFAULT_PRECIS,
        CLIP_DEFAULT_PRECIS,
        ANTIALIASED_QUALITY,
        DEFAULT_PITCH | FF_DONTCARE,
        L"微软雅黑");

    return hFont;
}

//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的: 处理主窗口的消息。
//
//  WM_COMMAND    - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY    - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent, x = 0, y = 0, i, j, charHeight, charWidth;
    PAINTSTRUCT ps;
    HDC hdc;
    HFONT hFont;
    TCHAR str[] = _T("欲穷千里目更上一层楼");
    // 要减1, 是因为多算了一个串尾
    int len = sizeof(str)/sizeof(TCHAR)-1;

    switch (message)
    {
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // 分析菜单选择:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        
        // 由高到低
        for(i = 0; i < len; i++){
            // 计算字符的高度和宽度
            charHeight = 40 - i*2;
            charWidth = 15;
            // 创建字体
            hFont = MyCreateFont(charHeight, charWidth);
            SelectObject(hdc, hFont);
            // 设置文字颜色
            SetTextColor(hdc, RGB(255-i*10, 0, 0));
            // 计算文字的输出位置
            x = i*30;
            y = 30 - charHeight/2;
            TextOut(hdc, x, y, &str[i], 1);
        }

        // 由低到高
        for(i = 0; i < len; i++){
            // 计算字符的高度和宽度
            charHeight = 22 + i*2;
            charWidth = 15;
            // 创建字体
            hFont = MyCreateFont(charHeight, charWidth);
            SelectObject(hdc, hFont);
            // 设置文字颜色
            SetTextColor(hdc, RGB(165+i*10, 0, 0));
            // 计算文字的输出位置
            x = i*30;
            y = 60 - charHeight/2;
            TextOut(hdc, x, y, &str[i], 1);
        }

        // 由低到高再到低
        for(i = 0; i < len; i++){
            // 计算字符的高度和宽度
            charHeight = 40 - abs(i-len/2)*4;
            charWidth = 15;
            // 创建字体
            hFont = MyCreateFont(charHeight, charWidth);
            SelectObject(hdc, hFont);
            // 设置文字颜色
            SetTextColor(hdc, RGB(0, 255-i*20, 0));
            // 计算文字的输出位置
            x = i*30;
            y = 100 - charHeight/2;
            TextOut(hdc, x, y, &str[i], 1);
        }

        // 由低到高到更高
        for(i = 0; i < len; i++){
            // 计算字符的高度和宽度
            charHeight = 40 + (i-len/2)*6;
            charWidth = 15;
            // 创建字体
            hFont = MyCreateFont(charHeight, charWidth);
            SelectObject(hdc, hFont);
            // 设置文字颜色
            SetTextColor(hdc, RGB(0, 0, 255-i*20));
            // 计算文字的输出位置
            x = i*30;
            y = 160 - charHeight/2;
            TextOut(hdc, x, y, &str[i], 1);
        }

        // 根据正弦曲线排列文字
        for(i = 0; i < len; i++){
            // 计算字符的高度和宽度
            charHeight = 40;
            charWidth = 15;
            // 创建字体
            hFont = MyCreateFont(charHeight, charWidth);
            SelectObject(hdc, hFont);
            // 设置文字颜色
            SetTextColor(hdc, RGB(80+i*10, 255, 255-i*10));
            // 计算文字的输出位置
            x = i*30;
            y = 250 - 40*sin(2*PI*i/(len-1));
            TextOut(hdc, x, y, &str[i], 1);
        }

        // 根据余弦曲线排列文字
        for(i = 0; i < len; i++){
            // 计算字符的高度和宽度
            charHeight = 40;
            charWidth = 15;
            // 创建字体
            hFont = MyCreateFont(charHeight, charWidth);
            SelectObject(hdc, hFont);
            // 设置文字颜色
            SetTextColor(hdc, RGB(80+i*10, 100, 255));
            // 计算文字的输出位置
            x = i*30;
            y = 400 - 40*cos(2*PI*i/(len-1));
            TextOut(hdc, x, y, &str[i], 1);
        }

        // 按照圆形排列文字
        for(i = 0; i < len; i++){
            // 计算字符的高度和宽度
            charHeight = 40;
            charWidth = 15;
            // 创建字体
            hFont = MyCreateFont(charHeight, charWidth);
            SelectObject(hdc, hFont);
            // 设置文字颜色
            SetTextColor(hdc, RGB(255-i*10, 144, 60));
            // 计算文字的输出位置
            x = 500 + 100*cos(2*PI*i/len);
            y = 350 + 100*sin(2*PI*i/len);
            TextOut(hdc, x, y, &str[i], 1);
        }

        // 按照半圆形排列文字
        for(i = 0; i < len; i++){
            // 计算字符的高度和宽度
            charHeight = 30-i*2;
            charWidth = 15-i;
            // 创建字体
            hFont = MyCreateFont(charHeight, charWidth);
            SelectObject(hdc, hFont);
            // 设置文字颜色
            SetTextColor(hdc, RGB(255, 150-i*10, 255));
            // 计算文字的输出位置
            x = 500 - 100*cos(PI*i/len);
            y = 150 - 100*sin(PI*i/len);
            TextOut(hdc, x, y, &str[i], 1);
        }


        EndPaint(hWnd, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

第三次作业

// RotateTriangle.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "RotateTriangle.h"
#include <math.h>
#include <strsafe.h>

#define MAX_LOADSTRING 100
#define PI 3.1415
#define TIMES 10

// 全局变量:
HINSTANCE hInst;                                // 当前实例
WCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

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

    // 初始化全局字符串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_ROTATETRIANGLE, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 执行应用程序初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ROTATETRIANGLE));

    MSG msg;

    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函数: MyRegisterClass()
//
//  目标: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ROTATETRIANGLE));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_ROTATETRIANGLE);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目标: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 将实例句柄存储在全局变量中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

// 创建指定角度的字体
HFONT CreateFont(int degree) {
	HFONT hFont = CreateFont(
		0,
		0,
		degree,
		0,
		FW_BOLD,
		0,
		0,
		0,
		DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		ANTIALIASED_QUALITY,
		DEFAULT_PITCH | FF_MODERN,
		_T("微软雅黑")
	);
	return hFont;
}

//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目标: 处理主窗口的消息。
//
//  WM_COMMAND  - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY  - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static RECT rect;
	// 保存三角形三个顶点的坐标 输出文字左上角坐标
	static POINT points[3], p;
	// 中心坐标		旋转的角度	三角形的边长  三角形到中心点的距离 
	static int x, y, degree = 0, len = 150, dis = 150;
	static TCHAR str[20];
	static HFONT hFont, hOldFont;
	static HBRUSH hBrush;

    switch (message)
    {
	case WM_CREATE:
		SetTimer(hWnd, 1, 1000, NULL);
		break;
	case WM_SIZE:
		x = LOWORD(lParam)/2;
		y = HIWORD(lParam)/2;
		break;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析菜单选择:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);			
			GetClientRect(hWnd, &rect);
			// 随机生成三角形背景颜色 
			int r = rand() * 255;
			int g = rand() * 255;
			int b = rand() * 255;
			hBrush = CreateSolidBrush(RGB(r, g, b));
			SelectObject(hdc, hBrush);
			// 绘制三角形
			Polygon(hdc, points, 3);
			// 设置新字体
			hOldFont = (HFONT)SelectObject(hdc, hFont);
			// 随机生成文字颜色 
			r = rand() * 255;
			g = rand() * 255;
			b = rand() * 255;
			// 文字背景透明
			SetBkMode(hdc, TRANSPARENT);
			SetTextColor(hdc, RGB(r, g, b));
			// 输出三角形中的文字
			TextOut(hdc, p.x, p.y, _T("三角形"), 3);
			// 设置回原来的字体
			SelectObject(hdc, hOldFont);
			SetTextColor(hdc, RGB(0, 0, 0));
			// 输出中间的文字
			DrawText(hdc, str, -1, &rect,
				DT_SINGLELINE | DT_CENTER | DT_VCENTER);
			
            EndPaint(hWnd, &ps);
        }
        break;
	case WM_TIMER:
		{
			// 根据旋转角度创建字体
			hFont = CreateFont((int)(360 * ((TIMES - 1) / TIMES))-degree*10);
			// 拼接字符串
			StringCchPrintf(str, 20, _T("三角形现在旋转到%d度!"), degree);
			// 角度转弧度
			double rad = degree * PI / 180;
			// 计算三角形顶点坐标
			points[0].x = x - dis * cos(rad);
			points[0].y = y - dis * sin(rad);
			points[1].x = x - (len + dis) * cos(rad);
			points[1].y = y - (len + dis) * sin(rad);
			// 
			double k = sqrt(pow(dis + len/2, 2) + 3*pow(len/2, 2));
			points[2].x = x - k * cos(rad + acos((dis+len/2) / k));
			points[2].y = y - k * sin(rad + acos((dis+len/2) / k) );

			// 计算输出文字左上角坐标
			double h = len * 0.4;
			double a = sqrt(pow(dis + len*0.7, 2) + pow(h, 2));
			p.x = x - a * cos(rad + asin(h / a));
			p.y = y - a * sin(rad + asin(h / a));

			// 计算每次的角度
			degree += 360 / TIMES;
			degree %= 360;

			InvalidateRect(hWnd, NULL, TRUE);
		}
		break;
    case WM_DESTROY:
		KillTimer(hWnd, 1);
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值