在窗体的画布上画图, 起初可以画,画了N遍后出错:Raise exception class EInvalidOperation With message 'Canvas does not allow drawing.
请问,什么原因会造成的?

解决方案 »

  1.   

    如果把下面这条语句注释掉,则不会出错,该语句在OnMouseMove中:
    tf:=LineMouseOn(Point(X,Y),NLine,NSegment,aPoint,bPoint);function TfrmWF.LineMouseOn(Pt: TPoint; var NL,NS: Integer; var Pt1,
      Pt2: TPoint):Boolean;
    var
      xLine:PALine;
      i,j:Integer;
      aPoint,bPoint:TPoint;
    begin
      Result:=False;
      for i:=0 to LineList.Count-1 do
        begin
          xLine:=LineList.Items[i];
          aPoint:=xLine^.PointOfALine[0];
          for j:=1 to High(xLine^.PointOfALine) do
            begin
              bPoint:=xLine^.PointOfALine[j];
              if (aPoint.Y=bPoint.Y) and PtInRegion(RgnOfAObj(1,aPoint,bPoint),Pt.X,Pt.Y) then
                begin        {horizontal line}
                  Result:=True;
                  NL:=i;
                  NS:=j;
                  Pt1:=aPoint;
                  Pt2:=bPoint;
                  Exit;
                end;
              if (bPoint.X=bPoint.X) and PtInRegion(RgnOfAObj(2,aPoint,bPoint),Pt.X,Pt.Y) then
                begin        {vertical line}
                  Result:=True;
                  NL:=i;
                  NS:=j;
                  Pt1:=aPoint;
                  Pt2:=bPoint;
                  Exit;
                end;
              aPoint:=bPoint;
            end;  //end scan a line
        end;  //end for i:=1 to High(xLine^.PointOfALine
    end;function TfrmWF.RgnOfAObj(N:ShortInt;Pt1, Pt2: TPoint): HRGN;
    var
      VarPoints:Array[0..3] of TPoint;
    begin
      Case N of
        1:          {horizontal line}
          begin
            VarPoints[0]:=Point(Pt1.X,Pt1.Y-1);
            VarPoints[1]:=Point(Pt2.X,Pt2.Y-1);
            VarPoints[2]:=Point(Pt2.X,Pt2.Y+1);
            VarPoints[3]:=Point(Pt1.X,Pt1.Y+1);
          end;
        2:          {vertical line}
          begin
            VarPoints[0]:=Point(Pt1.X-1,Pt1.Y);
            VarPoints[1]:=Point(Pt2.X+1,Pt1.Y);
            VarPoints[2]:=Point(Pt2.X+1,Pt2.Y);
            VarPoints[3]:=Point(Pt2.X-1,Pt2.Y);
          end;
        3:          {rectangle}
          begin
            VarPoints[0]:=Point(Pt1.X,Pt1.Y);
            VarPoints[1]:=Point(Pt2.X,Pt1.Y);
            VarPoints[2]:=Point(Pt2.X,Pt2.Y);
            VarPoints[3]:=Point(Pt1.X,Pt2.Y);
          end;
      end;  //end case N of
      Result:=CreatePolygonRgn(VarPoints,4,WINDING);
    end;
      

  2.   

    这个是另一个关于这个问题的帖子国外站点找到的关于这个问题的问答,看一下吧:Q:
    What does the error message 'Canvas does not allow drawing' mean?A:
    you may have run out of resources, which means a new DC cannot be
    allocated. Usually this is a result of not freeing unused DCs or other
    objectsyou are attempting to draw to an device context that is not yet valid.
    Such as in the create constructor of a component.you are trying to draw on a canvas while your last draw-operation has
    not finished yet. In this case you can use Canvas.Lock or Canvas.TryLock to fix it.
      

  3.   

    canvas是不是没有得到HANDLE值造成的