- •12.Списки
- •Interface
- •Implementation
- •13.Модули
- •Interface
- •Implementation
- •Interface
- •Implementation
- •Interface
- •Implementation
- •14.Таблицы
- •Interface
- •Implementation
- •15.Графика, отображение графиков функций
- •Interface
- •Implementation
- •Initialization
- •16.Графика, примитивы
- •Interface
- •Implementation
- •Interface
- •Implementation
- •Interface
- •Implementation
- •Initialization
- •17.Графика, растровые изображения и анимация
- •Interface
- •Implementation
- •Initialization
- •Interface
- •Implementation
- •18.Тестирование и отладка
- •19.Квадратное уравнение
- •Interface
- •20.Класс tThread
- •Interface
- •Implementation
- •Interface
- •Implementation
- •Interface
- •Implementation
- •21.Класс tStream
- •Interface
- •Implementation
- •Initialization
- •Interface
- •Implementation
- •Initialization
- •22.Создание в Delphi кроссплатформенного приложения
Interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
FormColor: TButton;
ShapeColor: TButton;
ScrollBar1: TScrollBar;
ComboBox1: TComboBox;
Shape1: TShape;
ColorDialog1: TColorDialog;
ScrollBar2: TScrollBar;
Panel1: TPanel;
procedure FormColorClick(Sender: TObject);
procedure ShapeColorClick(Sender: TObject);
procedure ScrollBar1Change(Sender: TObject);
procedure ScrollBar2Change(Sender: TObject);
procedure ComboBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Implementation
{$R *.DFM}
procedure TForm1.FormColorClick(Sender: TObject);
begin
if ColorDialog1.Execute then Form1.Color:= ColorDialog1.Color;
end;
procedure TForm1.ShapeColorClick(Sender: TObject);
begin
if ColorDialog1.Execute then Shape1.Brush.Color:= ColorDialog1.Color;
end;
procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
Shape1.Width:= ScrollBar1.Position*3;
end;
procedure TForm1.ScrollBar2Change(Sender: TObject);
begin
Shape1.Height:= ScrollBar2.Position*3;
end;
procedure TForm1.ComboBox1Click(Sender: TObject);
var
Index: integer;
begin
if ComboBox1.Text='stRectangle' then Index:=0;
if ComboBox1.Text='stSquare' then Index:=1;
if ComboBox1.Text='stRoundRect' then Index:=2;
if ComboBox1.Text='stRoundSquare' then Index:=3;
if ComboBox1.Text='stEllipse' then Index:=4;
if ComboBox1.Text='stCircle' then Index:=5;
Shape1.Shape:=TShapeType(Index);
end;
end.
Задание 3
Создать проект работы с метафайлом. Метафайл содержит график с двумя фигурами (эллипс и квадрат), создается программно, отображается на форме и сохраняется на диске ПК. Для проверки праильности метафайла он считывается в компонент Image формы. Проект – приложение.
На рис. 16 .17 показана форма проекта задачи 3. Она содержит компонент Image1 для просмотра метафайла и две кнопки:
Button1 - «Создать метафайл». При нажатии кнопки метафайл MyMetafile.wmf создается, прорисовывается на форме и сохраняется на диске.
Button2 - «Смотреть метафайл». Нажатие этой кнопки приводит к отображению метафайла в компоненте Image1 формы.
рис. 16.17. Форма проекта задачи 3
Листинг модуля формы
unit Prg_16_3_;
Interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Sozdath: TButton;
Image1: TImage;
Smotreth: TButton;
procedure SozdathClick(Sender: TObject);
procedure SmotrethClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MyMetafile:TMetafile;
Implementation
{$R *.dfm}
procedure TForm1. SozdathClick(Sender: TObject);
begin
with TMetafileCanvas.Create(MyMetafile, 0) do
try
Brush.Color := clRed;
Ellipse(0,0,150,100); // Рисуем эллипс
Brush.Color:=clWhite;
Rectangle(150,150,200,200); // Рисуем квадрат
finally
Free
end;
MyMetafile.SaveToFile('MyMetafile.wmf'); // Сохранить метафайл
Form1.Canvas.Draw(0,0,MyMetafile); // Рисовать метафайл на форме
end;
procedure TForm1. SmotrethClick(Sender: TObject);
begin
Image1.Picture.LoadFromFile('MyMetafile.wmf'); // Просмотр
end;