子窗体上有ScrollBox,将PaintBox放在ScrollBox上,在PaintBox的MouseDown、MouseUp、MouseMove事件中绘图,但当有多个子窗体时,如果在一个子窗体上绘图时其它的子窗体上也同时绘出同样的图形,我要在不同的子窗体上绘不同的图形。我的程序比较复杂,为了说明这个问题,我做了一个简单的,又发现了一个新的问题,当有两个子窗体,在第一个子窗体上绘图,如果将另一个子窗体从第一个子窗体上移过,刚画的图形被檫除,以下是我的代码,这是怎么回事,最好能回答我的上面的问题。先谢谢!
unit Unit1;interfaceuses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls, Menus;type
 TForm1 = class(TForm)
   MainMenu1: TMainMenu;
   New1: TMenuItem;
   procedure New1Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;var
 Form1: TForm1;implementation
uses unit2;
{$R *.dfm}procedure TForm1.New1Click(Sender: TObject);
begin
 TForm2.Create(Application) ;
end;end.unit Unit2;interfaceuses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, ExtCtrls;type
 TForm2 = class(TForm)
   ScrollBox1: TScrollBox;
   PaintBox1: TPaintBox;
   procedure FormClose(Sender: TObject; var Action: TCloseAction);
   procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
     Shift: TShiftState; X, Y: Integer);
   procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
     Shift: TShiftState; X, Y: Integer);
   procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
     Y: Integer);
 private
   { Private declarations }
 public
   { Public declarations }
 end;var
 Form2: TForm2;
 Drawing:boolean;implementation{$R *.dfm}procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 Action:=caFree;
end;procedure TForm2.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
 Shift: TShiftState; X, Y: Integer);
begin
  Drawing:=true;
  PaintBox1.Canvas.Pen.Mode:=pmNotXor;
  PaintBox1.Canvas.MoveTo(x,y);
end;procedure TForm2.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
 Shift: TShiftState; X, Y: Integer);
begin
 Drawing:=false;
end;procedure TForm2.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
 Y: Integer);
begin
 if Drawing then
 PaintBox1.Canvas.LineTo(x,y);
end;end.

解决方案 »

  1.   

    用双DC或者自己处理WM_PAINT,或者直接调用InvalidateRect迫使窗口重画。
      

  2.   

    风大侠,就因为我把在OnMouseDown、OnMouseUp、OnMouseMove绘出的各种图形都记录下来,然后在PaintBox 的 OnPaint 重画图形,所有子窗体的PaintBox 的 OnPaint、OnMouseDown、OnMouseUp、OnMouseMove 代码都是一样的,才在不同的子窗体中画出同样的图形。有点问题不明白:用双DC能解决三个或三个以上子窗体同时绘图中出现的所有的子窗体中绘出同样的图形吗?至于上面例子中的移动后图像被擦出,我已经搞定。