Graphics.h

#pragma once

#include<Windows.h>
/*函数功能:获得控制台窗口句柄*/
HWND  getConsoleHwnd(void);
/*函数功能:在窗口上用创建的hpen画笔以(cx,cy)为圆心,r为半径画圆*/
void  circle(HDC, HPEN hpen,int cx, int cy, int r);
/*函数功能:在窗口上用创建的画刷画坐标为(lx,ly)点*/
void  point(HDC, HBRUSH hbrush,int lx, int ly);
/*函数功能:在窗口上用创建的画笔画出以(sx,sy)作为起点,(ex,ey)作为终点的一条直线*/
void  line(HDC hdc, HPEN hpen, int sx, int sy, int ex, int ey);
/*函数功能:在窗口上用创建的画笔画出以(left,top)为左上角坐标,(right,bottom)为右下角坐标的矩形*/
void  rect(HDC hdc, HPEN hpen, int left, int top, int right, int bottom);

 Graphics.cpp 


#include"Graphics.h"
#define MY_BUFSIZE 100
HWND getConsoleHwnd(void)
{  
	char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated  
	char pszOldWindowTitle[MY_BUFSIZE]; // Contains original  
	GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);  
	wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(),  GetCurrentProcessId());  
	SetConsoleTitle(pszNewWindowTitle);  
	Sleep(40);  
	HWND hConsole=FindWindow(NULL, pszNewWindowTitle);  
	SetConsoleTitle(pszOldWindowTitle); 
	return hConsole;
}
void circle(HDC hdc, HPEN hPen,int cx, int cy, int r)
{
	SelectObject(hdc, hPen);
	Arc(hdc,cx-r,cy-r,cx+r,cy+r,cx-r,cy,cx+r,cy);
	Arc(hdc,cx-r,cy-r,cx+r,cy+r,cx+r,cy,cx-r,cy);
}
void point(HDC hdc, HBRUSH hBrush,int cx, int cy)
{
	HPEN hpen = CreatePen(0, 5, RGB(0, 0, 0));
	SelectObject(hdc, hpen);
	(HPEN)SelectObject(hdc,hBrush);
	Ellipse(hdc,cx,cy,cx+50,cy+50);
}
void line(HDC hdc, HPEN hpen, int sx, int sy, int ex, int ey)
{
	SelectObject(hdc, hpen);
	(HPEN)SelectObject(hdc,hpen);
	MoveToEx(hdc,sx,sy,NULL); 
	LineTo(hdc,ex,ey);
}
void rect(HDC hdc, HPEN hpen, int left, int top, int right, int bottom)
{
	SelectObject(hdc, hpen);
	(HPEN)SelectObject(hdc,hpen);
	Rectangle(hdc,left,top,right,bottom);
}

Figure.h

#include<Windows.h>
class Figure{
public:
	virtual void show(HDC) = 0;
};
class Location{
public:
	Location(int x, int y);
	int get_x();
	int get_y();
protected:
	int x_pos, y_pos;
};
class Point: public Location, public Figure{
public:
	Point(int x, int y);
	bool is_visible();
	void show(HDC hdc);
	void hide(HDC hdc);
	void move_to(HDC hdc,int x, int y);
protected:
	bool visible;
};
class Circle: public Point{
public:
	Circle(int x, int y, int r);
	void show(HDC hdc);
	void hide(HDC hdc);
	void move_to(HDC hdc,int x, int y);
protected:
	int radius;
};
class Rect:public Figure{
private:
	int lx, ly, rx, ry;
public:
	Rect(int lx, int ly, int rx, int ry);
	void show(HDC);
};
class Tria:public Figure{
private:
	int lx, ly, rx, ry,tx,ty;
public:
	Tria(int lx, int ly, int rx, int ry,int tx, int ty);
	void show(HDC);
};

Location.cpp


#include "Figure.h"

Location::Location(int x, int y)
{
	x_pos = x;
	y_pos = y;
}

int Location::get_x()
{
	return x_pos;
}

int Location::get_y()
{
	return y_pos;
}

 Point.cpp

#include "Figure.h"
#include"Graphics.h"
#include<iostream>
using namespace std;

Point::Point(int x, int y): Location(x, y)
{
	visible = false;							// 缺省情况下点是不可见的
}

bool Point::is_visible()
{
	return visible;
}
void Point::show(HDC hdc)
{
	if (! is_visible()) {
		visible = true;
		HBRUSH hBrush = CreateSolidBrush(RGB(200, 256,256));	
		point(hdc,hBrush,x_pos,y_pos);
	}
}
void Point::hide(HDC hdc)
{
	if (is_visible()) {
		visible = false;
		HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));	
		point(hdc,hBrush,x_pos,y_pos);	
	}
}
void Point::move_to(HDC hdc,int x, int y)
{
	hide(hdc);									
	x_pos = x;								
	y_pos = y;
	show(hdc);									
}

Circle.cpp

#include "Figure.h"
#include"Graphics.h"
#include<iostream>
#include<Windows.h>
using namespace std;
Circle::Circle(int x, int y, int r): Point(x, y)
{
	radius = r;
}
void Circle::show(HDC hdc)
{
	if (! is_visible()) {
		visible =true;
		HPEN hpen = CreatePen(0, 5, RGB(200, 256,256));
		circle(hdc,hpen,x_pos,y_pos,radius);
	}
}
void Circle::hide(HDC hdc)
{
	if (is_visible()) {
		visible=false;
		HPEN hpen = CreatePen(0, 5, RGB(0, 0, 0));
		circle(hdc,hpen,x_pos,y_pos,radius);			
	}
}
void Circle::move_to(HDC hdc,int x, int y)
{
	hide(hdc);								
	x_pos = x;							
	y_pos = y;
	show(hdc);								
}

Rect.cpp

#include"Figure.h"
#include"Graphics.h"
Rect::Rect(int lx,int ly, int rx, int ry):lx(lx),ly(ly),rx(rx),ry(ry){
}
void Rect::show(HDC hdc){
	HPEN hpen = CreatePen(0, 5, RGB(200, 256,256));
	HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));	
	(HPEN)SelectObject(hdc,hBrush);
	rect(hdc,hpen,lx, ly, rx, ry);
}

Tria.cpp

#include"Figure.h"
#include"Graphics.h"
Tria::Tria(int lx,int ly, int rx, int ry,int tx, int ty):lx(lx),ly(ly),rx(rx),ry(ry),tx(tx),ty(ty){
}
void Tria::show(HDC hdc){
	HPEN hpen = CreatePen(0, 5, RGB(200, 256,256));
	HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));
	(HPEN)SelectObject(hdc,hBrush);
	line(hdc,hpen,lx, ly, rx, ry);
	line(hdc,hpen,lx, ly, tx, ty);
	line(hdc,hpen,tx, ty, rx, ry);
}

 

Gramdemo.cpp

#include "figure.h"
#include"Graphics.h"
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
	HWND hwnd = getConsoleHwnd();
	HDC hdc = GetDC(hwnd);
//	Circle circle(100, 200, 100);
//	circle.show(hdc);
//	getch();
//	circle.move_to(hdc,200, 250);
//	getch();
	//Rect rect(100,100,400,500);
	//rect.show(hdc);
	//getch();
    Circle circle1(200, 400, 50);
    Circle circle2(500, 400, 50);
    Rect rect1(125,250,600,350);
    Tria tria1(125,250,300,250,125,100);
	Figure *shape[4]={&circle1,&circle2,&rect1,&tria1};
    for(int i=0;i<4;i++)
 	   shape[i]->show(hdc);
	ReleaseDC(hwnd,hdc);
	return 0;
}