在一个图形面板上,有许多的矩形框、线条及园,点击鼠标后,如何填充其周围的一块封闭区域

解决方案 »

  1.   

    几何好就很简单,先算出闭合区域的大小,然后用FillRect来填充
      

  2.   

    先得出Rect 的大小,在用FillRect填充。
      

  3.   

    现在就是计算RECT的问题,写了一个算法,但出现堆栈溢出,代码如下:procedure FillPoly(Canvas :TCanvas;Po :TPoint;FillColor :TColor;OldFillColor :TColor);
    type
      TFillDirection = (fdLeft,fdTop,fdRight,fdBottom);  procedure Fill(Po :TPoint;FillDirection :TFillDirection);
      begin
        if Canvas.Pixels[Po.X,Po.Y] <> OldFillColor then Exit;
        case FillDirection  of
             fdLeft  :begin
                         if Po.X <=0 then Exit;
                         if Canvas.Pixels[Po.X - 1,Po.Y] = OldFillColor then
                            Fill(Point(Po.X - 1,Po.Y),fdLeft);
                         if Canvas.Pixels[Po.X,Po.Y - 1] = OldFillColor then
                            Fill(Point(Po.X ,Po.Y - 1),fdTop)
                      end;
             fdTop   :begin
                         if Po.Y <= 0 then Exit;
                         if Canvas.Pixels[Po.X + 1,Po.Y] = OldFillColor then
                            Fill(Point(Po.X + 1,Po.Y),fdRight);
                         if Canvas.Pixels[Po.X ,Po.Y - 1] = OldFillColor then
                            Fill(Point(Po.X,Po.Y - 1),fdTop)
                      end;
             fdRight :begin
                         if Canvas.Pixels[Po.X + 1,Po.Y] = OldFillColor then
                            Fill(Point(Po.X + 1,Po.Y),fdRight);
                         if Canvas.Pixels[Po.X,Po.Y + 1] = OldFillColor then
                            Fill(Point(Po.X,Po.Y + 1),fdBottom)
                      end;
             fdBottom:begin
                         if Canvas.Pixels[Po.X - 1,Po.Y] = OldFillColor then
                            Fill(Point(Po.X - 1,Po.Y),fdLeft);
                         if Canvas.Pixels[Po.X,Po.Y + 1] = OldFillColor then
                            Fill(Point(Po.X,Po.Y + 1),fdBottom)
                      end;
        end;
        Canvas.Pixels[Po.X,Po.Y] := FillColor;
      end;
    begin
      Fill(Point(Po.X - 1,Po.Y - 1),fdLeft);
      Fill(Point(Po.X + 1,Po.Y + 1),fdRight);
      Fill(Point(Po.X - 1,Po.Y + 1),fdBottom);
      Fill(Point(Po.X + 1,Po.Y - 1),fdTop);
    end;
      

  4.   

    如果不是矩形区域,
    使用 FillRgn