我编了一段程序,后来需要在某个按钮的onclick事件中画出一段直线。但是同样一段画直线的代码,如果直接在form的onpaint事件中可以出现直线,如果放在button的onclick事件中就没有直线出来。
我另外新建一个project,就可以在onclick事件中把直线画出来。是因为我原来程序中有的form的oncreat或者onpaint事件对在onclick事件画直线造成了影响吗?
请问是怎么回事呢?实在想不明白。

解决方案 »

  1.   

    你用button 花了线,可能那个地方刷新了,导致线看不到了,
    你在新的项目中画的线,不在paint 中画的, 用别的窗口挡一下,线也会看不到的,
      

  2.   

    肯定是你的onpaint里的代码有影响你自己把onpaint里的代码注释掉试下不就知道了吗
      

  3.   

    首先你的在buttonclick中画的线只是暂时显示的,在一刷新就会丢失,你只有将你画线的路径保存下来,然后在ONPAINT中来根据你的保存的数据来画线才行,才不会丢失
      

  4.   


    我注释掉onpaint的代码,还是画不出直线。
      

  5.   

    找到原因了,问题出在Form的属性。我原来程序中的formstyle是fsMDIForm,所以没有直线出现;改为fsNorm就能画出直线了。
      

  6.   

    在onpaint中画线的话怎么控制画的时间?我要在按钮点击了之后再出现这条线,难道在按钮点击后设置标志为true,然后强制刷新?
      

  7.   

    我在不考虑闪烁问题时经常这么干。
    稍微复杂一点的方法是做一个单独的画图函数,OnPaint和Button的OnClick都调用它。
      

  8.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        Label1: TLabel;
        Label2: TLabel;
        Edit3: TEdit;
        Edit4: TEdit;
        Label3: TLabel;
        Label4: TLabel;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure FormPaint(Sender: TObject);
      private
        FPaintList: TList;
        { Private declarations }
      public
        { Public declarations }
      end;
    type
      PPaintRecord =^TPaintRecord;
      TPaintRecord=record
        sX,sY :Integer;
        eX,eY :Integer;
      end;
    var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      FPaintList:=TList.Create;
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      while FPaintList.Count >0 do
      begin
        Dispose(PPaintRecord(FPaintList[0]));
        FPaintList.Delete(0);
      end;
      FPaintList.Free;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var p:PPaintRecord;
    begin
      New(P);
      P.sX :=StrToInt(Edit1.Text);
      P.sY :=StrToInt(Edit2.Text);
      P.eX :=StrToInt(Edit3.Text);
      P.eY :=StrToInt(Edit4.Text);
      FPaintList.Add(P);
      self.Repaint;
    end;procedure TForm1.FormPaint(Sender: TObject);
    var i:Integer;
    begin
      for i:=0 to FPaintList.Count-1 do
      begin
        self.Canvas.MoveTo(PPaintRecord(FPaintList[i]).sX,PPaintRecord(FPaintList[i]).sY);
        self.Canvas.LineTo(PPaintRecord(FPaintList[i]).eX,PPaintRecord(FPaintList[i]).eY);
      end;end;end.