请问在image如何编写取消上几步画的图案呢?

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls, Buttons, AppEvnts, jpeg, Menus, ActnList,
      ComCtrls, ToolWin, ImgList,TypeDefineU;
    type
      TForm1 = class(TForm)
        imgPaint: TImage;
        ToolBar1: TToolBar;
        tbtBack: TToolButton;
        tbtForward: TToolButton;
        ActionList1: TActionList;
        actBack: TAction;
        actForward: TAction;
        ToolButton6: TToolButton;
        cmbShape: TComboBox;
        cmbWidth: TComboBox;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure imgPaintMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure imgPaintMouseUp(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure imgPaintMouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
        procedure actBackExecute(Sender: TObject);
        procedure actForwardExecute(Sender: TObject);
      private
        FDownPoint,FUpPoint:TPoint;
        FGShape:array[0..MaxCount-1] of TGShape;
        Fbitmap:TBitmap;
        FCount,FMaxCount:Integer;//the count of FGShape;
        procedure BackBitmap;
      public  end;var
      Form1: TForm1;
    implementation
    uses math, DateUtils;
    {$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      FCount:=0;
      Fbitmap:=TBitmap.Create;
      with Fbitmap do
      begin
        //Canvas.Pen.Width:=cmbWidth.ItemIndex+1;
        Canvas.Pen.Style:=psSolid;
        Canvas.Pen.Mode:=pmMask;
        Canvas.Brush.Color:=clWhite;
        Width:=ClientWidth;
        Height:=ClientHeight;
      end;
      BackBitmap;
      //imgPaint.Canvas.CopyRect(imgPaint.Canvas.ClipRect,Fbitmap.Canvas,imgPaint.Canvas.ClipRect);end;procedure TForm1.BackBitmap;
    var
      j:integer;
    begin
      With Fbitmap.Canvas do
      begin
        Brush.Color:=clWhite;
        Pen.Color:=clWhite;
        FillRect(ClipRect);
        for  j:=0  to FCount-1 do
        begin
          Pen.Width:=FGShape[j].PenWidth;;
          Pen.Color:=FGShape[j].PenColor;
          Brush.Color:=FGShape[j].BrushColor;      case  FGShape[j].Shape of
          ssLine:
            begin
              MoveTo(FGShape[j].DownPoint.X,FGShape[j].DownPoint.Y,);
              LineTo(FGShape[j].UPPoint.X,FGShape[j].UPPoint.Y);
            end;
          ssRectangle:
            Rectangle(FGShape[j].DownPoint.X , FGShape[j].DownPoint.Y,
                     FGShape[j].UPPoint.X , FGShape[j].UPPoint.Y);
          ssEllipse:
            Ellipse(FGShape[j].DownPoint.X , FGShape[j].DownPoint.Y,
                    FGShape[j].UPPoint.X , FGShape[j].UPPoint.Y);
          end;
        end;
      end;
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      Fbitmap.Free;
    end;procedure TForm1.imgPaintMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      FDownPoint.X:=X;
      FDownPoint.Y:=Y;
    end;procedure TForm1.imgPaintMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      FUpPoint.X:=X;
      FUpPoint.Y:=Y;
      if FCount<MaxCount-1 then
      begin
        Randomize;
        FGShape[FCount].DownPoint:=FDownPoint;
        FGShape[FCount].UpPoint:=FUpPoint;
        FGShape[FCount].PenWidth:=cmbWidth.ItemIndex+1;
        FGShape[FCount].PenColor:=RandomRange(255,255*255*1); //边线色
        //FGShape[FCount].BrushColor:=clWhite;    //填充色
        FGShape[FCount].BrushColor:=RandomRange(255*255*8,255*255*16);
        FGShape[FCount].Shape:=tshapestyle(cmbShape.ItemIndex);
        // if mouse ,Down and Up are not a same point,it will be add into FCount
        if (FUpPoint.X<>FDownPoint.X) and ((FUpPoint.Y<>FDownPoint.Y)) then
        inc(FCount);
        FMaxCount:=FCount;
      end;
      BackBitmap;  tbtBack.Enabled:=(FCount>0)
    end;procedure TForm1.imgPaintMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      imgPaint.Canvas.CopyRect(imgPaint.Canvas.ClipRect,Fbitmap.Canvas,imgPaint.Canvas.ClipRect);
      FUpPoint.X:=X;
      FUpPoint.Y:=Y;
      if FCount<MaxCount-1 then
      begin
        FGShape[FCount].DownPoint:=FDownPoint;
        FGShape[FCount].UpPoint:=FUpPoint;
        FGShape[FCount].PenWidth:=cmbWidth.ItemIndex+1;
        //FGShape[FCount].PenColor:=RandomRange(255,255*255*1); //边线色
        //FGShape[FCount].BrushColo
        FGShape[FCount].PenColor:=RGB(123,125,123);//RGB(0,255,0);//clred;
        FGShape[FCount].Shape:=tshapestyle(cmbShape.ItemIndex);;
      end;
      if ssleft in Shift then
      begin
        with imgPaint.Canvas do
        begin
          Pen.Color:=FGShape[FCount].PenColor;
          Brush.Color:=FGShape[FCount].BrushColor;
          Pen.Width:=FGShape[FCount].PenWidth;
          // Pen.Style:=psDashDot ;
          Brush.Style:=bsClear ;
          case FGShape[FCount].Shape of
          ssLine:
          begin
            MoveTo(FGShape[FCount].DownPoint.X, FGShape[FCount].DownPoint.Y);
            LineTo(FGShape[FCount].UPPoint.X, FGShape[FCount].UPPoint.Y);
          end;
          ssRectangle:
            Rectangle(FGShape[FCount].DownPoint.X, FGShape[FCount].DownPoint.Y,
                      FGShape[FCount].UPPoint.X, FGShape[FCount].UPPoint.Y);
          ssEllipse:
            Ellipse(FGShape[FCount].DownPoint.X, FGShape[FCount].DownPoint.Y,
                    FGShape[FCount].UPPoint.X, FGShape[FCount].UPPoint.Y); 
          end;    end;
      end;
    end;procedure TForm1.actBackExecute(Sender: TObject);
    begin
      if FCount>0 then
      begin
        Dec(FCount);
        BackBitmap;
        imgPaint.Canvas.CopyRect(imgPaint.Canvas.ClipRect,Fbitmap.Canvas,imgPaint.Canvas.ClipRect);
        Caption:=inttostr(FCount);
      end;
      tbtBack.Enabled:=(FCount>0);
      tbtForward.Enabled:=(FCount<FMaxCount)
    end;procedure TForm1.actForwardExecute(Sender: TObject);
    begin
      if FCount<FMaxCount then
      begin
        Inc(FCount);
        BackBitmap;
        imgPaint.Canvas.CopyRect(imgPaint.Canvas.ClipRect,Fbitmap.Canvas,imgPaint.Canvas.ClipRect);
        Caption:=inttostr(FCount);
      end;
      tbtForward.Enabled:=(FCount<FMaxCount);
      tbtBack.Enabled:=(FCount>0)
    end;end.
      

  2.   

    用面向对象的思维去回答这个问题,很简单。每次将image对象复制到一个备份的Image对象。如果你想保留撤销几步,就创建几个备份的image对象。代码也很简单,就只需要一个Assign语句。
      

  3.   

    你可不可以用tbitmap数组来保存已显示的图片呀
      

  4.   

    lianshaohua(连少华) 兄
    你的提议好象不错,实现也很简单,但是图片占用的内存太大了,所以这种方法不可取
    ---------
    song317(山东老实人) 兄
    记录各动作(动作可当作一个对象,可以是个记录,用个数组来统一管理)  就是我采用的方法(我暂时是当作记录),但是我取消的时候不是执行反动作,而是重新画的时候,将有删除标记的不画出来而已
      

  5.   

    cll007(gazo)兄:
       说得非常有道理,嘿嘿。收藏了。