欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 编程语言 > c/c >内容正文

c/c

c/c 控制台应用程序——画三角形、圆、直线、矩形 -凯发ag旗舰厅登录网址下载

发布时间:2024/10/5 c/c 20 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 c/c 控制台应用程序——画三角形、圆、直线、矩形 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

 graphics.h

#pragma once#include /*函数功能:获得控制台窗口句柄*/ 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 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 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 #include 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 #include 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; }

 

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的c/c 控制台应用程序——画三角形、圆、直线、矩形的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

网站地图