unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ToolWin, ComCtrls, StdCtrls;
const
  ccolcount = 20;
  crowcount = 20;
  cheight = 20;
  cwidth = 20;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    ToolBar1: TToolBar;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
function threaddraw(P:pointer):longint;stdcall;
function threadcount(P:pointer):longint;stdcall;
function pointswap(var npointa,npointb:tpoint);
implementation{$R *.dfm}
procedure pointswap(var npointa,npointb:tpoint);
var
  temp: tpoint;
begin
  temp.X := npointa.X;
  temp.Y := npointa.Y;
  npointa.X := npointb.X;
  npointa.Y := npointb.Y;
  npointb.X := temp.X;
  npointb.Y := temp.Y;
end;
function threaddraw(p:pointer):longint;stdcall;
var
  a:array[1.. ccolcount * crowcount] of tpoint;
  i,j,k :integer;
begin
  //////begin 得到一个顺序的坐标列表
  k := 0;
  for i := 1 to ccolcount do
   for j := 1 to crowcount do begin
     inc(k);
     a[k].X := i;
     a[k].Y := j;
   end;
for i := 1 to ccolcount * crowcount do
 pointswap(a[i],a[succ(random(ccolcount * crowcount))]);
 form1.Canvas.Brush.Color := form1.Color;
 form1.Canvas.FillRect(rect(0,0,ccolcount * cwidth,crowcount * cheight));
 for i := 1 to ccolcount * crowcount do begin
   form1.Canvas.Brush.Color := random($ffffff);
   j := pred(a[i].X) * cwidth;
   k := pred(a[i].Y) * cheight;
   sleep(20);
   form1.Canvas.FillRect(rect(j,k,j+cwidth,k+cheight));
  end;
end;
function thrradcount(p:pointer):longint;stdcall;
var
 i:integer;
 dc:hdc;
 s:string;
begin
  dc:= getdc(form1.Handle);
  for I:=0 to 1000000 do begin
  s:= inttostr(i);
  textout(dc,100,100,pchar(s),length(s));
  end;
  releasedc(form1.Handle,dc);
end;procedure TForm1.Button1Click(Sender: TObject);
var
  hthread : thandle;
  threadid : dword;
begin
  hthread := createthread(nil,
                            0,
                            @threadcount,
                            nil,
                            0,
                            threadid);
  if hthread = 0 then
  messagebox(handle,'线程建立失败!',nil,mb_ok);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
  threadcount(nil);
end;procedure TForm1.Button3Click(Sender: TObject);
var
  hthread :thandle;
  threadid : dword;
begin
  hthread := createthread(nil,
                            0,
                            @threaddraw,
                            nil,
                            0,
                            threadid);
  if hthread = 0 then
  messagebox(handle,'线程建立失败!',nil,mb_ok);
end;procedure TForm1.Button4Click(Sender: TObject);
begin
  threaddraw(nil);
end;end.

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ToolWin, ComCtrls, StdCtrls;
    const
      ccolcount = 20;
      crowcount = 20;
      cheight = 20;
      cwidth = 20;
    type
      ethreadcount = function(P: pointer): longint;
      TForm1 = class(TForm)
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
      private
        { Private declarations }  public
        { Public declarations }
      end;var
      Form1: TForm1;procedure pointswap(var npointa, npointb: tpoint);
    implementation{$R *.dfm}procedure pointswap(var npointa, npointb: tpoint);
    var
      temp: tpoint;
    begin
      temp.X := npointa.X;
      temp.Y := npointa.Y;
      npointa.X := npointb.X;
      npointa.Y := npointb.Y;
      npointb.X := temp.X;
      npointb.Y := temp.Y;
    end;function threaddraw(p: pointer): longint; stdcall;
    var
      a: array[1..ccolcount * crowcount] of tpoint;
      i, j, k: integer;
    begin
      //////begin 得到一个顺序的坐标列表
      k := 0;
      for i := 1 to ccolcount do
        for j := 1 to crowcount do
        begin
          inc(k);
          a[k].X := i;
          a[k].Y := j;
        end;
      for i := 1 to ccolcount * crowcount do
        pointswap(a[i], a[succ(random(ccolcount * crowcount))]);
      form1.Canvas.Brush.Color := form1.Color;
      form1.Canvas.FillRect(rect(0, 0, ccolcount * cwidth, crowcount * cheight));
      for i := 1 to ccolcount * crowcount do
      begin
        form1.Canvas.Brush.Color := random($FFFFFF);
        j := pred(a[i].X) * cwidth;
        k := pred(a[i].Y) * cheight;
        sleep(20);
        form1.Canvas.FillRect(rect(j, k, j + cwidth, k + cheight));
      end;
    end;function threadcount(p: pointer): longint; stdcall;
    var
      i: integer;
      dc: hdc;
      s: string;
    begin
      dc := getdc(form1.Handle);
      for I := 0 to 1000000 do
      begin
        s := inttostr(i);
        textout(dc, 100, 100, pchar(s), length(s));
      end;
      releasedc(form1.Handle, dc);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      hthread: thandle;
      threadid: dword;
    begin
      hthread := createthread(nil,
        0,
        @threadcount,
        nil,
        0,
        threadid);
      if hthread = 0 then
        messagebox(handle, '线程建立失败!', nil, mb_ok);
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      threadcount(nil);
    end;procedure TForm1.Button3Click(Sender: TObject);
    var
      hthread: thandle;
      threadid: dword;
    begin
      hthread := createthread(nil,
        0,
        @threaddraw,
        nil,
        0,
        threadid);
      if hthread = 0 then
        messagebox(handle, '线程建立失败!', nil, mb_ok);
    end;procedure TForm1.Button4Click(Sender: TObject);
    begin
      threaddraw(nil);
    end;end.