多边形填充实验
实验目标:
1.窗口对用户"左按键"和"右按键"事件的响应
2.实现多边形填充
步骤:
1.运行VC6.0
2.创建工程Fill , (工程类型选择:MFC Wizard (.exe) )
3. 添加 "双击左键"和"单击右键"事件的消息响应
(1).选择菜单"View"->"class Wizard"
(2)在弹出的窗口里
classname: 选择CFillView
Messages: 分别选择 WM_LBUTTONDBLCLK , WM_RBUTTONDOWN
然后Add Function
4.修改代码:
(1) 修改fillview.h文件 :
在类的定义中,加入如下代码:
public:
CPoint spt[7];
void Floodfill(CPoint s_point, int f_color, int b_color) ;
(2)修改fillview.cpp文件
修改OnDraw,OnLButtonDblClk,OnRButtonDown的三个函数
加入一个函数: Floodfill
void CFillView::OnDraw(CDC* pDC)
{
CFillDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPen newpen(PS_SOLID,1,RGB(255,0,0));
CPen *old=pDC->SelectObject(&newpen);
pDC->TextOut(20,20,"双击鼠标左键, 出现需填充的多边形");
pDC->TextOut(20,50,"进行填充, 需用鼠标右键, 单击多边形内一点, 作为开始填充的种子点");
pDC->SelectObject(old);
}
(2)void CFillView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
RedrawWindow();
CDC* pDC=GetDC();
CPen newpen(PS_SOLID,1,RGB(255,0,0));
CPen *old=pDC->SelectObject(&newpen);
//多边形各顶点的坐标
spt[0]=CPoint(100,100);
spt[1]=CPoint(300,100);
spt[2]=CPoint(250,250);
spt[3]=CPoint(100,250);
spt[4]=CPoint(150,200);
spt[5]=CPoint(90,180);
spt[6]=CPoint(150,150);
spt[7]=CPoint(100,100);
pDC->Polyline(spt,8); //画多边形
1.窗口对用户"左按键"和"右按键"事件的响应
2.实现多边形填充
步骤:
1.运行VC6.0
2.创建工程Fill , (工程类型选择:MFC Wizard (.exe) )
3. 添加 "双击左键"和"单击右键"事件的消息响应
(1).选择菜单"View"->"class Wizard"
(2)在弹出的窗口里
classname: 选择CFillView
Messages: 分别选择 WM_LBUTTONDBLCLK , WM_RBUTTONDOWN
然后Add Function
4.修改代码:
(1) 修改fillview.h文件 :
在类的定义中,加入如下代码:
public:
CPoint spt[7];
void Floodfill(CPoint s_point, int f_color, int b_color) ;
(2)修改fillview.cpp文件
修改OnDraw,OnLButtonDblClk,OnRButtonDown的三个函数
加入一个函数: Floodfill
void CFillView::OnDraw(CDC* pDC)
{
CFillDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPen newpen(PS_SOLID,1,RGB(255,0,0));
CPen *old=pDC->SelectObject(&newpen);
pDC->TextOut(20,20,"双击鼠标左键, 出现需填充的多边形");
pDC->TextOut(20,50,"进行填充, 需用鼠标右键, 单击多边形内一点, 作为开始填充的种子点");
pDC->SelectObject(old);
}
(2)void CFillView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
RedrawWindow();
CDC* pDC=GetDC();
CPen newpen(PS_SOLID,1,RGB(255,0,0));
CPen *old=pDC->SelectObject(&newpen);
//多边形各顶点的坐标
spt[0]=CPoint(100,100);
spt[1]=CPoint(300,100);
spt[2]=CPoint(250,250);
spt[3]=CPoint(100,250);
spt[4]=CPoint(150,200);
spt[5]=CPoint(90,180);
spt[6]=CPoint(150,150);
spt[7]=CPoint(100,100);
pDC->Polyline(spt,8); //画多边形
pDC->SelectObject(old);
ReleaseDC(pDC);
CView::OnLButtonDblClk(nFlags, point);
}
void CFillView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnRButtonDown(nFlags, point);
int fill=RGB(0,255,0);
int boundary=RGB(255,0,0);
Floodfill(point, fill, boundary);
ReleaseDC(pDC);
CView::OnLButtonDblClk(nFlags, point);
}
void CFillView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnRButtonDown(nFlags, point);
int fill=RGB(0,255,0);
int boundary=RGB(255,0,0);
Floodfill(point, fill, boundary);
}
/*
s_point 为种子点
f_color是要填充的颜色, b_color是边界的颜色
*/
void CFillView::Floodfill(CPoint s_point, int f_color, int b_color)
{
CWindowDC dc (this);
int x,y,p0,pmin,pmax;
//求多边形的最大最小值
pmin=pmax=spt[0].y;
for(int i=1;i<8;i++)
{
if(spt[i].y<pmin) pmin=spt[i].y;
if(spt[i].y>pmax) pmax=spt[i].y;
}
x=s_point.x;
y=s_point.y;
for(;y<pmax;y++)
{
int current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x++;
current=dc.GetPixel(x,y);
}
x=s_point.x;
x--;
current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x--;
current=dc.GetPixel(x,y);
}
x=s_point.x;
}
x=s_point.x;
y=s_point.y-1;
/*
s_point 为种子点
f_color是要填充的颜色, b_color是边界的颜色
*/
void CFillView::Floodfill(CPoint s_point, int f_color, int b_color)
{
CWindowDC dc (this);
int x,y,p0,pmin,pmax;
//求多边形的最大最小值
pmin=pmax=spt[0].y;
for(int i=1;i<8;i++)
{
if(spt[i].y<pmin) pmin=spt[i].y;
if(spt[i].y>pmax) pmax=spt[i].y;
}
x=s_point.x;
y=s_point.y;
for(;y<pmax;y++)
{
int current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x++;
current=dc.GetPixel(x,y);
}
x=s_point.x;
x--;
current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x--;
current=dc.GetPixel(x,y);
}
x=s_point.x;
}
x=s_point.x;
y=s_point.y-1;
for(;y>pmin+2;y--)
{
int current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x++;
current=dc.GetPixel(x,y);
}
x=s_point.x;
x--;
current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x--;
current=dc.GetPixel(x,y);
}
x=s_point.x;
}
}
{
int current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x++;
current=dc.GetPixel(x,y);
}
x=s_point.x;
x--;
current=dc.GetPixel(x,y);
while((current!=b_color)&&(current!=f_color))
{
dc.SetPixel(x,y,f_color);
x--;
current=dc.GetPixel(x,y);
}
x=s_point.x;
}
}