C/C++控制台应用程序——画三角形、圆、直线、矩形
生活随笔
收集整理的這篇文章主要介紹了
C/C++控制台应用程序——画三角形、圆、直线、矩形
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?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; }?
總結
以上是生活随笔為你收集整理的C/C++控制台应用程序——画三角形、圆、直线、矩形的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “美登杯”上海市高校大学生程序设计邀请赛
- 下一篇: CCPC2019-湖南全国邀请赛(湘潭大