Logo New book: Delphi 2007 Handbook
My blog in online
Delphi tech support service: support.marcocantu.com
Google
  Web www.marcocantu.com

Menu for Development

Site Menu
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)

Breaking News
Buy Mastering Borland Delphi 2005 from Amazon
Free ebook: Mastering Delphi Update for Delphi 2006

Advertising
Home My Blog Books My Bookstore Development Links Marco



Home: Code Repository: Mastering Delphi 5

Project THLOCK

Project Structure


THLOCK.DPR

program Thlock;

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.Lock;
  try
    Canvas.Pen.Color := clYellow;
    Canvas.Brush.Color := clYellow;
    Canvas.Ellipse (x - 30, y - 30, x + 30, y + 30);
  finally
    Canvas.Unlock;
  end;
end;

end.

PAINTTH.PAS

unit paintth;

interface

uses
  Classes;

type
  TPainterThread = class(TThread)
  protected
    procedure Execute; override;
  end;

implementation

{ TPainterThread }

uses
  MainForm, Graphics;

procedure TPainterThread.Execute;
var
  X, Y: Integer;
begin
  Randomize;
  repeat
    X := Random (300);
    Y := Random (Form1.ClientHeight);
    with Form1.Canvas do
    begin
      Lock;
      try
        Pixels [X, Y] := clBlue;
      finally
        Unlock;
      end;
    end;
  until Terminated;
end;

end.

MAINFORM.DFM

object Form1: TForm1
  Left = 274
  Top = 89
  Width = 370
  Height = 293
  Caption = 'Thread Lock'
  Font.Charset = DEFAULT_CHARSET
  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