학교수업/윈도우즈 프로그래밍

3주차 - 복습 / struct와 class로 1, 2주차 프로그램 만들기

허세98 2021. 1. 21. 23:47
반응형

3주차는 동영상강의를 안 듣고온 학생들이 많아서 1, 2주차 복습 위주로 했다.

 

이 강의는 이름은 윈도우즈 프로그래밍이지만 원도우즈 프로그래밍보다 GUI기반 응용프로그램을 만들어보는데 초점을 둔다.

 

실제로 프로그래밍 환경이 많이 진화되어서  윈도우즈 WPF를 쓰면 직접하지 않고 컨트롤로 많이 움직이지만 본질은 모두 GUI기반으로 움직인다.

GUI란

graphical user interface의 약자로 사용자가 컴퓨터와 정보를 교환할 때, 그래픽을 통해 작업할 수 있는 환경을 말한다.

 

원도우 출력시 크기 설정

원도우의 크기 디폴트 값은 pixel이다.

하지만 Mapping을 통해 크기 값, 좌표의 축 등도 바꿀 수 있다.

 

이벤트 발생시

윈도우 창안에서 이벤트가 발생할 경우 OS가 윈도우 프로시져를 호출해준다.

원도우 프로시져 함수(마우스 왼쪽을 클릭하는 이벤트 발생시 원을 그리는 함수이다)

iMessages는 어떤 종류의 메시지인지 어떤 변화가 발생했는가에 관한 정보

ex) WM_PAINT, WM_LBUTTONDOWN 등이 있다

wParam, lParam은 어떤 값이 전달되는지는 메시지별로 다르다.

이 메시지에서는 OS는 lParam에 마우스 좌표를 넣어준다.

 

struct로 만든 원그리기 프로그램

typedef struct tagmyPOINT
{
	LONG  x;
	LONG  y;
	
} myPOINT;
myPOINT p[1000];

