visual studio下实现图形函数调用

来源:互联网 发布:cf手游刷枪软件绿色版 编辑:程序博客网 时间:2024/05/17 04:41

实现功能

1.实现点,线,矩形,三角形,圆形画法;
2.可以对图形的形状进行调整,主要通过其在运行窗口上的坐标来实现;
3.纯虚函数进行实现不同图形的同一接口;

代码实现

graphics.h

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

graphics.cpp

//#define DLL_IMPLEMENT#include"Graphics.h"#define MY_BUFSIZE 100HWND 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);}

demo.cpp

#include <iostream>#include <conio.h>#include "Graphics.h"using namespace std;class shape{public:    virtual void draw(HDC hdc) = 0;    virtual void resize() = 0;};class Point : public shape{public:    void draw(HDC hdc);    void resize() {};};class Line : public shape{private:    int ex, ey;public:    Line(int a, int b) { ex = a; ey = b; }    void draw(HDC hdc);    void resize();};class Circle : public shape{public :    Circle(int r) { radius = r; }    void draw(HDC hdc);    void resize();private:    int radius;};class Rectangl : public shape{private:    int right, bottom;public:    Rectangl(int r, int b) { right = r; bottom = b; }    void draw(HDC hdc);    void resize();};class Triangel : public shape{private:    int x_mid, y_mid;    int x_end, y_end;public:    Triangel(int xm, int ym, int xe, int ye) { x_mid = xm; y_mid = ym; x_end = xe; y_end = ye; }    void draw(HDC hdc);    void resize();};void Circle::draw(HDC hdc){    HPEN hpen = CreatePen(0, 5, RGB(200, 256, 256));    circle(hdc, hpen, 0, 200, radius);}void Circle::resize(){    cout << "input radius:";    cin >> radius;}void Point::draw(HDC hdc){    HBRUSH hBrush = CreateSolidBrush(RGB(200, 256, 256));    point(hdc, hBrush, 200, 200);}void Line::draw(HDC hdc){    HPEN hpen = CreatePen(0, 5, RGB(200, 256, 256));    line(hdc, hpen, 400, 200, ex, ey);}void Line::resize(){    cout << "input x_end:";    cin >> ex;    cout << "input y_end:";    cin >> ey;}void Rectangl::draw(HDC hdc){    HPEN hpen = CreatePen(0, 5, RGB(200, 256, 256));    rect(hdc, hpen, 600, 200, right, bottom);}void Rectangl::resize(){    cout << "input right:";    cin >> right;    cout << "input bottom:";    cin >> bottom;}void Triangel::draw(HDC hdc){    HPEN hpen = CreatePen(0, 5, RGB(200, 256, 256));    line(hdc, hpen, 800, 200, x_mid, y_mid);    line(hdc, hpen, x_mid, y_mid, x_end, y_end);    line(hdc, hpen, 800, 200, x_end, y_end);}void Triangel::resize(){    cout << "input x_mid:";    cin >> x_mid;    cout << "input y_mid";    cin >> y_mid;    cout << "input x_end";    cin >> x_end;    cout << "input y_end";    cin >> y_end;}

main.cpp

int main(){    HWND hwnd = getConsoleHwnd();    HDC hdc = GetDC(hwnd);   //获取窗口句柄    Point  point;    Line line(300, 300);    Triangel triangel(200, 300, 300, 300);    Circle circle(50);    Rectangl rectangle(400, 400);    shape *shape[5];    shape[0] = &point;    shape[1] = &circle;    shape[2] = &line;    shape[3] = &rectangle;    shape[4] = &triangel;    for (int i = 0; i < 5; i++)    {        shape[i]->draw(hdc);        shape[i]->resize();        shape[i]->draw(hdc);    }    _getch();    return 0;}

注:

本程序是调用visual studio下的图形库进行绘图。

0 0