第一次作业
#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
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);
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_KALEIDOSCOPE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_KALEIDOSCOPE));
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
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);
}
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;
}
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);
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;
}
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;
}
第二次作业
#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);
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;
}
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);
}
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;
}
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("欲穷千里目更上一层楼");
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;
}
第三次作业
#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);
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;
}
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);
}
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;
}
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;
}