Logo Delphi Developer Days 2011

Menu for Development

Site Menu
Delphi 2010 Handbook
Delphi 2009 Handbook
Delphi 2007 Handbook
Mastering Borland Delphi 2005
Essential Pascal
Essential Delphi
Buy Books Online
Code Repository
Newsgroups
White Papers
Tools
Conferences
Training
Delphi Links
Contact Marco

My Other Sites
Italian Site (www.marcocantu.it)
Developers Newsgroups Browser (dev.newswhat.com)
My town (www.piazzacavalli.net)
the delphi search
Wintech Italia (my company)

Advertising
Home My Blog Books My Bookstore Development Links Marco



Home: Code Repository: Mastering Delphi 5

Project THOLD

Project Structure


THOLD.DPR

program ThOld;

uses
  Forms,
  MainForm in 'MainForm.pas' {Form1},
  PaintTh in 'PaintTh.pas';

{$R *.RES}

begin
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

MAINFORM.PAS

unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Paintth;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    PT: TPainterThread;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.Enabled := False;
  Button2.Enabled := True;
  PT := TPainterThread.Create (False);  // start
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  PT.Free;
  Button1.Enabled := True;
  Button2.Enabled := False;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Canvas.Pen.Color := Color; // of the form
  Canvas.Brush.Color := Color;
  Canvas.Ellipse (x - 30, y - 30, x + 30, y + 30);
end;

end.

PAINTTH.PAS

unit paintth;

interface

uses
  Classes;

type
  TPainterThread = class(TThread)
  private
    X, Y: Integer;
  protected
    procedure Execute; override;
    procedure Paint;
  end;

implementation

{ TPainterThread }

uses
  MainForm, Graphics;

procedure TPainterThread.Paint;
begin
  Form1.Canvas.Pixels [X, Y] := clRed;
end;

procedure TPainterThread.Execute;
begin
  Randomize;
  repeat
    X := Random (300);
    Y := Random (Form1.ClientHeight);
    Synchronize (Paint);
  until Terminated;
end;

end.

MAINFORM.DFM

object Form1: TForm1
  Left = 200
  Top = 109
  Width = 370
  Height = 293
  Caption = 'Thread Old'
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OnMouseDown = FormMouseDown
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 272
    Top = 16
    Width = 75
    Height = 25
    Caption = 'Start'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 272
    Top = 48
    Width = 75
    Height = 25
    Caption = 'Stop'
    Enabled = False
    TabOrder = 1
    OnClick = Button2Click
  end
end