File: unit1.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (133 lines) | stat: -rw-r--r-- 3,116 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
unit Unit1; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, GraphType, ExtCtrls, StdCtrls,
  ExtDlgs, LazUTF8;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    GroupBox3: TGroupBox;
    GroupBox4: TGroupBox;
    GroupBox5: TGroupBox;
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    Image4: TImage;
    Image5: TImage;
    ImageList1: TImageList;
    ListBox1: TListBox;
    OpenPictureDialog1: TOpenPictureDialog;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    function GetImageMap(AEffect: TGraphicsDrawEffect): TImage;
    procedure ListBox1SelectionChange(Sender: TObject; User: boolean);
  private

  public
    procedure ShowImages(AIndex: Integer);
    procedure AddFile(AFileName: String);
    property ImageMap[AEffect: TGraphicsDrawEffect]: TImage read GetImageMap;
  end;

var
  Form1: TForm1; 

implementation

{$R *.lfm}

{ TForm1 }

function LoadBitmapFromFile(AFileName: String): TCustomBitmap;
var
  Stream: TStream;
  GraphicClass: TGraphicClass;
begin
  Result := nil;
  Stream := nil;
  try
    Stream := TFileStream.Create(UTF8ToSys(AFileName), fmOpenRead or fmShareDenyNone);
    GraphicClass := GetGraphicClassForFileExtension(ExtractFileExt(AFileName));
    if (GraphicClass <> nil) and (GraphicClass.InheritsFrom(TCustomBitmap)) then
    begin
      Result := TCustomBitmap(GraphicClass.Create);
      Result.LoadFromStream(Stream);
    end;
  finally
    Stream.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  AddFile(ExtractFilePath(ParamStrUTF8(0)) + SetDirSeparators('images/edit-clear.png'));
  AddFile(ExtractFilePath(ParamStrUTF8(0)) + SetDirSeparators('images/edit-find-replace.png'));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then
    AddFile(OpenPictureDialog1.FileName);
end;

function TForm1.GetImageMap(AEffect: TGraphicsDrawEffect): TImage;
begin
  case AEffect of
    gdeNormal: Result := Image1;
    gdeDisabled: Result := Image2;
    gdeHighlighted: Result := Image3;
    gdeShadowed: Result := Image4;
    gde1Bit: Result := Image5;
  else
    Result := nil;
  end;
end;

procedure TForm1.ListBox1SelectionChange(Sender: TObject; User: boolean);
begin
  if ListBox1.ItemIndex <> -1 then
    ShowImages(ListBox1.ItemIndex);
end;

procedure TForm1.ShowImages(AIndex: Integer);
var
  AEffect: TGraphicsDrawEffect;
  Bmp: TBitmap;
begin
  for AEffect := Low(TGraphicsDrawEffect) to High(TGraphicsDrawEffect) do
  begin
    Bmp := TBitmap.Create;
    ImageList1.GetBitmap(AIndex, Bmp, AEffect);
    if ImageMap[AEffect] <> nil then
      ImageMap[AEffect].Picture.Assign(Bmp);
    Bmp.Free;
  end;
end;

procedure TForm1.AddFile(AFileName: String);
var
  bmp: TCustomBitmap;
begin
  bmp := LoadBitmapFromFile(AFileName);
  if bmp <> nil then
  begin
    ImageList1.Add(bmp, nil);
    ListBox1.Items.Add(ExtractFileName(AFileName));
    if ListBox1.ItemIndex = -1 then
      ListBox1.ItemIndex := 0;
  end;
  bmp.Free;
end;

end.