Listing 20.2. DemoMouse loyihasi bosh forma moduli
unit Main;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, ComCtrls;
type
TMainForm = class(TForm) ColorDlg: TColorDialog;
StatusBar: TStatusBar; Timer: TTimer;
procedure FormMouseDown (Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure FormMouseUp (Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure TimerTimer(Sender: TObject);
private
MouseRect: TRect;
IsDown: Boolean;
RectColor: TColor;
public
{ Public declarations }
end;
var MainForm: TMainForm;
implementation {$R *.DFM}
procedure TMainForm.FormMouseDown (Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then with MouseRect do begin
IsDown := True; Left := X; Top := Y; Right := X; Bottom := Y;
Canvas.Pen.Color := RectColor;
end;
if (Button = mbRight) and ColorDlg.Execute then
RectColor := ColorDlg.Color;
end;
procedure TMainForm.FormMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
IsDown := False;
Canvas.Pen.Color := Color;
with MouseRect do
Canvas.Polyline([Point(Left, Top), Point(Right, Top),
Point(Right, Bottom), Point(Left, Bottom), Point(Left, Top)]);
with StatusBar do
begin
Panels[4].Text := ''; Panels [5] .Text := ";
end;
end;
procedure TMainForm.FonnMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
with StatusBar do
begin
Panels[2].Text := 'X: ' + IntToStr(X);
Panels[3].Text := 'Y: ' + IntToStr(Y);
end;
if Not IsDown then Exit; Canvas.Pen.Color := Color;
with mouserect do begin
Canvas.Polyline([Point(Left, Top), Point(Right, Top),
Point(Right, Bottom), Point(Left, Bottom), Point(Left, Top)]);
Right := X;
Bottom := Y;
Canvas.Pen.Color := RectColor;
Canvas.Polyline([Point(Left, Top), Point(Right, Top),
Point(Right, Bottom), Point(Left, Bottom), Point(Left, Top)]);
end;
with StatusBar do begin
Panels [4] .Text := 'Kenglik: ' + IntToStr
(Abs(MouseRect.Right - MouseRect.Left));
Panels[5].Text := 'Balandlik: ' +
IntToStr(Abs(MouseRect.Bottom - MouseRect.Top));
end;
end;
procedure TMainForm.TimerTimer(Sender: TObject);
begin
with StatusBar do
begin
Panels[0].Text := 'Sana: ' + DateToStr(Now);
Panels[1].Text := 'Baqt: ' + TimeToStr(Now);
end;
end;
end.
Sichqonchaning chap tugmasi chertilganda FormMouseDown metodida to`rtburchak chizish rejimi (isDown := True) ishga tushadi va uning boshlang’ich koordinatalari aniqlanadi.
Sichqoncha forma bo`ylab “yurganda” FormMouseMove metodi ishga tushadi va ko`rsatkichning koordinata va o`lchamlari holatlar paneliga uzatiladi. Agar sichqonchaning chap tugmasi bosilgan (isDown:=True) bo`lsa, u holda to`rtburchak qayta chiziladi.
Sichqoncha tugmasi qo`yib yuborilganda (isDown:=False) FormMouseUp metodi to`rtburchak chizish jarayonini to`xtatadi.
Agar sichqonchaning o`ng tugmasi bosilgan bo`lsa, FormMouseDown metodi rang tanlash oynasini ekranga chiqaradi va to`rtburchak chiziqlari rangini o`zgartirish imkoni paydo bo`ladi.
TimerTimer metodi xolatlar panelida joriy vaqt va sanani ko`rsatadi.
Do'stlaringiz bilan baham: |