| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 184 人关注过本帖
标题:c++builder画正方形和圆形
只看楼主 加入收藏
KS010005
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2021-3-28
结帖率:50%
收藏
 问题点数:0 回复次数:3 
c++builder画正方形和圆形
要求:把畫的图形記成不同的class.按一個 redraw 會依記錄畫出不同形狀.要求要用 virtual function.
搜索更多相关主题的帖子: 要求 正方形 c++ 不同 builder 
2021-04-24 17:02
KS010005
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2021-3-28
收藏
得分:0 
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

 TGroupBox *GroupBox1;
        TImage *Image1;
        int shape;
        int posX,posY,X,Y;  //初始及终点坐标
 class type
        {
        public:
            void zuotu1()
            {
            Image1->Canvas->Ellipse(posX,posY,X,Y);
            }
            virtual void zuotu2()
            {
            }
        };
         class cycle:public type
        {
        public:
            void zuotu1()
            {
            }
            void zuotu2()
            {
            Image1->Canvas->Rectangle(posX,posY,X,Y);
            }
        };
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
        shape=1;
        StatusBar1->Panels->Items[0]->Text="绘制正方形";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
        shape=2;
        StatusBar1->Panels->Items[0]->Text="绘制圆形";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, int X, int Y)
{
        posX=X;
        posY=Y;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
       if(abs(X-posX)>abs(Y-posY))
                        Y=X-posX+posY;
                else
                        X=Y-posY+posX;
        type a;
        cycle b;
        switch(shape)
        {
                case 1:
                        a.zuotu1();
                        break;
                case 2:
                        b.zuotu2();
                        break;
                default:
                        break;
}
}
//---------------------------------------------------------------------------
2021-04-24 21:39
KS010005
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2021-3-28
收藏
得分:0 
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

 TGroupBox *GroupBox1;
        TImage *Image1;
        int shape;
        int posX,posY,X,Y;  //初始及终点坐标
 class type
        {
        public:
            void zuotu1()
            {
            Image1->Canvas->Ellipse(posX,posY,X,Y);
            }
            virtual void zuotu2()
            {
            }
        };
         class cycle:public type
        {
        public:
            void zuotu1()
            {
            }
            void zuotu2()
            {
            Image1->Canvas->Rectangle(posX,posY,X,Y);
            }
        };
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
        shape=1;
        StatusBar1->Panels->Items[0]->Text="绘制正方形";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
        shape=2;
        StatusBar1->Panels->Items[0]->Text="绘制圆形";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, int X, int Y)
{
        posX=X;
        posY=Y;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
       if(abs(X-posX)>abs(Y-posY))
                        Y=X-posX+posY;
                else
                        X=Y-posY+posX;
        type a;
        cycle b;
        switch(shape)
        {
                case 1:
                        a.zuotu1();
                        break;
                case 2:
                        b.zuotu2();
                        break;
                default:
                        break;
}
}
2021-04-24 21:40
KS010005
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2021-3-28
收藏
得分:0 
class CShape
{
   protected:
     TCanvas* m_pCanvas;
   public:
    virtual ~CShape(void){};
    virtual void draw( void)= 0;
};// class CShape

class CRectangle: public CShape
{
    int m_x1;
    int m_y1;
    int m_x2;
    int m_y2;
  public:
     CRectangle( TCanvas* pCanvas, int x1, int y1, int x2, int y2);
     virtual void draw( void);
};//C Rectangle


class CCircle: public CShape
{
    int m_x;
    int m_y;
    int m_r;
  public:
    CCircle( TCanvas* pCanvas, int x, int y, int r);
    virtual void draw( void);
};// class CCircle

class TForm1:...
{
   ...
  private:
   void addRect( x1, y1, x2, y2);
   void addCir( x, y, r);
   void redraw( void);
   CShape* m_apShape[100];
   int     m_szShape;
  
}

// in .cpp

CRectangle::CRectangle( TCanvas* pCanvas, int x1, int y1, int x2, int y2)
{
  m_pCanvas= pCanvas;
  m_x1= x1;
  m_y1= y1;
  m_x2= x2;
  m_y2= y2;
}// CRectangle()

void CRectangle::draw( void)
{
  m_pCanvas->Rectangle( m_x1, m_y1, m_x2, m_y2);
}// draw()

CCircle::CCircle( TCanvas* pCanvas, int x, int y, int r)
{
  int x1= m_x- m_r;
  int y1= m_y- m_r;
  int x2= m_x+ m_r;
  int y2= m_y+ m_r;
  m_pCanvas->Ellipse( x1, y1, x2, y2);
}// CCircle

TForm1::TFrom1()
{
  m_szShape= 0;
}//

void TForm1::addRect( x1, y1, x2, y2)
{
  CShape* pShape= new CRectangle( Image1->Canvas, x1, y1, x2, y2);
  m_apShape[ m_szShape]= pShape;
  m_szShape++;
}// addRect

void TForm1::addCir( x, y, r)
{
  CShape* pShape= new CCircle( Image1->Canvas, x, y, r);
  m_apShape[ m_szShape]= pShape;
  m_szShape++;
}// addCir

void TForm1::redraw( void)
{
  for( int ixShape= 0; ixShape< m_szShape; ixShape++)
  {
    m_apShape->draw();
  }// for ixShape
}// redraw()
2021-04-25 20:18
快速回复:c++builder画正方形和圆形
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.123470 second(s), 8 queries.
Copyright©2004-2021, BCCN.NET, All Rights Reserved