int iCount;

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	int x, y;
	HBRUSH hBrush;


	switch (iMessage) {
	case WM_LBUTTONDOWN:
		hdc = GetDC(hWnd);
		p[iCount].x = x = (WORD)(lParam);// LOWORD(lParam);
		p[iCount].y = y = HIWORD(lParam);
		iCount++;
		hBrush = CreateSolidBrush(RGB(255, 0, 0));//도형의 색을 선택
		SelectObject(hdc, hBrush); // 그리기 도구 hdc의 색을 지정
		Ellipse(hdc, x - 10, y - 10, x + 10, y + 10);
		return 0;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		for (int i = 0; i < iCount; i++)
		{
			hBrush = CreateSolidBrush(RGB(255, 0, 0));//도형의 색을 선택
			SelectObject(hdc, hBrush); // 그리기 도구 hdc의 색을 지정
			Ellipse(hdc, p[i].x - 10, p[i].y - 10, p[i].x + 10, p[i].y + 10);
		}
		
		EndPaint(hWnd, &ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}

 

class로 만든 원그리기 프로그램

class pt{
public:
	int x;
	int y;
};

class mypoints : public pt
{
public:
	pt p[1000];
	int iCount;
};
class pt2{
public:
	int x;
	int y;
};

class mypoints2 : public pt2
{
public:
	pt p[1000];
	int iCount;
};
mypoints my;
mypoints2 my2;
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	int x, y;
	HBRUSH hBrush;


	switch (iMessage) {
	case WM_LBUTTONDOWN:
		hdc = GetDC(hWnd);
		my.p[my.iCount].x = x = (WORD)(lParam);// LOWORD(lParam);
		my.p[my.iCount].y = y = HIWORD(lParam);
		my.iCount++;
		hBrush = CreateSolidBrush(RGB(255, 0, 0));//도형의 색을 선택
		SelectObject(hdc, hBrush); // 그리기 도구 hdc의 색을 지정
		Ellipse(hdc, x-10, y-10, x+10, y+10);
		return 0;
	case WM_RBUTTONDOWN:
		hdc = GetDC(hWnd);
		my2.p[my2.iCount].x = x = (WORD)(lParam);// = LOWORD(lParam);
		my2.p[my2.iCount].y = y = (WORD)(lParam >> 16); //16비트 잘라서 넣는다 HIWORD, LOWORD는 이 명령을 전처리
		my2.iCount++;
		hBrush = CreateSolidBrush(RGB(0, 0, 255));//도형의 색을 선택
		SelectObject(hdc, hBrush); // 그리기 도구 hdc의 색을 지정
		Ellipse(hdc, x - 10, y - 10, x + 10, y + 10);
		return 0;
	case WM_PAINT: 
		hdc = BeginPaint(hWnd, &ps);
		for (int i = 0; i < my.iCount; i++)
		{
			hBrush = CreateSolidBrush(RGB(255, 0, 0));//도형의 색을 선택
			SelectObject(hdc, hBrush); // 그리기 도구 hdc의 색을 지정
			Ellipse(hdc, my.p[i].x - 10, my.p[i].y - 10, my.p[i].x + 10, my.p[i].y + 10);
		}
		for (int i = 0; i < my2.iCount; i++)
		{
			hBrush = CreateSolidBrush(RGB(0, 0, 255));//도형의 색을 선택
			SelectObject(hdc, hBrush); // 그리기 도구 hdc의 색을 지정
			Ellipse(hdc, my2.p[i].x - 10, my2.p[i].y - 10, my2.p[i].x + 10, my2.p[i].y + 10);
		}
		EndPaint(hWnd, &ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}

class private을 호출하는 매서드 만들기

헤더파일과 소스파일 분리

class pt
{
private:
	int x;
	int y;
public:
	void Set(int x, int y)
	{
		//x = x;  //문제발생 compile error는 없으나.
		//y = y;
		this->x = x;
		this->y = y;
	}
	
	POINT Get()
	{
		POINT p;
		p.x = x;
		p.y = y;
		return p;
	}
};

class mypoints : public pt  //상속
{
	//public:   
private:
	pt p[1000]; //pt 클래스의 (instance, object) 배열
	int iCount;
public:
	void Add(int x, int y)
	{
		if (iCount < 1000)
		{
			//p[iCount].x = x;
			//p[iCount].y = y;
			p[iCount].Set(x, y);
			iCount++;
		}

	}
	void Draw(HDC hdc)
	{
		HBRUSH hBrush;
		hBrush = CreateSolidBrush(RGB(255, 0, 0));
		SelectObject(hdc, hBrush);
		for (int i = 0; i < iCount; i++)
		{
			Rectangle(hdc, p[i].Get().x - 10, p[i].Get().y - 10,
				p[i].Get().x + 10, p[i].Get().y + 10);
		}
	}
};

 ↑ points.h++

//점들 - objects
// description---> class : Abstraction 
#include "points.h"  //Code reusability 코드재사용
//실체화: instantiation
mypoints points;  // 정의한 클래스의 (instance, object) 선언

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{  //Window Procedure      //window handle(메시지가 발생한)
	HDC hdc;
	PAINTSTRUCT ps;
	int x, y;
	HBRUSH hBrush;
	
	
	switch (iMessage) {
	case 0x0001://WM_CREATE:
		hWndMain = hWnd;
		return 0;
	case  WM_LBUTTONDOWN: //왼쪽버튼을 눌렀을때
		hdc = GetDC(hWnd);
		x = LOWORD(lParam);  //Macro
		y = HIWORD(lParam);
		points.Add(x, y);
		InvalidateRect(hWnd, NULL, FALSE);
		return 0;
	
	case WM_PAINT:  // 화면을 다시 그려라 os가 보내는 MSG
		hdc = BeginPaint(hWnd, &ps);    //len = strlen(buf);  scanf("%d", &a);
		// 화면복구 routine
		points.Draw(hdc);
		EndPaint(hWnd, &ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}

 ↑ 소스 파일

반응형