728x90
반응형
Polyline 사용하여 별 그리기
[전체코드]
// WindowsProject6.cpp : 애플리케이션에 대한 진입점을 정의합니다.
//
#include "framework.h"
#include "WindowsProject6.h"
#include <math.h>
#define MAX_LOADSTRING 100
#define BTN1 1000
#define BTN2 ((BTN1) + 1000)
// 전역 변수:
HINSTANCE hInst; // 현재 인스턴스입니다.
WCHAR szTitle[MAX_LOADSTRING]; // 제목 표시줄 텍스트입니다.
WCHAR szWindowClass[MAX_LOADSTRING]; // 기본 창 클래스 이름입니다.
HWND hWnd;
HWND hWndButton1, hWndButton2;
// 이 코드 모듈에 포함된 함수의 선언을 전달합니다:
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_WINDOWSPROJECT6, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 애플리케이션 초기화를 수행합니다:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDOWSPROJECT6));
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_WINDOWSPROJECT6));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINDOWSPROJECT6);
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 = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
0, 0, 800, 600, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 함수: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 용도: 주 창의 메시지를 처리합니다.
//
// WM_COMMAND - 애플리케이션 메뉴를 처리합니다.
// WM_PAINT - 주 창을 그립니다.
// WM_DESTROY - 종료 메시지를 게시하고 반환합니다.
//
//
#define RADIUS 200
#define ANGULAR 5
#define STARPOINT ((ANGULAR) + 1)
#define _PI 3.141592f
#define Radian(Degree) (Degree) * (_PI) / (180.f)
float change = 0.f;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
hWndButton1 = CreateWindowW(_T("BUTTON"), _T("회전"), WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
10, 10, 80, 30, hWnd, (HMENU)BTN1, hInst, NULL);
hWndButton2 = CreateWindowW(_T("BUTTON"), _T("정지"), WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
10, 50, 80, 30, hWnd, (HMENU)BTN2, hInst, NULL);
} break;
case WM_TIMER:
{
switch (wParam)
{
case BTN1:
{
InvalidateRect(hWnd, NULL, true);
} break;
}
} break;
case WM_COMMAND:
{
switch (wParam)
{
case BTN1:
{
HANDLE hTimer1 = (HANDLE)SetTimer(hWnd, BTN1, 50, NULL);
} break;
case BTN2:
{
KillTimer(hWnd, BTN1);
} break;
}
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);
RECT rc;
GetClientRect(hWnd, &rc);
WCHAR ar[32];
wsprintf(ar, L"%d %d %d %d", rc.left, rc.top, rc.right, rc.bottom); // client 창 좌표 확인 , 0, 0, 784, 541
int Xcenter = rc.right / 2;
int Ycenter = rc.bottom / 2;
float StartAngle = Radian(-90.f) + change;
float PlusAngle = Radian((360.f / ANGULAR) * 2.f);
POINT star[STARPOINT];
for (int i = 0; i < STARPOINT; i++)
{
int X = Xcenter + RADIUS * cosf(StartAngle + (PlusAngle * i));
int Y = Ycenter + RADIUS * sinf(StartAngle + (PlusAngle * i));
star[i] = {X, Y};
}
Polyline(hdc, star, 6);
change += 0.05f;
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;
[코드 요약 설명]
float degreeToRadian(float de)
{
return de * 3.141592 / 180.0f);
}
// 함수를 #define으로 바꿔도 된다.
// #define degreeToRadian(degree) (((degree) * 3.141592f) / 180.0f)
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
POINT pts[6];
// 규칙 찾기
/*
pts[0].x = cosf(90 + 144 * 0) * R; //+ 144 * 0 생략 되어있음
pts[0].y = sinf(90 + 144 * 0) * R;
pts[1].x = cosf(90 + 144 * 1) * R;
pts[1].y = sinf(90 + 144 * 1) * R;
pts[2].x = cosf(90 + 144 * 2) * R;
pts[2].y = sinf(90 + 144 * 2) * R;
pts[3].x = cosf(90 + 144 * 3) * R;
pts[3].y = sinf(90 + 144 * 3) * R;
pts[4].x = cosf(90 + 144 * 4) * R;
pts[4].y = sinf(90 + 144 * 4) * R;
pts[5].x = cosf(90 + 144 * 5) * R;
pts[5].y = sinf(90 + 144 * 5) * R;
*/
// 위 코드 for문으로 바꾸기
//라디안으로 변환시키는 코드 각도(= 90 + 144 * 0) * 3.141592 / 180.0f
float dx = 400.0f, dy = 300.0f;
for (int i = 0; i < 5; i++)
{
pts[i].x = cosf(degreeToRadian(ct + 144 * i)) * 100.0f + dx;
pts[i].y = sinf(degreeToRadian(ct + 144 * i)) * 100.0f + dy;
}
ct += 3.0f;
// TODO: 여기에 hdc를 사용하는 그리기 코드를 추가합니다...
Polyline(hdc, pt, 5);
EndPaint(hWnd, &ps);
}
[코드 설명]
pts[i].x = cosf(degreeToRadian(ct + 144 * i)) * 100.0f + dx;
pts[i].y = sinf(degreeToRadian(ct + 144 * i)) * 100.0f + dy;
ct += 3.0f;
```
위식을 아래와 같이 바꾸면 실행속도에서 비용을 절감 할 수 있다.
```
pts[i].x = cosf(degreeToRadian(144.0f * i)) + ct * 100.0f + dx;
pts[i].y = sinf(degreeToRadian(144.0f * i)) + ct * 100.0f + dy;
ct += 0.05f; // 라디안 값으로 바뀜
728x90
반응형
'Education > Edu | .net' 카테고리의 다른 글
# 2.8) [Win32API] 그래픽처리 (bitmap 출력하기) (0) | 2021.01.04 |
---|---|
# 2.7) [Win32API] 메세지 띄우는 법 (0) | 2021.01.04 |
# 2.5) [Win32API] 오각형 타이머 설정하여 회전시키기 (0) | 2021.01.04 |
# 2.4) [Win32API] 오각형 그리기 (0) | 2020.12.31 |
# 2.3) [Win32API] 윈도우즈 데스크톱 애플리케이션 코드 설명(2) (0) | 2020.12.31 |