关于FloodFill()的一个问题

来源:百度知道 编辑:UC知道 时间:2024/06/28 02:37:47
我想完成如下一个功能:
用鼠标点击屏幕的某一个区域(比如说一个圆形区域或者一个矩形区域或者其他封闭区域),然后该封闭区域被我所用的颜色所填充.
我用了函数BOOL FloodFill( int x, int y, COLORREF crColor )其中第三个参数就是碰到什么颜色就终止填充,即封闭区域的边缘颜色.
此时遇到一个问题,就是如何实现让点击到任何封闭区域我都能获得其第三个参数呢?
比如说有一个紫色边缘的圆形区域,我点击的时候如何知道第三个参数就是紫色呢?如何进行这个参数的捕获和获取?
我想到的办法是先用GetPixel(CPoint point)获得当前点击点的颜色,然后沿着所点击点的坐标开始逐渐扩大一个矩形区域,每扩大一次都检测此矩形区域的边沿,看其颜色是不是和点击点的颜色相同,如果相同,则继续扩展,如果不相通,则把其作为FloodFill的第三个参数.
代码如下

bool reflesh=false;
COLORREF pt_Color,temp_Color;
CPoint temp_Pt;
CRect temp_Rect=CRect(point.x-1,point.y-1,point.x+1,point.y+1);
temp_Pt=point.x-1;
CClientDC dc(this);
pt_Color=dc.GetPixel(point);
temp_Color=pt_Color;
while(pt_Color==temp_Color)
{
if(temp_Pt.y==temp_Rect.top&&temp_Pt.x<temp_Rect.right&&reflesh==false)
{
temp_Color=dc.GetPixel(temp_Pt);
temp_Pt.x++;
}
if(temp_Pt.x==temp_Rect.right&&temp_Pt.y<temp_Rect.bottom)
{

1 边界寻找你可以划线,会快很多

2 你用ExtFloodFill即可。

BOOL ExtFloodFill(
HDC hdc, // handle to DC
int nXStart, // starting x-coordinate
int nYStart, // starting y-coordinate
COLORREF crColor, // fill color
UINT fuFillType // fill type
);
fuFillType 选这个

FLOODFILLSURFACE The fill area is defined by the color that is specified by crColor. Filling continues outward in all directions as long as the color is encountered. This style is useful for filling areas with multicolored boundaries.

可以指定你需要覆盖的颜色,而不是边界的颜色,你只要得到鼠标点击那点的颜色即可。

接分 ……