2,643
社区成员
发帖
与我相关
我的任务
分享
class CDrawView:CView
{
CDC* pdc;
CDC memDC;
CBitmap memBmp;
BOOL OnInitialUpdate( )
{
pdc=this->GetDC();
GetClientRect(&rect);
//建立与屏幕显示兼容的内存显示设备
memDC.CreateCompatibleDC(pdc);
//建立一个与屏幕显示兼容的位图
memBmp.CreateCompatibleBitmap(pdc, rect.Width(), rect.Height());
//将位图选入到内存显示设备中
memDC.SelectObject(pBmp);
}
void Draw( )
{
memDC.FillSolidRect(rect,RGB(255,255,255));
Graphics graphic( memDC.m_hDC ); //声明一个Graphics
m_data->Get(&DrawCount,&Angle);
m_data->GetIncludedAngle(&IncludedAngle);
//定义指向类成员函数的指针,并使它指向类的43个绘图函数
void (CDrawView::*p[100])()={&CDrawView::Draw1,&CDrawView::Draw2,&CDrawView::Draw3,&CDrawView::Draw4,
&CDrawView::Draw5,&CDrawView::Draw6,&CDrawView::Draw7,&CDrawView::Draw8,
&CDrawView::Draw9, &CDrawView::Draw10,&CDrawView::Draw11,&CDrawView::Draw12,
&CDrawView::Draw13,&CDrawView::Draw14,&CDrawView::Draw15,&CDrawView::Draw16,
&CDrawView::Draw17,&CDrawView::Draw18,&CDrawView::Draw19,&CDrawView::Draw20,
&CDrawView::Draw21,&CDrawView::Draw22,&CDrawView::Draw23,&CDrawView::Draw24,
&CDrawView::Draw25,&CDrawView::Draw26,&CDrawView::Draw27,&CDrawView::Draw28,
&CDrawView::Draw29,&CDrawView::Draw30,&CDrawView::Draw31,&CDrawView::Draw32,
&CDrawView::Draw33,&CDrawView::Draw34,&CDrawView::Draw35,&CDrawView::Draw36,
&CDrawView::Draw37,&CDrawView::Draw38,&CDrawView::Draw39,&CDrawView::Draw40,
&CDrawView::Draw41,&CDrawView::Draw42,&CDrawView::Draw43,&CDrawView::Draw44,
&CDrawView::Draw45,&CDrawView::Draw46};
//定义for循环,用来判断当前用户输入的是第几个图形,并且调用相应的成员函数将图形绘制出来
for(int i=0;i<100;i++)
{
if((i+1)==DrawCount)
{
(this->*p[i])( &graphic); //输入Graphic指针
}
}
//将内存中的图拷贝到屏幕上进行显示
pdc->BitBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0,SRCCOPY);
}
void OnDraw( CDC* pDC ) //屏幕刷新时调用防止窗口被覆盖等情况时屏幕不刷新
{
Draw( );
}
}
void CDrawView::RotatePolygon(Graphics* graphic,int RotateAngle,PointF *Point,int PointCount,PointF Center,Pen *pen)
{
//定义旋转后的PointF数组
PointF *PointChange;
PointChange=new PointF[PointCount];
//对坐标数组进行旋转变换,并将新的坐标赋给PointChange数组
for(int i=0;i<PointCount;i++)
{
PointChange[i]=RotatePoint(RotateAngle,Point[i],Center);
}
//对旋转后的坐标进行坐标变换
for(int i=0;i<PointCount;i++)
{
PointChange[i]=ConvertCoodinate(PointChange[i]);
}
//将图像画到内存
//消除锯齿
graphic->SetSmoothingMode(SmoothingModeAntiAlias);
//绘制多边形
graphic->DrawPolygon(pen,PointChange,PointCount);
//删除指针
delete PointChange;
}
void CDrawView::Draw()
{
CDC *pDC=this->GetDC();
GetClientRect(&rect);
//内存DC的初始化
pMemDC=new CDC;
//位图对象的初始化
pBmp=new CBitmap;
//建立与屏幕显示兼容的内存显示设备
pMemDC->CreateCompatibleDC(pDC);
//建立一个与屏幕显示兼容的位图
pBmp->CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
//将位图选入到内存显示设备中
pMemDC->SelectObject(pBmp);
//先用背景色将位图清除干净
pMemDC->FillSolidRect(rect,RGB(255,255,255));
m_data->Get(&DrawCount,&Angle);
m_data->GetIncludedAngle(&IncludedAngle);
//定义指向类成员函数的指针,并使它指向类的43个绘图函数
void (CDrawView::*p[100])()={&CDrawView::Draw1,&CDrawView::Draw2,&CDrawView::Draw3,&CDrawView::Draw4,
&CDrawView::Draw5,&CDrawView::Draw6,&CDrawView::Draw7,&CDrawView::Draw8,
&CDrawView::Draw9, &CDrawView::Draw10,&CDrawView::Draw11,&CDrawView::Draw12,
&CDrawView::Draw13,&CDrawView::Draw14,&CDrawView::Draw15,&CDrawView::Draw16,
&CDrawView::Draw17,&CDrawView::Draw18,&CDrawView::Draw19,&CDrawView::Draw20,
&CDrawView::Draw21,&CDrawView::Draw22,&CDrawView::Draw23,&CDrawView::Draw24,
&CDrawView::Draw25,&CDrawView::Draw26,&CDrawView::Draw27,&CDrawView::Draw28,
&CDrawView::Draw29,&CDrawView::Draw30,&CDrawView::Draw31,&CDrawView::Draw32,
&CDrawView::Draw33,&CDrawView::Draw34,&CDrawView::Draw35,&CDrawView::Draw36,
&CDrawView::Draw37,&CDrawView::Draw38,&CDrawView::Draw39,&CDrawView::Draw40,
&CDrawView::Draw41,&CDrawView::Draw42,&CDrawView::Draw43,&CDrawView::Draw44,
&CDrawView::Draw45,&CDrawView::Draw46};
//定义for循环,用来判断当前用户输入的是第几个图形,并且调用相应的成员函数将图形绘制出来
for(int i=0;i<100;i++)
{
if((i+1)==DrawCount)
{
(this->*p[i])();
}
}
//将内存中的图拷贝到屏幕上进行显示
pDC->BitBlt(0,0,rect.Width(),rect.Height(),pMemDC,0,0,SRCCOPY);
}
代码经过上面这样的改动以后,你后续的Draw1~Draw46等就都需要改动了,改动很简单:
Draw1等类型函数都改成下面的结构:
void CDrawView::Draw1( Graphics* graphic ) //增加输入指针
{
Pen ShipPen(Color(0,0,0),3);
Pen LinePen(Color(200,200,200),1);
//绘制船
DrawShip( graphic, &ShipPen,&LinePen,&LinePen ); //使用Graphic指针绘图
}
Draw函数变成:
void CDrawView::Draw()
{
CDC *pDC=this->GetDC();
GetClientRect(&rect);
//内存DC的初始化
pMemDC=new CDC;
//位图对象的初始化
pBmp=new CBitmap;
//建立与屏幕显示兼容的内存显示设备
pMemDC->CreateCompatibleDC(pDC);
//建立一个与屏幕显示兼容的位图
pBmp->CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
//将位图选入到内存显示设备中
pMemDC->SelectObject(pBmp);
//先用背景色将位图清除干净
pMemDC->FillSolidRect(rect,RGB(255,255,255));
Graphics graphic( pMemeDC->m_hDC ); //声明一个Graphics
m_data->Get(&DrawCount,&Angle);
m_data->GetIncludedAngle(&IncludedAngle);
//定义指向类成员函数的指针,并使它指向类的43个绘图函数
void (CDrawView::*p[100])()={&CDrawView::Draw1,&CDrawView::Draw2,&CDrawView::Draw3,&CDrawView::Draw4,
&CDrawView::Draw5,&CDrawView::Draw6,&CDrawView::Draw7,&CDrawView::Draw8,
&CDrawView::Draw9, &CDrawView::Draw10,&CDrawView::Draw11,&CDrawView::Draw12,
&CDrawView::Draw13,&CDrawView::Draw14,&CDrawView::Draw15,&CDrawView::Draw16,
&CDrawView::Draw17,&CDrawView::Draw18,&CDrawView::Draw19,&CDrawView::Draw20,
&CDrawView::Draw21,&CDrawView::Draw22,&CDrawView::Draw23,&CDrawView::Draw24,
&CDrawView::Draw25,&CDrawView::Draw26,&CDrawView::Draw27,&CDrawView::Draw28,
&CDrawView::Draw29,&CDrawView::Draw30,&CDrawView::Draw31,&CDrawView::Draw32,
&CDrawView::Draw33,&CDrawView::Draw34,&CDrawView::Draw35,&CDrawView::Draw36,
&CDrawView::Draw37,&CDrawView::Draw38,&CDrawView::Draw39,&CDrawView::Draw40,
&CDrawView::Draw41,&CDrawView::Draw42,&CDrawView::Draw43,&CDrawView::Draw44,
&CDrawView::Draw45,&CDrawView::Draw46};
//定义for循环,用来判断当前用户输入的是第几个图形,并且调用相应的成员函数将图形绘制出来
for(int i=0;i<100;i++)
{
if((i+1)==DrawCount)
{
(this->*p[i])( &graphic); //输入Graphic指针
}
}
//将内存中的图拷贝到屏幕上进行显示
pDC->BitBlt(0,0,rect.Width(),rect.Height(),pMemDC,0,0,SRCCOPY);
}