-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathATImageBox.pas
More file actions
1346 lines (1204 loc) · 33.1 KB
/
ATImageBox.pas
File metadata and controls
1346 lines (1204 loc) · 33.1 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{************************************}
{ }
{ ATImageBox Component }
{ Copyright (C) Alexey Torgashin }
{ http://www.uvviewsoft.com }
{ }
{************************************}
{$BOOLEVAL OFF} //Short boolean evaluation required
{$I ATImageBoxOptions.inc} //ATImageBox options
unit ATImageBox;
interface
uses
Windows, Messages, Classes, Controls, Graphics,
StdCtrls, ExtCtrls,
{$ifdef TNT} TntGraphics, {$endif}
Forms;
const
cViewerDefaultResampleDelay = 300;
cViewerImageScales: array[1 .. 33] of Integer = (
1, 2, 4, 7, 10, 15, 20, 25, 30,
40, 50, 60, 70, 80, 90, 100,
125, 150, 175, 200, 250, 300, 350, 400, 450, 500,
600, 700, 800, 1000, 1200, 1400, 1600);
type
TPictureWide = {$ifdef TNT}TTntPicture{$else}TPicture{$endif};
TATScrollAltEvent = procedure(Sender: TObject; Inc: Boolean) of object;
TATOnAfterPaint = procedure(Sender: TObject; aCanvas: TCanvas; const aDrawRec: TRect) of object;
type
TATImage = class(TGraphicControl)
private
FPicture: TPictureWide;
FOnPaint: TNotifyEvent;
FOnProgress: TProgressEvent;
FOnAfterPaint: TATOnAfterPaint;
FStretch: Boolean;
FCenter: Boolean;
FIncrementalDisplay: Boolean;
FTransparent: Boolean;
FResample: Boolean;
FResampleBackColor: TColor;
FDrawing: Boolean;
FProportional: Boolean;
FTimer: TTimer; //Helper timer to do resampling after a delay
procedure PictureChanged(Sender: TObject);
procedure SetCenter(Value: Boolean);
procedure SetPicture(Value: TPictureWide);
procedure SetStretch(Value: Boolean);
procedure SetTransparent(Value: Boolean);
procedure SetProportional(Value: Boolean);
procedure SetResample(Value: Boolean);
procedure TimerTimer(Sender: TObject);
procedure PaintResampled;
function GetResampleDelay: Integer;
procedure SetResampleDelay(AValue: Integer);
protected
function CanAutoSize(var NewWidth, NewHeight: Integer): Boolean; override;
function DestRect: TRect;
function DoPaletteChange: Boolean;
function GetPalette: HPALETTE; override;
procedure Paint; override;
procedure Progress(Sender: TObject; Stage: TProgressStage;
PercentDone: Byte; RedrawNow: Boolean; const R: TRect; const Msg: string); dynamic;
procedure DoExtraPaint(const aRect: TRect);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Align;
property Anchors;
property AutoSize;
property Center: Boolean read FCenter write SetCenter default False;
property Constraints;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property IncrementalDisplay: Boolean read FIncrementalDisplay write FIncrementalDisplay default False;
property ParentShowHint;
property Picture: TPictureWide read FPicture write SetPicture;
property PopupMenu;
property Proportional: Boolean read FProportional write SetProportional default false;
property ShowHint;
property Stretch: Boolean read FStretch write SetStretch default False;
property Transparent: Boolean read FTransparent write SetTransparent default False;
property Resample: Boolean read FResample write SetResample default False;
property ResampleDelay: Integer read GetResampleDelay write SetResampleDelay default cViewerDefaultResampleDelay;
property ResampleBackColor: TColor read FResampleBackColor write FResampleBackColor default clWhite;
property Visible;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
property OnProgress: TProgressEvent read FOnProgress write FOnProgress;
property OnStartDock;
property OnStartDrag;
property OnAfterPaint: TATOnAfterPaint read FOnAfterPaint write FOnAfterPaint;
end;
type
TATImageBox = class(TScrollBox)
private
FFocusable: Boolean;
FIsGif: Boolean;
FImageGif: TImage;
FImage: TATImage;
FImageLabel: TLabel;
FImageWidth: Integer;
FImageHeight: Integer;
FImageFit,
FImageFitOnlyBig,
FImageFitWidth,
FImageFitHeight,
FImageCenter: Boolean;
FImageScale: Integer;
FImageKeepPosition: Boolean;
FImageDrag: Boolean;
FImageDragCursor: TCursor;
FImageScaleCursor: TCursor;
FImageDragging: Boolean;
FImageDraggingPoint: TPoint;
FImageMouseDown: Boolean;
FOnScroll: TNotifyEvent;
FOnScrollAlt: TATScrollAltEvent;
FOnOptionsChange: TNotifyEvent;
procedure DoScroll;
procedure DoScrollAlt(AInc: Boolean);
procedure DoOptionsChange;
procedure MouseWheelUp(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
procedure MouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
procedure UpdateImagePosition(AResetPosition: Boolean = False);
procedure UpdateImageLabelPosition;
procedure SetImageFit(AValue: Boolean);
procedure SetImageFitOnlyBig(AValue: Boolean);
procedure SetImageFitWidth(AValue: Boolean);
procedure SetImageFitHeight(AValue: Boolean);
procedure SetImageCenter(AValue: Boolean);
procedure SetImageScale(AValue: Integer);
procedure ImageMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure ImageMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure ImageMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure ImagePaint(Sender: TObject);
procedure ImageProgress(Sender: TObject;
Stage: TProgressStage; PercentDone: Byte; RedrawNow: Boolean; const R: TRect; const Msg: string);
function GetOnAfterPaint: TATOnAfterPaint;
procedure SetOnAfterPaint(Value: TATOnAfterPaint);
public
constructor Create(AOwner: TComponent); override;
procedure LoadFromFile(const FN: string);
procedure LoadBitmap(ABitmap: TBitmap; ATransp: Boolean);
procedure LoadPicture(APicture: TPicture);
procedure Unload;
procedure UpdateInfo;
function CurrentPicture: TPicture;
procedure IncreaseImageScale(AIncrement: Boolean);
property Image: TATImage read FImage;
property ImageLabel: TLabel read FImageLabel;
property ImageWidth: Integer read FImageWidth;
property ImageHeight: Integer read FImageHeight;
property ImageScale: Integer read FImageScale write SetImageScale;
protected
procedure WMHScroll(var Msg: TMessage); message WM_HSCROLL;
procedure WMVScroll(var Msg: TMessage); message WM_VSCROLL;
procedure WMGetDlgCode(var Message: TMessage); message WM_GETDLGCODE;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure Resize; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
published
property Focusable: Boolean read FFocusable write FFocusable default True;
property ImageFitToWindow: Boolean read FImageFit write SetImageFit default False;
property ImageFitOnlyBig: Boolean read FImageFitOnlyBig write SetImageFitOnlyBig default True;
property ImageFitWidth: Boolean read FImageFitWidth write SetImageFitWidth default False;
property ImageFitHeight: Boolean read FImageFitHeight write SetImageFitHeight default False;
property ImageCenter: Boolean read FImageCenter write SetImageCenter default True;
property ImageKeepPosition: Boolean read FImageKeepPosition write FImageKeepPosition default True;
property ImageDrag: Boolean read FImageDrag write FImageDrag default True;
property ImageDragCursor: TCursor read FImageDragCursor write FImageDragCursor default crSizeAll;
property ImageScaleCursor: TCursor read FImageScaleCursor write FImageScaleCursor default crSizeNS;
property OnScroll: TNotifyEvent read FOnScroll write FOnScroll;
property OnScrollAlt: TATScrollAltEvent read FOnScrollAlt write FOnScrollAlt;
property OnOptionsChange: TNotifyEvent read FOnOptionsChange write FOnOptionsChange;
property OnAfterPaint: TATOnAfterPaint read GetOnAfterPaint write SetOnAfterPaint;
end;
procedure Register;
implementation
uses
SysUtils
{$ifdef GIF} , GifImage {$endif};
{ Constants }
const
cImageLineSize = 50; //Line size: pixels to scroll by arrows and mouse sheel
cImageGapSize = 20; //Gap size: PgUp/PgDn/Home/End scroll by control size minus gap size
{ Helper functions }
function IMax(N1, N2: Integer): Integer;
begin
if N1 >= N2 then
Result := N1
else
Result := N2;
end;
function IMin(N1, N2: Integer): Integer;
begin
if N1 <= N2 then
Result := N1
else
Result := N2;
end;
{
We need to "fix" icon sizes. Icon should be drawn once before its sizes are to be read.
http://qc.codegear.com/wc/qcmain.aspx?d=6018
}
procedure FixIcon(AIcon: TIcon);
var
Bmp: TBitmap;
begin
try
Bmp:= TBitmap.Create;
try
Bmp.PixelFormat := pf24bit;
Bmp.Canvas.Draw(0, 0, AIcon);
finally
Bmp.Free;
end;
except
end;
end;
{
Scaling doesn't work with icons. So, we need to convert icon to a bitmap,
preferrably with PixelFormat = pf24bit.
}
function FixImageFormat(AImage: TATImage; ABackColor: TColor): Boolean;
var
bmp: TBitmap;
begin
Result := True;
with AImage.Picture do
if (not (Graphic is TBitmap)) or ((TBitmap(Graphic).PixelFormat <> pf24Bit)) then
try
bmp := TBitmap.Create;
try
bmp.PixelFormat := pf24bit;
bmp.Width := Graphic.Width;
bmp.Height := Graphic.Height;
bmp.Canvas.Brush.Color:= ABackColor;
bmp.Canvas.FillRect(Rect(0, 0, bmp.Width, bmp.Height));
bmp.Canvas.Draw(0, 0, Graphic);
AImage.Picture.Graphic := bmp;
finally
bmp.Free;
end;
except
Result := False;
end;
end;
{ TATImage }
constructor TATImage.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
FPicture := TPictureWide.Create;
FPicture.OnChange := PictureChanged;
FPicture.OnProgress := Progress;
Height := 105;
Width := 105;
FTimer := TTimer.Create(Self);
FTimer.Enabled := False;
FTimer.Interval := cViewerDefaultResampleDelay;
FTimer.OnTimer := TimerTimer;
FResampleBackColor := clWhite;
FOnAfterPaint := nil;
end;
destructor TATImage.Destroy;
begin
FPicture.Free;
inherited Destroy;
end;
function TATImage.GetPalette: HPALETTE;
begin
Result := 0;
if FPicture.Graphic <> nil then
Result := FPicture.Graphic.Palette;
end;
function TATImage.DestRect: TRect;
var
w, h, cw, ch: Integer;
xyaspect: Double;
begin
w := Picture.Width;
h := Picture.Height;
cw := ClientWidth;
ch := ClientHeight;
if Stretch or (Proportional and ((w > cw) or (h > ch))) then
begin
if Proportional and (w > 0) and (h > 0) then
begin
xyaspect := w / h;
if w > h then
begin
w := cw;
h := Trunc(cw / xyaspect);
if h > ch then // woops, too big
begin
h := ch;
w := Trunc(ch * xyaspect);
end;
end
else
begin
h := ch;
w := Trunc(ch * xyaspect);
if w > cw then // woops, too big
begin
w := cw;
h := Trunc(cw / xyaspect);
end;
end;
end
else
begin
w := cw;
h := ch;
end;
end;
with Result do
begin
Left := 0;
Top := 0;
Right := w;
Bottom := h;
end;
if Center then
OffsetRect(Result, (cw - w) div 2, (ch - h) div 2);
end;
procedure TATImage.Paint;
var
Save: Boolean;
tmpRect: TRect;
begin
if csDesigning in ComponentState then
with inherited Canvas do
begin
Pen.Style := psDash;
Brush.Style := bsClear;
Rectangle(0, 0, Width, Height);
end;
Save := FDrawing;
FDrawing := True;
try
tmpRect := DestRect;
//Do the standard rendering
with inherited Canvas do
StretchDraw(tmpRect, Picture.Graphic);
DoExtraPaint(tmpRect);
//Do the delayed resampling rendering
if FResample
//Do not resample metafiles:
and not (Picture.Graphic is TMetafile)
(*ifdef GIF}
//Do not resample *animated* GIF images:
and not ((Picture.Graphic is TGifImage) and ((Picture.Graphic as TGifImage).Images.Count > 1))
endif}*) then
begin
FTimer.Enabled := False;
FTimer.Enabled := True;
end;
finally
FDrawing := Save;
end;
end;
procedure TATImage.DoExtraPaint(const aRect: TRect);
begin
if Assigned(FOnAfterPaint) then
FOnAfterPaint(Self, inherited Canvas, aRect);
end;
procedure TATImage.PaintResampled;
var
Bmp: TBitmap;
tmpRect: TRect;
begin
Bmp := TBitmap.Create;
try
with Bmp do
begin
PixelFormat := pf24bit;
Width := Picture.Width;
Height := Picture.Height;
Canvas.Brush.Color := FResampleBackColor;
Canvas.FillRect(Rect(0, 0, Width, Height));
Canvas.Draw(0, 0, Picture.Graphic);
end;
with inherited Canvas do
begin
tmpRect := DestRect;
SetStretchBltMode(Handle, STRETCH_HALFTONE);
SetBrushOrgEx(Handle, 0, 0, nil);
StretchBlt(
Handle,
tmpRect.Left, tmpRect.Top, tmpRect.Right - tmpRect.Left, tmpRect.Bottom - tmpRect.Top,
Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height,
SRCCOPY);
end;
finally
Bmp.Free;
end;
if Assigned(FOnPaint) then
FOnPaint(Self);
DoExtraPaint(tmpRect);
end;
function TATImage.DoPaletteChange: Boolean;
var
ParentForm: TCustomForm;
Tmp: TGraphic;
begin
Result := False;
Tmp := Picture.Graphic;
if Visible and (not (csLoading in ComponentState)) and (Tmp <> nil) and
(Tmp.PaletteModified) then
begin
if (Tmp.Palette = 0) then
Tmp.PaletteModified := False
else
begin
ParentForm := GetParentForm(Self);
if Assigned(ParentForm) and ParentForm.Active and Parentform.HandleAllocated then
begin
if FDrawing then
ParentForm.Perform(wm_QueryNewPalette, 0, 0)
else
PostMessage(ParentForm.Handle, wm_QueryNewPalette, 0, 0);
Result := True;
Tmp.PaletteModified := False;
end;
end;
end;
end;
procedure TATImage.Progress(Sender: TObject; Stage: TProgressStage;
PercentDone: Byte; RedrawNow: Boolean; const R: TRect; const Msg: string);
begin
if FIncrementalDisplay and RedrawNow then
begin
if DoPaletteChange then Update
else Paint;
end;
if Assigned(FOnProgress) then FOnProgress(Sender, Stage, PercentDone, RedrawNow, R, Msg);
end;
procedure TATImage.SetCenter(Value: Boolean);
begin
if FCenter <> Value then
begin
FCenter := Value;
PictureChanged(Self);
end;
end;
procedure TATImage.SetPicture(Value: TPictureWide);
begin
FPicture.Assign(Value);
end;
procedure TATImage.SetStretch(Value: Boolean);
begin
if Value <> FStretch then
begin
FStretch := Value;
PictureChanged(Self);
end;
end;
procedure TATImage.SetTransparent(Value: Boolean);
begin
if Value <> FTransparent then
begin
FTransparent := Value;
PictureChanged(Self);
end;
end;
procedure TATImage.SetProportional(Value: Boolean);
begin
if FProportional <> Value then
begin
FProportional := Value;
PictureChanged(Self);
end;
end;
procedure TATImage.SetResample(Value: Boolean);
begin
//Resampling works only under WinNT, since
//STRETCH_HALFTONE doesn't work under Win9x
if Win32Platform = VER_PLATFORM_WIN32_NT then
if Value <> FResample then
begin
FResample := Value;
PictureChanged(Self);
end;
end;
procedure TATImage.PictureChanged(Sender: TObject);
var
G: TGraphic;
D : TRect;
begin
if AutoSize and (Picture.Width > 0) and (Picture.Height > 0) then
SetBounds(Left, Top, Picture.Width, Picture.Height);
G := Picture.Graphic;
if G <> nil then
begin
if not ((G is TMetaFile) or (G is TIcon)) then
G.Transparent := FTransparent;
D := DestRect;
if (not G.Transparent) and (D.Left <= 0) and (D.Top <= 0) and
(D.Right >= Width) and (D.Bottom >= Height) then
ControlStyle := ControlStyle + [csOpaque]
else // picture might not cover entire clientrect
ControlStyle := ControlStyle - [csOpaque];
if DoPaletteChange and FDrawing then Update;
end
else ControlStyle := ControlStyle - [csOpaque];
if not FDrawing then Invalidate;
end;
function TATImage.CanAutoSize(var NewWidth, NewHeight: Integer): Boolean;
begin
Result := True;
if not (csDesigning in ComponentState) or (Picture.Width > 0) and
(Picture.Height > 0) then
begin
if Align in [alNone, alLeft, alRight] then
NewWidth := Picture.Width;
if Align in [alNone, alTop, alBottom] then
NewHeight := Picture.Height;
end;
end;
procedure TATImage.TimerTimer(Sender: TObject);
begin
FTimer.Enabled := False;
PaintResampled;
end;
function TATImage.GetResampleDelay: Integer;
begin
Result := FTimer.Interval;
end;
procedure TATImage.SetResampleDelay(AValue: Integer);
begin
FTimer.Interval := AValue;
end;
{ TATImageBox }
constructor TATImageBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
//Init inherited properties
AutoScroll := False;
DoubleBuffered := True; //To remove flicker when new image is loaded
HorzScrollBar.Tracking := True;
VertScrollBar.Tracking := True;
//Init fields
FFocusable:= True;
FImageFit := False;
FImageFitOnlyBig := True;
FImageFitWidth := False;
FImageFitHeight := False;
FImageCenter := True;
FImageWidth := 0;
FImageHeight := 0;
FImageScale := 100;
FImageKeepPosition := True;
FImageDrag := True;
FImageDragCursor := crSizeAll;
FImageScaleCursor := crSizeNS;
FImageDragging := False;
FImageDraggingPoint := Point(0, 0);
FImageMouseDown := False;
//Init objects
FImageGif := TImage.Create(Self);
with FImageGif do
begin
Parent := Self;
Align := alClient;
AutoSize := False;
Center := True;
Proportional := True;
end;
FImage := TATImage.Create(Self);
with FImage do
begin
Parent := Self;
Align := alNone;
AutoSize := False;
IncrementalDisplay := True;
OnMouseDown := ImageMouseDown;
OnMouseUp := ImageMouseUp;
OnMouseMove := ImageMouseMove;
OnPaint := ImagePaint;
OnProgress := ImageProgress;
end;
FImageLabel := TLabel.Create(Self);
with FImageLabel do
begin
Parent := Self;
Visible := False;
Brush.Style := bsClear;
Font.Style := [fsBold];
Font.Color := clWhite;
Caption := '';
end;
//Init event handlers
OnMouseWheelUp := MouseWheelUp;
OnMouseWheelDown := MouseWheelDown;
end;
procedure TATImageBox.DoScroll;
begin
UpdateImageLabelPosition;
if Assigned(FOnScroll) then
FOnScroll(Self);
end;
procedure TATImageBox.DoScrollAlt(AInc: Boolean);
begin
if Assigned(FOnScrollAlt) then
FOnScrollAlt(Self, AInc);
end;
procedure TATImageBox.WMHScroll(var Msg: TMessage);
begin
inherited;
DoScroll;
end;
procedure TATImageBox.WMVScroll(var Msg: TMessage);
begin
inherited;
DoScroll;
end;
procedure TATImageBox.MouseWheelUp(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
begin
if (Shift = []) then
begin
with VertScrollBar do
Position := Position - cImageLineSize;
DoScroll;
end
else
if (Shift = [ssShift]) then
begin
with HorzScrollBar do
Position := Position - cImageLineSize;
DoScroll;
end
else
if (Shift = [ssCtrl]) or FImageMouseDown then
begin
IncreaseImageScale(True);
FImageDragging := False;
if FImageMouseDown then
Screen.Cursor := FImageScaleCursor;
end;
Handled := True;
end;
procedure TATImageBox.MouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
begin
if (Shift = []) then
begin
with VertScrollBar do
Position := Position + cImageLineSize;
DoScroll;
end
else
if (Shift = [ssShift]) then
begin
with HorzScrollBar do
Position := Position + cImageLineSize;
DoScroll;
end
else
if (Shift = [ssCtrl]) or FImageMouseDown then
begin
IncreaseImageScale(False);
FImageDragging := False;
if FImageMouseDown then
Screen.Cursor := FImageScaleCursor;
end;
Handled := True;
end;
procedure TATImageBox.WMGetDlgCode(var Message: TMessage);
begin
Message.Result := DLGC_WANTARROWS;
end;
procedure TATImageBox.KeyDown(var Key: Word; Shift: TShiftState);
function PageSize(AClientSize: Integer): Integer;
begin
Result := IMax(AClientSize - cImageGapSize, AClientSize div 3 * 2);
end;
begin
case Key of
VK_LEFT:
begin
if Shift = [] then
begin
with HorzScrollBar do
Position := Position - cImageLineSize;
DoScroll;
Key := 0;
end
else
if Shift = [ssCtrl] then
begin
with HorzScrollBar do
Position := 0;
DoScroll;
Key := 0;
end
else
if Shift = [ssAlt] then
begin
DoScrollAlt(False);
Key := 0;
end;
end;
VK_RIGHT:
begin
if Shift = [] then
begin
with HorzScrollBar do
Position := Position + cImageLineSize;
DoScroll;
Key := 0;
end
else
if Shift = [ssCtrl] then
begin
with HorzScrollBar do
Position := Range;
DoScroll;
Key := 0;
end
else
if Shift = [ssAlt] then
begin
DoScrollAlt(True);
Key := 0;
end;
end;
VK_HOME:
if Shift = [] then
begin
with HorzScrollBar do
Position := Position - PageSize(ClientWidth);
DoScroll;
Key := 0;
end;
VK_END:
if Shift = [] then
begin
with HorzScrollBar do
Position := Position + PageSize(ClientWidth);
DoScroll;
Key := 0;
end;
VK_UP:
begin
if Shift = [] then
begin
with VertScrollBar do
Position := Position - cImageLineSize;
DoScroll;
Key := 0;
end
else
if Shift = [ssCtrl] then
begin
with VertScrollBar do
Position := 0;
DoScroll;
Key := 0;
end;
end;
VK_DOWN:
begin
if Shift = [] then
begin
with VertScrollBar do
Position := Position + cImageLineSize;
DoScroll;
Key := 0;
end
else
if Shift = [ssCtrl] then
begin
with VertScrollBar do
Position := Range;
DoScroll;
Key := 0;
end;
end;
VK_PRIOR:
if Shift = [] then
begin
with VertScrollBar do
Position := Position - PageSize(ClientHeight);
DoScroll;
Key := 0;
end;
VK_NEXT:
if Shift = [] then
begin
with VertScrollBar do
Position := Position + PageSize(ClientHeight);
DoScroll;
Key := 0;
end;
end;
end;
procedure TATImageBox.UpdateImagePosition(AResetPosition: Boolean = False);
var
AKeepPosition: Boolean;
AWidth, AHeight,
ANewWidth, ANewHeight,
ANewLeft, ANewTop,
AScrollMaxX, AScrollMaxY: Integer;
ARatio, AImageRatio,
ACenterRatioX, ACenterRatioY: Double;
begin
AKeepPosition := FImageKeepPosition and (not AResetPosition);
AWidth := ClientWidth;
AHeight := ClientHeight;
//Save center position, need to restore it later
ACenterRatioX := 0;
ACenterRatioY := 0;
if FImage.Width > 0 then
begin
if FImage.Left >= 0 then
ACenterRatioX := (AWidth div 2 - FImage.Left) / FImage.Width
else
ACenterRatioX := (AWidth div 2 + HorzScrollBar.Position) / FImage.Width;
end;
if FImage.Height > 0 then
begin
if FImage.Top >= 0 then
ACenterRatioY := (AHeight div 2 - FImage.Top) / FImage.Height
else
ACenterRatioY := (AHeight div 2 + VertScrollBar.Position) / FImage.Height;
end;
//Set controls params
if not AKeepPosition then
begin
HorzScrollBar.Position := 0;
VertScrollBar.Position := 0;
end;
AutoScroll := not FImageFit;
FImage.AutoSize := (not FImageFit) and (FImageScale = 100);
FImage.Stretch := not FImage.AutoSize;
{
//Note: commented, because we convert icon to bitmap in UpdateInfo.
//Work around VCL draw bug for icons:
if FImageIsIcon then
begin
FImage.AutoSize := False;
FImage.Stretch := True;
FImage.Width := FImageWidth;
FImage.Height := FImageHeight;
end;
}
//Fit and recalculate ImageScale
FImage.Left := 0;
FImage.Top := 0;
AWidth := ClientWidth;
AHeight := ClientHeight;
if FImageFit then
begin
{
//Note: code commented in as it causes wrong scaling sometimes.
//If image is already fit, don't scale it:
if (FImage.Width = AWidth) and
(FImage.Height = AHeight) then
begin
ANewWidth := FImage.Width;
ANewHeight := FImage.Height;
end
else
}
//Need to scale
begin
ANewWidth := FImageWidth;
ANewHeight := FImageHeight;
if FImageFitOnlyBig and
(FImageWidth <= AWidth) and (FImageHeight <= AHeight) then
begin
FImageScale := 100;
end
else
begin
if (AWidth > 0) and (AHeight > 0) and
(FImageWidth > 0) and (FImageHeight > 0) then
begin
ARatio := AWidth / AHeight;
AImageRatio := FImageWidth / FImageHeight;
if ((ARatio >= AImageRatio) and (not FImageFitWidth)) or FImageFitHeight then
begin
//fit height
if FImageFitOnlyBig and (AHeight >= FImageHeight) then begin end
else
begin