本文最后更新于2020年10月29日,已超过 4 个月没更新!
void CCGPainterView::ScanLineFill(CDC* pDC, CPoint startPoint, COLORREF fillCol, COLORREF boundaryCol)
{
    //Write your boundary fill algorithm here.
    //定义堆栈
    CArray<CPoint, CPoint>Stack;
    CPoint fillPoint = startPoint;
    COLORREF currentCol = pDC->GetPixel(startPoint);
    //最左点,最右点
    int xl, xr;
    //标志位
    bool spanNeedFill;
    //种子点
    CPoint pt;
    Stack.RemoveAll();//设置堆栈为空
    pt.x = fillPoint.x;
    pt.y = fillPoint.y;
    Stack.Add(pt);//种子点压入堆栈
    while (Stack.GetSize() != 0)
    {
        pt = Stack[Stack.GetSize() - 1];//取出种子点
        Stack.RemoveAt(Stack.GetSize() - 1);//堆栈减短
        fillPoint.y = pt.y;
        fillPoint.x = pt.x;
        //从种子开始向右填充
        while (pDC->GetPixel(fillPoint.x, fillPoint.y) == currentCol)
        {
            pDC->SetPixelV(fillPoint, fillCol);
            fillPoint.x++;
        }
        xr = fillPoint.x - 1;
        fillPoint.x = pt.x - 1;
        //从种子开始向左填充
        while (pDC->GetPixel(fillPoint.x, fillPoint.y) == currentCol)
        {
            pDC->SetPixelV(fillPoint, fillCol);
            fillPoint.x--;
        }
        xl = fillPoint.x + 1;
        //处理上一条扫描线和下一条扫描线
        for (int I = 0; I < 2; I++)
        {
            fillPoint.x = xl;
            if (I == 0) fillPoint.y = fillPoint.y + 1;
            else fillPoint.y = fillPoint.y - 2;

            while (fillPoint.x < xr)
            {
                spanNeedFill = FALSE;
                while (pDC->GetPixel(fillPoint.x, fillPoint.y) == currentCol)
                {
                    spanNeedFill = TRUE;
                    fillPoint.x++;
                }//待填充区搜索完毕
                if (spanNeedFill)
                {//右端点作为种子入栈
                    pt.x = fillPoint.x - 1;
                    pt.y = fillPoint.y;
                    Stack.Add(pt);
                    spanNeedFill = FALSE;
                }
                //继续向右检查以防遗漏
                while (pDC->GetPixel(fillPoint.x, fillPoint.y) != currentCol && fillPoint.x < xr)
                    fillPoint.x++;
            }//上一条扫描线上检查完毕
        }
    }
}

疏影横斜水清浅,暗香浮动月黄昏