使用API绘制轮船,并实现按下键盘上的W,A,S,D键进行上下左右移动,按下+,-键实现放大和缩小功能

使用API绘制轮船,并实现按下键盘上的W,A,S,D键进行上下左右移动,按下+,-键实现放大和缩小功能

代码中的坐标为提前在草纸上设计好的,如果想要设计其它图形,可计算各点坐标,用图形拼凑自己想要的图形。
使用API绘制轮船,并实现按下键盘上的W,A,S,D键进行上下左右移动,按下+,-键实现放大和缩小功能_第1张图片



class CGraph {                  //创建CGraph类

public:

    CGraph() {}

    virtual void draw(HDC hdc){};

private:

    

};

class CLine :public CGraph      //创建CLine类,继承CGraph

{

public:

    CLine() {}

    //画一条线,只需给出起点和终点的坐标

    void DrawLine(HDC hdc, int start_x, int start_y, int end_x, int end_y) {

       MoveToEx(hdc, start_x, start_y, NULL);

       LineTo(hdc, end_x, end_y);

    }

    void draw(HDC hdc) {

DrawLine(hdc, 325, 490, 325, 740);//锚定

       Arc(hdc, 275, 640, 375, 740, 275, 710,
375, 710);

       DrawLine(hdc, 310, 670, 340, 670); 

}

};

class CRetangle :public CGraph     //创建CRetangle类,继承CGraph

{

public:

    CRetangle(){}

//画矩形,只需给出矩形的左上角坐标和右下角坐标,可以选择画笔

    void DrawRectangle(HDC hdc,int s_x,int s_y,int e_x,int e_y,HPEN hpen) {

       HPEN hp = hpen;

       SelectObject(hdc,
hp);

       Rectangle(hdc,s_x,s_y,e_x,e_y);

    }

void draw(HDC
hdc) {

       HPEN hPen;

       hPen = CreatePen(PS_SOLID, 1, RGB(0, 0,
0));//创建一个黑色画笔

       DrawRectangle(hdc, 450, 270, 950, 390,
hPen);//上层1

       DrawRectangle(hdc, 500, 200, 900, 270,
hPen);//上层2

    }

};

class CCircle :public CGraph       //创建CCircle类,继承CGraph

{

public:

    CCircle(){}

    //画圆,只需给出输入圆心坐标和半径即可

    void DrawCircle(HDC hdc,int x,int y,int r) {

       int s_x, s_y, e_x, e_y;

       s_x = x - r;

       s_y= y - r;

       e_x= x + r;

       e_y= y + r;

       Ellipse(hdc,
s_x, s_y, e_x, e_y);

    }

void
draw(HDC hdc) {

       DrawCircle(hdc,
1100, 390, 75);//炮台

    }

};

 

//画图,设计缩小,放大,移动的响应事件

int i = 0;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

    HBRUSH hBrush;

    HPEN hPen;

    PAINTSTRUCT ps;

    HDC hdc;

    CLine cl;

    CRetangle cr;

    CCircle cc;

    CGraph *cg1 = &cl;

    CGraph *cg2 = &cr;

    CGraph *cg3 = &cc;

    int x, y;

    switch (message)

    {  

    case WM_PAINT:

        {

       PAINTSTRUCT ps;

       hdc = BeginPaint(hWnd,
&ps);

       if (i == 0) {        

       }

       //移动

       //按下W向上移动

       else if (i == 1) {

           //xWindow=(xViewport-xViewOrg)*(xWinExt/xViewExt)+xWinOrg

           //yWindow = (yViewport - yViewOrg)*(yWinExt/yViewExt) +
yWinOrg

           //xWindow为点的窗口坐标,xViewport是点的视口坐标,xViewOrg是视口的原点坐标

           //xWinOrg是窗口的原点坐标,xWinExt是窗口横坐标尺寸,xViewExt是视口的横坐标尺寸

           SetMapMode(hdc, MM_ANISOTROPIC);//设置映像模式,窗口可伸缩

           SetWindowOrgEx(hdc, 500, 600, NULL);//设置窗口原点坐标

           SetViewportOrgEx(hdc, 500, 500, NULL);//设置视口的原点坐标(500,500)

           SetWindowExtEx(hdc, 500, 500, NULL);//设置窗口的尺寸

           SetViewportExtEx(hdc, 500, 500, NULL);//设置视口的尺寸  

}

       //按下A向左移动

       else if (i == 2) {

           SetMapMode(hdc, MM_ANISOTROPIC);//设置映像模式,窗口可伸缩

           SetWindowOrgEx(hdc, 700, 500, NULL);

           SetViewportOrgEx(hdc, 500, 500, NULL);//把窗口的零点映像为视口的(100,100)

           SetWindowExtEx(hdc, 500, 500, NULL);

           SetViewportExtEx(hdc, 500, 500, NULL);

       }

       //按下S向下移动

       else if (i == 3) {

           SetMapMode(hdc, MM_ANISOTROPIC);//设置映像模式,窗口可伸缩

           SetWindowOrgEx(hdc, 500, 400, NULL);

           SetViewportOrgEx(hdc, 500, 500, NULL);//把窗口的零点映像为视口的(100,100)

           SetWindowExtEx(hdc, 500, 500, NULL);

           SetViewportExtEx(hdc, 500, 500, NULL);

       }

       //按下D向右移动

       else if (i == 4) {

           SetMapMode(hdc, MM_ANISOTROPIC);//设置映像模式,窗口可伸缩

           SetWindowOrgEx(hdc, 200, 500, NULL);

           SetViewportOrgEx(hdc, 500, 500, NULL);//把窗口的零点映像为视口的(100,100)

           SetWindowExtEx(hdc, 500, 500, NULL);

           SetViewportExtEx(hdc, 500, 500, NULL);

       }

       //按下加号轮船扩大

       else if (i == 5) {          

           SetMapMode(hdc, MM_ANISOTROPIC);//设置映像模式,窗口可伸缩

           SetViewportOrgEx(hdc, 0, 0, NULL);//把窗口的零点映像为视口的(0,0)

           SetWindowExtEx(hdc, 500, 500, NULL);

           SetViewportExtEx(hdc, 600, 600, NULL);                                              

       }

       //按下减号轮船缩小

       else if (i == 6) {   

       SetMapMode(hdc, MM_ANISOTROPIC);//设置映像模式,窗口可伸缩

       SetViewportOrgEx(hdc,0,0, NULL);//把窗口的零点映像为视口的(0,0)

       SetWindowExtEx(hdc, 500, 500, NULL);

       SetViewportExtEx(hdc, 300, 300, NULL);    

       }      

        }

       // TODO: 在此处添加使用 hdc 的任何绘图代码... 

       cg1->draw(hdc);//使用CGraph类指针来调用子类中的draw函数

 

        hPen
= CreatePen(PS_SOLID, 1, RGB(0,
0, 0));//创建一个黑色画笔

        hBrush
= CreateSolidBrush(RGB(220, 220, 220));

        SelectObject(hdc,
hBrush);

        cg2->draw(hdc); //使用CGraph类指针来调用子类中的draw函数

 

 

        hPen
= CreatePen(PS_SOLID, 1, RGB(30,
144, 255));//创建一个蓝色画笔

        hBrush
= CreateSolidBrush(RGB(0, 255, 255));

        SelectObject(hdc,
hBrush);

 

         cg3->draw(hdc); //使用CGraph类指针来调用子类中的draw函数

 

       cl.DrawLine(hdc, 300, 340, 1200, 340);//栏杆

       x = 300; y = 340;

       for (int i = 0; i < 19; i++) {

           cl.DrawLine(hdc, x, y, x, y + 50);

           x += 50;

       }

 

       hBrush = CreateSolidBrush(RGB(30, 144, 255));//创建一个浅蓝色的画刷

       SelectObject(hdc, hBrush);

 

       cr.DrawRectangle(hdc, 400, 390, 1000,
640, hPen);//主体                   

       Pie(hdc, 800, 190, 1200, 590, 1000, 590,
1200, 390);//前半截扇形                              

       cr.DrawRectangle(hdc, 350, 390, 400, 540,
hPen);//后尾1

       Pie(hdc, 350, 490, 450, 590, 350, 540,
400, 590);//后尾1弧线

       cr.DrawRectangle(hdc, 300, 390, 350, 490,
hPen);//后尾2

 

       hBrush = CreateSolidBrush(RGB(248, 248, 255));//创建一个浅白色的画刷

       SelectObject(hdc, hBrush);

 

       x = 500; y = 440;//船舷窗

       for (int i = 0; i < 4; i++) {

           cc.DrawCircle(hdc, x, y, 25);

           x += 150;

       }

       x = 550; y = 290;//船窗口          

       for (int i = 0; i < 3; i++) {

           cr.DrawRectangle(hdc, x, y, x + 30, y
+ 30, hPen);

           x += 150;

       }

       x = 600; y = 220;

       for (int i = 0; i < 2; i++) {

           cr.DrawRectangle(hdc, x, y, x + 30, y
+ 30, hPen);

           x += 150;

       }

 

       hBrush = CreateSolidBrush(RGB(255, 0, 0));//创建一个红色画刷

       SelectObject(hdc, hBrush);

 

       cr.DrawRectangle(hdc, 400, 590, 1000,
640, hPen);//画底部       

       Pie(hdc, 975, 590, 1025, 640, 1000, 640,
1000, 590);

       cl.DrawLine(hdc, 700, 200, 700, 150);//红旗

       cr.DrawRectangle(hdc, 700, 80, 805, 150,
hPen);

       DeleteObject(hBrush);

       EndPaint(hWnd,
&ps);

        break;

    case WM_DESTROY:

       PostQuitMessage(0);

       break; 

    case WM_KEYUP:

       i = 0;

       InvalidateRect(hWnd, NULL, TRUE);

       break;

    case WM_KEYDOWN:

       switch (wParam) {

           case VK_W:     //按下W键响应

              i = 1;

               InvalidateRect(hWnd, NULL, TRUE);

              break;

           case VK_A:     //按下A键响应

              i = 2;

              InvalidateRect(hWnd, NULL, TRUE);

              break;

           case VK_S:     //按下S键响应

              i = 3;

              InvalidateRect(hWnd, NULL, TRUE);

              break;

           case VK_D:     //按下D键响应

              i = 4;

               InvalidateRect(hWnd, NULL, TRUE);

              break;

           case VK_OEM_PLUS:        //按下加号响应

              i = 5;

              InvalidateRect(hWnd, NULL, TRUE);

              break;

           case VK_OEM_MINUS:          //按下减号响应

              i = 6;

              InvalidateRect(hWnd, NULL, TRUE);

              break;

       }

    

    

    default:

        return DefWindowProc(hWnd, message, wParam, lParam);

    }

    return 0;

}

图中是主要代码,最前面类的创建可以删去。

你可能感兴趣的:(使用API绘制轮船,并实现按下键盘上的W,A,S,D键进行上下左右移动,按下+,-键实现放大和缩小功能)