File: glass.pas

package info (click to toggle)
vmg 3.8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,528 kB
  • sloc: pascal: 5,160; cpp: 605; sh: 278; lex: 105; ansic: 68; makefile: 37
file content (1056 lines) | stat: -rw-r--r-- 34,883 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
{
glass.pas

Class which implements the magnifying effect

Copyright (C) 1998 - 2010 Harri Pyy, Chris O'Donnell, Felipe Monteiro de Carvalho

This file is part of Virtual Magnifying Glass.

Virtual Magnifying Glass is free software;
you can redistribute it and/or modify it under the
terms of the GNU General Public License version 2
as published by the Free Software Foundation.

Virtual Magnifying Glass is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.

Please note that the General Public License version 2 does not permit
incorporating Virtual Magnifying Glass into proprietary
programs.

AUTHORS: Chris O'Donnell, Felipe Monteiro de Carvalho and Harri Pyy
}
unit glass;

{$IFDEF FPC}
  {$MODE DELPHI}{$H+}
{$ENDIF}

{$IFDEF Win32}
  {$DEFINE Windows}
{$ENDIF}

{$IF DEFINED(Windows) or defined(LCLCustomDrawn) or defined(UNIX)}
  {$DEFINE MAGNIFIER_USE_NATIVE_STRETCH}
{$ENDIF}

interface

uses

{$IFDEF Windows}
  {$IFDEF WinCE}
    Windows, Messages,
  {$ELSE}
    Windows, Messages, ShellAPI, jwawinuser, BitmapProcess, BitmapThreads,
  {$ENDIF}
{$ENDIF}

{$IFDEF UNIX}
  Unix, LCLType, BitmapProcess,
{$ENDIF}

  // fcl-image
  fpcanvas, fpimage, fpimgcanv,
  // LCL
  Graphics, Controls, Classes, SysUtils, ExtCtrls, Forms,
  constants, appsettings, translationsvmg, LMessages,
  IntfGraphics, LCLIntf, lazcanvas;

type

  {@@
    viewRect       - The area on the screen which needs to be enlarged.
                     Is completely filled by CalculateViewRect
    drawGlassRect  - The default glass position.
    screenRect     - The size and position of the screen.
  }
  TGlassDrawInfo = packed record
    BorderSize: Integer;
    viewRect, drawGlassRect, screenRect: TRect;
    { Pre-calculated width and height for optimization }
    drawGlassWidth, drawGlassHeight, viewRectWidth, viewRectHeight: Integer;
  end;

  { TGlass }

  TGlass = class(TCustomControl)
  private
    OSVersion: TOSVersion;
    // Bitmaps for the loaded images
    bmpTopLeft, bmpTopRight, bmpBottomLeft, bmpBottomRight: TBitmap;
    bmpTop, bmpLeft, bmpBottom, bmpRight: TBitmap;
    // Used for non-native stretching
    {$IFNDEF MAGNIFIER_USE_NATIVE_STRETCH}
    Image, SecImage: TLazIntfImage;
    TmpBitmap: TBitmap;
    {$IFEND}
    {$IFDEF Windows}
    BitmapThread : TBitmapThread;
    {$IFEND}
    function  LoadImages: Boolean;
  public
    procedure CalculateDrawInfo(DestCanvas: TCanvas; var DrawInfo: TGlassDrawInfo);
    procedure DrawStandardGlass(DestCanvas: TCanvas; DrawInfo: TGlassDrawInfo);
    procedure DrawGraphicalTools(DestCanvas: TCanvas; DrawInfo: TGlassDrawInfo);
    procedure DrawDynamicModeGlass(DestCanvas: TCanvas; DrawInfo: TGlassDrawInfo);
  public
    WindowList: TFPList;
    // Location of the Glass
    GlassTop, GlassLeft, GlassWidth, GlassHeight: Integer;
    // Multimonitor support properties
    XScreen, YScreen, CXScreen, CYScreen: Integer;
    // Bitmaps with size of the display
    bmpDisplay: TBitmap;              { For the original content of the display }
    bmpEnlargedDisplay: TBitmap;      { Temporary image - Only utilized on UNIX}
    bmpOutput: TBitmap;
    Height, Width: Integer;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function  GetDisplayBitmap: Boolean;
    procedure Paint; override;
    procedure EraseBackground(DC: HDC); override;
    procedure WMEraseBkgnd(var Message: TLMEraseBkgnd); message LM_ERASEBKGND;
    class function  LoadBitmap(bmp: TBitmap; NomeDoRecurso: Integer): Boolean;
    function GetCenterPoint(): TPoint;
    // Functions exported to plugins
    function  GetScreenSize(var vXScreen, vYScreen, vCXScreen, vCYScreen: Integer): Boolean; cdecl;
    procedure CalculateViewRect(var viewRect, drawGlassRect: TRect;
      const screenRect: TRect; AMagnification: double); cdecl;
    procedure FixGlassCoordinates(AFixHard: Boolean = False); cdecl;
    // makes inherited events public
    property OnMouseMove;
    property OnClick;
    property OnKeyDown;
    property OnKeyPress;
    property OnMouseWheel;
  end;

var
  vGlass: TGlass;

implementation

{@@
  On different operating systems and different widgetsets
  the behavior of drawing to a rectagle outside the boundaries
  of the paint area are very different. The best way to
  guarantee the same correct behavior everywhere it to
  avoid drawing to ractangles partially outside the paint
  area.

  This function will clip the glassRect rectangle to fit into
  screenRect and calculates a smaller viewRect rectangle that
  represents the area to be enlarged to glassRect to produce
  the screen magnification effect.

  Plugins can access this method.

  @param  viewRect      The area on the screen which needs to be enlarged.
                        Is completely filled by CalculateViewRect
  @param drawGlassRect  Should input the default glass position.
                        Do not pass the glass position variables themselves,
                        but rather a copy of their value, because this function
                        will modify this rectangle to avoid having a glassRect
                        that goes out of the screen.
  @param screenRect     The size and position of the screen.
  @param AMagnification The magnification factor to be applied.
}
procedure TGlass.CalculateViewRect(var viewRect, drawGlassRect: TRect;
  const screenRect: TRect; AMagnification: double); cdecl;
var
  virtGlassTop, virtGlassLeft, virtGlassWidth, virtGlassHeight,
   virtGlassRight, virtGlassBottom: Integer;
begin
  { Initializes helper values }
  virtGlassTop := drawGlassRect.Top;
  virtGlassLeft := drawGlassRect.Left;
  virtGlassWidth := drawGlassRect.Right - drawGlassRect.Left;
  virtGlassHeight := drawGlassRect.Bottom - drawGlassRect.Top;
  virtGlassRight := drawGlassRect.Right;
  virtGlassBottom := drawGlassRect.Bottom;

  { Calculates the initial view area }
  viewRect := Bounds(
   Round(virtGlassLeft + (virtGlassWidth / 2) - (virtGlassWidth) / (2 * AMagnification)),
   Round(virtGlassTop + (virtGlassHeight / 2) - (virtGlassHeight) / (2 * AMagnification)),
   Round(virtGlassWidth / AMagnification),
   Round(virtGlassHeight / AMagnification));

  { Clips the glassRect to fit the screen }
  if drawGlassRect.Left < 0 then drawGlassRect.Left := 0;
  if drawGlassRect.Top < 0 then drawGlassRect.Top := 0;
  if drawGlassRect.Right > screenRect.Right then drawGlassRect.Right := screenRect.Right;
  if drawGlassRect.Bottom > screenRect.Bottom then drawGlassRect.Bottom := screenRect.Bottom;

  { Adjusts the viewRect if the glassRect was clipped }
  if virtGlassLeft <> drawGlassRect.Left then
    viewRect.Left := Round(viewRect.Left - virtGlassLeft / AMagnification);

  if virtGlassTop <> drawGlassRect.Top then
    viewRect.Top := Round(viewRect.Top - virtGlassTop / AMagnification);

  if virtGlassRight <> drawGlassRect.Right then
    viewRect.Right := Round(viewRect.Right - (virtGlassRight - screenRect.Right) / AMagnification);

  if virtGlassBottom <> drawGlassRect.Bottom then
    viewRect.Bottom := Round(viewRect.Bottom - (virtGlassBottom - screenRect.Bottom) / AMagnification);
end;

{@@
  Loads bitmap from an internal resource to a bitmap object
}
class function TGlass.LoadBitmap(bmp: TBitmap; NomeDoRecurso: Integer): Boolean;
var
  buffer: THandle;
  memstream: TMemoryStream;
  FileName: string;
begin
  Result := False;

{$IFDEF Win32}

  bmp.LoadFromResourceID(hInstance, NomeDoRecurso);

{$ENDIF}
{$IFDEF Unix}

  case NomeDoRecurso of
   IDB_TOPLEFT: FileName := 'topleft.bmp';
   IDB_TOPRIGHT: FileName := 'topright.bmp';
   IDB_BOTTOMLEFT: FileName := 'bottomleft.bmp';
   IDB_BOTTOMRIGHT: FileName := 'bottomright.bmp';
   IDB_TOP: FileName := 'top.bmp';
   IDB_LEFT: FileName := 'left.bmp';
   IDB_BOTTOM: FileName := 'bottom.bmp';
   IDB_RIGHT: FileName := 'right.bmp';
   IDB_CECAE: FileName := 'cecae.bmp';
   IDB_FEUSP: FileName := 'feusp.bmp';
   IDB_VMG: FileName := 'vmg.bmp';
   IDB_LUPA: FileName := 'lupa.bmp';
   IDB_USPLEGAL: FileName := 'usplegal.bmp';
  else
    WriteLn('Wrong image name');
    Exit;
  end;

  Write('[TGlass.LoadBitmap] Loading ' + vConfigurations.MyDirectory + FileName);

  try
    bmp.LoadFromFile(vConfigurations.MyDirectory + FileName);
  except
    WriteLn(ErrorLoading);
    Exit;
  end;

  WriteLn('');

{$ENDIF}

  Result := True;
end;

{@@
  Calculates the point in the middle of the center display pixel
}
function TGlass.GetCenterPoint(): TPoint;
begin
  Result.X := GlassLeft + (GlassWidth div 2);
  Result.Y := GlassTop + (GlassHeight div 2);
end;

{@@
  Load bitmaps required by the Magnifying Glass
  This function loads and generates the rectangular glass
}
function TGlass.LoadImages: Boolean;
begin
  Result := True;

  if (not LoadBitmap(bmpTopLeft, IDB_TOPLEFT)) then Result := False;
  bmpTopLeft.Transparent := True;
  bmpTopLeft.TransparentColor := clFuchsia;

  if (not LoadBitmap(bmpTopRight, IDB_TOPRIGHT)) then Result := False;
  bmpTopRight.Transparent := True;
  bmpTopRight.TransparentColor := clFuchsia;

  if (not LoadBitmap(bmpBottomLeft, IDB_BOTTOMLEFT)) then Result := False;
  bmpBottomLeft.Transparent := True;
  bmpBottomLeft.TransparentColor := clFuchsia;

  if (not LoadBitmap(bmpBottomRight, IDB_BOTTOMRIGHT)) then Result := False;
  bmpBottomRight.Transparent := True;
  bmpBottomRight.TransparentColor := clFuchsia;

  if (not LoadBitmap(bmpTop, IDB_TOP)) then Result := False;
  bmpTop.Transparent := True;
  bmpTop.TransparentColor := clFuchsia;

  if (not LoadBitmap(bmpLeft, IDB_LEFT)) then Result := False;
  bmpLeft.Transparent := True;
  bmpLeft.TransparentColor := clFuchsia;

  if (not LoadBitmap(bmpBottom, IDB_BOTTOM)) then Result := False;
  bmpBottom.Transparent := True;
  bmpBottom.TransparentColor := clFuchsia;

  if (not LoadBitmap(bmpRight, IDB_RIGHT)) then Result := False;
  bmpRight.Transparent := True;
  bmpRight.TransparentColor := clFuchsia;

  // Check the sizes
  if ((bmpTopLeft.Width <> bmpTopRight.Width) or
        (bmpTopLeft.Height <> bmpTopRight.Height) or
        (bmpBottomLeft.Width <> bmpBottomRight.Width) or
        (bmpBottomLeft.Height <> bmpBottomRight.Height)) // Sizes must match
        then Result := False;

  // Check the sizes
  if ((bmpLeft.Width <> bmpRight.Width) or
      (bmpLeft.Height <> bmpRight.Height) or
      (bmpTop.Width <> bmpBottom.Width) or
      (bmpTop.Height <> bmpBottom.Height)) // Sizes must match
      then Result := False;

end;

{@@
  Creates a object from the TAplicativo class

  @param  AOwner   The owner of the component (this may be nil)

  @return          A pointer to the newly created object
}
constructor TGlass.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  WindowList := TFPList.Create;
  OSVersion := vConfigurations.GetOSVersion();

  // Create bitmap holding objects
  bmpDisplay := TBitmap.Create;
  bmpEnlargedDisplay := TBitmap.Create;
  bmpOutput := TBitmap.Create;
  bmpTopLeft := TBitmap.Create;
  bmpTopRight := TBitmap.Create;
  bmpBottomLeft := TBitmap.Create;
  bmpBottomRight := TBitmap.Create;
  bmpTop := TBitmap.Create;
  bmpLeft := TBitmap.Create;
  bmpBottom := TBitmap.Create;
  bmpRight := TBitmap.Create;

  // Load bitmaps
  if not LoadImages then
  begin
{$ifdef Unix}
    WriteLn('Error loading bitmaps');
{$endif}
  end;

{$IFNDEF MAGNIFIER_USE_NATIVE_STRETCH}
  Image := TLazIntfImage.Create(0, 0);
  TmpBitmap := TBitmap.Create;
  SecImage := TLazIntfImage.Create(0, 0);
{$IFEND}
end;

{@@
  Destroys a object derived from the TGlass class
}
destructor TGlass.Destroy;
begin
  {$IFNDEF MAGNIFIER_USE_NATIVE_STRETCH}
  TmpBitmap.Free;
  Image.Free;
  SecImage.Free;
  {$IFEND}

  bmpDisplay.Free;
  bmpEnlargedDisplay.Free;
  bmpOutput.Free;
  bmpTopLeft.Free;
  bmpTopRight.Free;
  bmpBottomLeft.Free;
  bmpBottomRight.Free;
  bmpTop.Free;
  bmpLeft.Free;
  bmpBottom.Free;
  bmpRight.Free;

  WindowList.Free;

  inherited Destroy;
end;

{@@

  Makes sure that the glass coordinates don't
  go outside the screen area. Utilized by plugins.

  @param AFixHard   If true, then it won't let the glass leave the screen area at all
                    If false, it will let the glass go up to it's half width and height
                    outside the screen area, just like the standard glass does, to catch
                    the edges with the standard central focus.
}
procedure TGlass.FixGlassCoordinates(AFixHard: Boolean = False); cdecl;
var
  halfWidth, halfHeight: Integer;
begin
  if AFixHard then
  begin
    if vGlass.GlassLeft < 0 then vGlass.GlassLeft := 0;
    if vGlass.GlassTop < 0 then vGlass.GlassTop := 0;
    if vGlass.GlassLeft + vGlass.GlassWidth > vGlass.CXScreen then
      vGlass.GlassLeft := vGlass.CXScreen - vGlass.GlassWidth;
    if vGlass.GlassTop + vGlass.GlassHeight > vGlass.CYScreen then
      vGlass.GlassTop := vGlass.CYScreen - vGlass.GlassHeight;
  end
  else
  begin
    halfWidth := vGlass.GlassWidth div 2;
    halfHeight := vGlass.GlassHeight div 2;
    if vGlass.GlassLeft < -halfWidth then vGlass.GlassLeft := -halfWidth;
    if vGlass.GlassTop < -halfHeight then vGlass.GlassTop := -halfHeight;
    if vGlass.GlassLeft + halfWidth > vGlass.CXScreen then
      vGlass.GlassLeft := vGlass.CXScreen - halfWidth;
    if vGlass.GlassTop + halfHeight > vGlass.CYScreen then
      vGlass.GlassTop := vGlass.CYScreen - halfWidth;
  end;
end;

{@@
  Get the RECT of the desktop, note that the origin
  will have a negative xCoord if a multi-monitor
  system doesn't have the primary monitor on the left
}
function TGlass.GetScreenSize(var vXScreen, vYScreen, vCXScreen, vCYScreen: Integer): Boolean; cdecl;
begin
{$IFDEF Windows}
  vXScreen := 0;
  vYScreen := 0;

{$ifdef wince}
  if (OSVersion >= vwWinCE4) then
{$else}
  if (OSVersion >= vwWin98) then
{$endif}
  begin
    vCXScreen := GetSystemMetrics(SM_CXVIRTUALSCREEN);
    vCYScreen := GetSystemMetrics(SM_CYVIRTUALSCREEN);
    vXScreen := GetSystemMetrics(SM_XVIRTUALSCREEN);
    vYScreen := GetSystemMetrics(SM_YVIRTUALSCREEN);
  end
  else
  begin
    // NOTE:  VIRTUALSCREEN doesn't seem to be supported on win95, nt4
    // And also on Windows CE <= 3.0
    // (according to MSDN).  This will only get the screen size of
    // the primary monitor, and thus, this app won't have multi-
    // monitor support.  It may be possible to get the virtual screen
    // resolution another way.
    vCXScreen := GetSystemMetrics(SM_CXSCREEN);
    vCYScreen := GetSystemMetrics(SM_CYSCREEN);
  end;
{$ENDIF}
{$IFDEF Unix}
  vCXScreen := Screen.Width;
  vCYScreen := Screen.Height;
  vXScreen := 0;
  vYScreen := 0;
{$ENDIF}

  Result := True;
end;

{$IFDEF WINDOWS}
function GetDisplayBitmap_EnumCallback(_para1:HWND; _para2:LPARAM):WINBOOL;stdcall;
begin
  Result := True;
  if not IsWindowVisible(_para1) then Exit;
  if _para1 = vGlass.Parent.Handle then Exit;
  vGlass.WindowList.Add(Pointer(_para1));
end;
{$ENDIF}

{@@
  Get contents of the display
}
function TGlass.GetDisplayBitmap: Boolean;
var
  c: TCanvas;
  srcRect, destRect: TRect;
  ScreenDC: HDC;
  lCurWindow, lVMGWindow: HWND;
  lCurWindowText: array[0..255] of WideChar;
  i: Integer;
  // for Mac OS X
  bmpRetinaDisplay: TBitmap;

  {$IFDEF WINDOWS}
  procedure GetDisplayBitmap_PrintWindowToHDC(AWindow: HWND; ADest: HDC);
  var
    wi: jwawinuser.WINDOWINFO;
    lClientRect, lWindowRect: TRect;
    lBorderX, lBorderY: Integer;
    lWindowDC: HDC;
  begin
    jwawinuser.GetWindowInfo(AWindow, wi);
    lWindowDC := Windows.GetDC(AWindow);

    // Take into account the border difference
    Windows.GetClientRect(AWindow, lClientRect);
    Windows.GetClientRect(AWindow, lWindowRect);
    lBorderX := (lWindowRect.right - lWindowRect.left) - lClientRect.right;
    lBorderY := (lWindowRect.bottom - lWindowRect.top) - lClientRect.bottom;

    Windows.BitBlt(ADest, wi.rcWindow.Left + lBorderX, wi.rcWindow.Top + lBorderY,
      wi.rcWindow.Right - wi.rcWindow.left,
      wi.rcWindow.bottom - wi.rcWindow.top,
      lWindowDC, 0, 0, SRCCOPY);
    Windows.ReleaseDC(AWindow, lWindowDC);
  end;
  {$ENDIF}

begin
  Result := True;

{$IFDEF Windows}

  c := TCanvas.Create;
  try
    try
      c.Handle := GetWindowDC(GetDesktopWindow());

      srcRect := Classes.Bounds(XScreen, YScreen, CXScreen, CYScreen);

      destRect := Classes.Bounds(0, 0, CXScreen, CYScreen);

      {*******************************************************************
      *  Fix for an issue with Windows 98 on StretchBlt
      *******************************************************************}
      if OSVersion >= vwWin2000 then
      begin
        bmpDisplay.Width := CXScreen;
        bmpDisplay.Height := CYScreen;
      end
      else
      begin
        bmpDisplay.Width := CXScreen * 2;
        bmpDisplay.Height := CYScreen * 2;
      end;

      if not vConfigurations.UsePlugins then
      begin
        bmpDisplay.Canvas.CopyRect(destRect, c, srcRect);
      end
      {*******************************************************************
      *  Dynamic mode screenshot improvement by drawing all windows in an iteration
      *******************************************************************}
      else
      begin
        lVMGWindow := Parent.Handle;
        //WriteLn('===============================');
        WindowList.Clear;
        Windows.EnumWindows(@GetDisplayBitmap_EnumCallback, bmpDisplay.Canvas.Handle);

        for i := 0 to WindowList.Count-1 do
        begin
          lCurWindow := HWND(WindowList.Items[WindowList.Count-1-i]);
          //PrintWindow(lCurWindow, bmpDisplay.Canvas.Handle, 0);
          GetDisplayBitmap_PrintWindowToHDC(lCurWindow, bmpDisplay.Canvas.Handle);
          GetWindowTextW(lCurWindow, lCurWindowText, 255);
          //WriteLn('Printing window _para1='+IntToHex(lCurWindow, 8)+' title='+lCurWindowText);
        end;
      end;
    finally
      ReleaseDC(0, c.Handle);
      c.Free;
    end;
  except
    Result := False;
    Exit;
  end;

{$ENDIF}
{$IFDEF Unix}

  ScreenDC := GetDC(0);

  {$ifdef Darwin}
  bmpRetinaDisplay := TBitmap.Create;
  bmpRetinaDisplay.LoadFromDevice(ScreenDC);
  bmpDisplay.Width := CXScreen;
  bmpDisplay.Height := CYScreen;
  StretchBlt(bmpDisplay.Canvas.Handle, 0, 0, CXScreen, CYScreen,
    bmpRetinaDisplay.Canvas.Handle, 0, 0, bmpRetinaDisplay.Width, bmpRetinaDisplay.Height,
    SRCCOPY);
  bmpRetinaDisplay.Free;
  {$else}
  bmpDisplay.LoadFromDevice(ScreenDC);
  {$endif}

  ReleaseDC(0, ScreenDC);

{$ENDIF}
end;

{@@

  @param  DestCanvas  The canvas where it will be drawn
  @param  DrawInfo    Record with drawing position information
}
procedure TGlass.CalculateDrawInfo(DestCanvas: TCanvas; var DrawInfo: TGlassDrawInfo);
begin
  with DrawInfo do
  begin
  if vConfigurations.graphicalBorder and
   not vConfigurations.UsePlugins then BorderSize := 10
  else BorderSize := 2;

  {*******************************************************************
  *  Calculates the rectangle (ScreenRect) to be magnified to fit ClientRect
  *  For example 4x magnification means that 1/4 of the picture is enlarged
  * to fill the whole picture
  *******************************************************************}
  screenRect.Left := XScreen;
  screenRect.Top := YScreen;
  screenRect.Right := CXScreen;
  screenRect.Bottom := CYScreen;

  {*******************************************************************
  *  Calculates the necessary rectangles and allows CalculateViewRect
  *  to return the drawing coordinates
  *******************************************************************}
  drawGlassRect.Left := GlassLeft + BorderSize;
  drawGlassRect.Top := GlassTop + BorderSize;
  drawGlassRect.Right := GlassLeft + GlassWidth - BorderSize;
  drawGlassRect.Bottom := GlassTop + GlassHeight - BorderSize;

  CalculateViewRect(viewRect, drawGlassRect, screenRect, vConfigurations.iMagnification);

  { Pre-Calculates the Width and Height of the rectangles to simplify the drawing code }
  drawGlassWidth := drawGlassRect.Right - drawGlassRect.Left;
  drawGlassHeight := drawGlassRect.Bottom - drawGlassRect.Top;

  viewRectWidth := viewRect.Right - viewRect.Left;
  viewRectHeight := viewRect.Bottom - viewRect.Top;
  end; // with
end;

{@@
  Draws the glass to a Canvas.
  There are 3 layers to be painted:

  1 - The screen outside the corners. Exists only when
      graphicalBorder is True;
  2 - The magnified screen.
  3 - The Border, either transparent or not;

  @param  DestCanvas  The canvas where it will be drawn
  @param  DrawInfo    Record with drawing position information
}
procedure TGlass.DrawStandardGlass(DestCanvas: TCanvas; DrawInfo: TGlassDrawInfo);
var
  I: Integer;
  dwROP: Cardinal;
  ImageCanvas: TFPImageCanvas = nil;
{$IFDEF MAGNIFIER_USE_NATIVE_STRETCH}
  fm: Single;
  inBitmap, outBitmap : TBitmap;
  drawBitMap : ^TBitmap;
{$IFEND}
begin
  with DrawInfo do
  begin
  {*******************************************************************
  *  Color invertion
  *******************************************************************}
  if vConfigurations.invertColors then dwROP := SRCINVERT
  else dwROP := SRCCOPY;

  {*******************************************************************
  *  The part outside the glass shows the background as it is (1:1 magnification)
  *  In MacBook with Retina display we need to make sure the image is scaled
  *  to fit the screen
  *******************************************************************}
  DestCanvas.Draw(0, 0, bmpDisplay);

  {*******************************************************************
  *  Draws the enlarged glass contents
  *******************************************************************}
{$IFDEF MAGNIFIER_USE_NATIVE_STRETCH}

  fm := vConfigurations.iMagnification;

  inBitmap := TBitmap.Create;
  inBitmap.Width := viewRectWidth;
  inBitmap.Height := viewRectHeight;
  inBitmap.Canvas.CopyRect(Bounds(0, 0, viewRectWidth, viewRectHeight), bmpDisplay.Canvas, viewRect);

  outBitmap := TBitmap.Create;
  outBitmap.Width := Round(viewRectWidth * fm);
  outBitmap.Height := Round(viewRectHeight * fm);

  if vConfigurations.invertColors then
    RawInvertColors(inBitmap);
{$IFDEF Windows}
  BitmapThread := TBitmapThread.Create;
  try
    BitmapThread.FBitmap := @inBitmap;
    BitmapThread.RBitmap := @outBitmap;
    BitmapThread.FM := fm;
    BitmapThread.Contrast := vConfigurations.Contrast;
    if vConfigurations.AntiAliasing then
      if vConfigurations.ExtraAntiAliasing then
        BitmapThread.PAction := CatmullRomAction
      else
        BitmapThread.PAction := BilinearAction
    else
      BitmapThread.PAction := PixelRepetitionAction;
    BitmapThread.Perform;
  finally
    BitmapThread.Free;
  end;
{$ENDIF}
{$IFDEF UNIX}
  if vConfigurations.AntiAliasing then
    if vConfigurations.ExtraAntiAliasing then
      RawCatmullRom(@InBitmap, @OutBitmap, FM, vConfigurations.Contrast)
    else
      RawBilinear(@InBitmap, @OutBitmap, FM, vConfigurations.Contrast)
  else
    RawPixelRepetition(@InBitmap, @OutBitmap, FM, vConfigurations.Contrast);
{$ENDIF}
  drawBitMap := @outBitmap;
  DestCanvas.Draw(drawGlassRect.Left, drawGlassRect.Top, drawBitMap^);
  inBitmap.Free;
  outBitmap.Free;
{$ELSE}

  // First we need to load the TLazIntfImage from a dummy bitmap to initialize it
  TmpBitmap.Width := drawGlassWidth;
  TmpBitmap.Height := drawGlassHeight;
  Image.LoadFromBitmap(TmpBitmap.Handle, 0);

  // Now build a Canvas for our destination image
  ImageCanvas := TFPImageCanvas.create(Image);
  if vConfigurations.AntiAliasing then
    ImageCanvas.Interpolation := TMitchelInterpolation.Create
  else
    ImageCanvas.Interpolation := TFPSharpInterpolation.Create;

  // Now Copy a section of the display
  bmpEnlargedDisplay.Height := viewRectHeight;
  bmpEnlargedDisplay.Width := viewRectWidth;
  bmpEnlargedDisplay.Canvas.CopyRect(
   Bounds(0, 0, viewRectWidth, viewRectHeight),
   bmpDisplay.Canvas, viewRect);
  SecImage.LoadFromBitmap(bmpEnlargedDisplay.Handle, 0);

  // Enlarge this section of the display
  ImageCanvas.StretchDraw(
    0, 0, drawGlassWidth, drawGlassHeight,
    SecImage);

  // And draw the result to our Canvas via a temporary bitmap
  TmpBitmap.LoadFromIntfImage(Image);
  DestCanvas.Draw(drawGlassRect.Left, drawGlassRect.Top, TmpBitmap);

  ImageCanvas.Interpolation.Free;
  ImageCanvas.Interpolation := nil;
  ImageCanvas.Free;
{$IFEND}

  {*******************************************************************
  *  Draws the graphical border
  *******************************************************************}
  if vConfigurations.graphicalBorder then
  begin
    // Corners
    DestCanvas.Draw(GlassLeft, GlassTop, bmpTopLeft);
    DestCanvas.Draw(GlassLeft + GlassWidth - 32, GlassTop, bmpTopRight);
    DestCanvas.Draw(GlassLeft, GlassTop + GlassHeight - 32, bmpBottomLeft);
    DestCanvas.Draw(GlassLeft + GlassWidth - 32, GlassTop + GlassHeight - 32, bmpBottomRight);

    // Sides
    for i := 1 to vConfigurations.xSquare - 2 do
     DestCanvas.Draw(GlassLeft + 32 * i, GlassTop, bmpTop);
    for i := 1 to vConfigurations.xSquare - 2 do
     DestCanvas.Draw(GlassLeft + 32 * i, GlassTop + GlassHeight - 32, bmpBottom);
    for i := 1 to vConfigurations.ySquare - 2 do
     DestCanvas.Draw(GlassLeft, GlassTop + 32 * i, bmpLeft);
    for i := 1 to vConfigurations.ySquare - 2 do
     DestCanvas.Draw(GlassLeft + GlassWidth - 32, GlassTop + 32 * i, bmpRight);
  end
  else
  {*******************************************************************
  *  Draws the rectangular border
  *******************************************************************}
  begin
    DestCanvas.Brush.Color := clBlack;
    DestCanvas.FrameRect(Bounds(GlassLeft, GlassTop, GlassWidth, GlassHeight));
    DestCanvas.FrameRect(Bounds(GlassLeft + 1, GlassTop + 1, GlassWidth - 2, GlassHeight - 2));
    DestCanvas.Brush.Color := clGray;
    DestCanvas.FrameRect(Bounds(GlassLeft + 2, GlassTop + 2, GlassWidth - 4, GlassHeight - 4));
    DestCanvas.FrameRect(Bounds(GlassLeft + 3, GlassTop + 3, GlassWidth - 6, GlassHeight - 6));
  end;
  end; // with
end;

{@@
  Graphical Tools
}
//  The commented parts are code from 2.35 version not merged here yet
procedure TGlass.DrawGraphicalTools(DestCanvas: TCanvas; DrawInfo: TGlassDrawInfo);
var
  i, MinWidth, MinHeight: Integer;
  GlassMinusBorderLeft, GlassMinusBorderTop: Integer;
  CentralColor: TColor;
  centerPt: TPoint;
  { Tickmarks }
  step, stepMinor, tickSize, tickCount: Integer;
  R, G, B: Byte;
begin
  with DrawInfo do
  begin
  if (vConfigurations.showTools) then
  begin
    { Pre-calculates some coordinates to simplify the code }
    GlassMinusBorderLeft := drawGlassRect.Left - BorderSize;
    GlassMinusBorderTop := drawGlassRect.Top - BorderSize;

    // Standard properties of the Canvas
    DestCanvas.Brush.Color := clGray;
    DestCanvas.Brush.Style := bsSolid;
    DestCanvas.Font.Color := clWhite;
    DestCanvas.Font.Style := [fsBold];
    DestCanvas.Font.Name := 'Arial';
    DestCanvas.Font.Size := 10;

    // It's better to draw the background ourselves
    DestCanvas.Rectangle(
      GlassMinusBorderLeft + 10,
      GlassMinusBorderTop + 10,
      GlassMinusBorderLeft + 150,
      GlassMinusBorderTop + 80
    );

    // Gets the RGB value of the center pixel
    CenterPt := GetCenterPoint();
    {$ifdef LCLCocoa}
    CentralColor := bmpDisplay.Canvas.Pixels[centerPt.X, centerPt.Y];
    {$else}
    CentralColor := DestCanvas.Pixels[centerPt.X, centerPt.Y];
    {$endif}

    // Draws the color in decimal
    {$ifdef fpc}
    R := Red(CentralColor);
    G := Green(CentralColor);
    B := Blue(CentralColor);
    {$else}
    R := GetRValue(ColorToRGB(CentralColor));
    G := GetGValue(ColorToRGB(CentralColor));
    B := GetBValue(ColorToRGB(CentralColor));
    {$endif}
    DestCanvas.TextOut(GlassMinusBorderLeft + 10, GlassMinusBorderTop + 10,
     Format('%.3d, %.3d, %.3d RGB', [R, G, B]));

    // Draws the color in hexadecimal
    DestCanvas.TextOut(GlassMinusBorderLeft + 10, GlassMinusBorderTop + 30,
     Format('%.2x, %.2x, %.2x RGB %.1fX', [R, G, B,
     vConfigurations.iMagnification]));

    // Draws the location of the central pixel
    DestCanvas.TextOut(GlassMinusBorderLeft + 10, GlassMinusBorderTop + 50,
     Format(vTranslations.lpGTMouse + lpSpace + vTranslations.lpGTX + lpSpace +
     '%d' + lpSpace + vTranslations.lpGTY + lpSpace + '%d',
     [Mouse.CursorPos.X, Mouse.CursorPos.Y]));

    // Tickmarks
//    centerOffset := 1;
//    lineOffset := Round( (vConfigurations.iMagnification / 2) + 0.50);

//    HPEN pen = CreatePen(PS_DOT, 1, 0x000000);
//    HPEN oldPen = (HPEN)SelectObject(bmpMasked1.hdcMem, pen);

    if (vConfigurations.iMagnification >= 4.0) then
    begin
      step := Round(5 * vConfigurations.iMagnification);
      stepMinor := Round(vConfigurations.iMagnification);
      tickSize := step;

{      // Right side ticks
      tickCount := GlassWidth div (2 * step);
      for i := 1 to tickCount do
      begin
        DestCanvas.MoveTo(centerX + i * step, centerY - tickSize);
        DestCanvas.LineTo(centerX + i * step, centerY + tickSize);
        // Alternates large and small ticks
        if i mod 2 = 0 then tickSize := tickSize * 2
        else tickSize := tickSize div 2;
      end

      i := centerX + stepMinor;
      while (i < GlassLeft + GlassWidth - 17) do
      begin
        if (i - (centerX + step)) mod step <> 0 then tickScale := 2
        else tickScale := 1;
        DestCanvas.MoveTo(i, centerY - (tickScale * lineOffset));
        DestCanvas.LineTo(i, centerY + (tickScale * lineOffset) + 1);
        i := i + stepMinor;
      end;

      i := centerX - stepMinor;
      for(int i = centerX - stepMinor; i > 17; i -= stepMinor)
      begin
          tickScale = (i - (centerX - step)) % step ? 1 : 2;
          drawLine(bmpMasked1.hdcMem, i, centerY - (tickScale * lineOffset), i, centerY + (tickScale * lineOffset) + 1);
      end;
      for(int i = centerY + stepMinor; i < cyGlass - 17; i += stepMinor)
      begin
          tickScale = (i - (centerY + step)) % step ? 1 : 2;
          drawLine(bmpMasked1.hdcMem, centerX - (tickScale * lineOffset), i, centerX + (tickScale * lineOffset) + 1, i);
      end;
      for(int i = centerY - stepMinor; i > 17; i -= stepMinor)
      begin
          tickScale = (i - (centerY + step)) % step ? 1 : 2;
          drawLine(bmpMasked1.hdcMem, centerX - (tickScale * lineOffset), i, centerX + (tickScale * lineOffset) + 1, i);
      end;}
    end;

    // Center crosshair

    // Top
    DestCanvas.MoveTo(GlassLeft + GlassWidth div 2, GlassTop + BorderSize);
    DestCanvas.LineTo(GlassLeft + GlassWidth div 2, GlassTop + GlassHeight div 2 - 1);

    // Bottom
    DestCanvas.MoveTo(GlassLeft + GlassWidth div 2, GlassTop + GlassHeight - BorderSize);
    DestCanvas.LineTo(GlassLeft + GlassWidth div 2, GlassTop + GlassHeight div 2 + 1);

    // Left
    DestCanvas.MoveTo(GlassLeft + BorderSize, GlassTop + GlassHeight div 2);
    DestCanvas.LineTo(GlassLeft + GlassWidth div 2 - 1, GlassTop + GlassHeight div 2);

    // Right
    DestCanvas.MoveTo(GlassLeft + GlassWidth - BorderSize, GlassTop + GlassHeight div 2);
    DestCanvas.LineTo(GlassLeft + GlassWidth div 2 + 1, GlassTop + GlassHeight div 2);
  end;
  end; // with
end;

{@@
  Draws the Glass for the Dynamic Mode

  @param  DestCanvas  The canvas where it will be drawn
  @param  DrawInfo    Record with drawing position information
}
procedure TGlass.DrawDynamicModeGlass(DestCanvas: TCanvas;
  DrawInfo: TGlassDrawInfo);
var
  I: Integer;
  MagMousePt, centerPt: TPoint;
begin
  with DrawInfo do
  begin
  DestCanvas.Draw(0, 0, bmpDisplay);

  {*******************************************************************
  *  Draws the enlarged glass contents
  *******************************************************************}
  StretchBlt(DestCanvas.Handle,
   drawGlassRect.Left - GlassLeft, drawGlassRect.Top - GlassTop,
   drawGlassWidth, drawGlassHeight,
   bmpDisplay.Canvas.Handle, viewRect.Left, viewRect.Top,
   viewRectWidth, viewRectHeight, SRCCOPY);

  {*******************************************************************
  *  Draws the rectangular border
  *******************************************************************}
  DestCanvas.Brush.Color := clBlack;
  DestCanvas.FrameRect(Bounds(0, 0, GlassWidth, GlassHeight));
  DestCanvas.FrameRect(Bounds(1, 1, GlassWidth - 2, GlassHeight - 2));
  DestCanvas.Brush.Color := clGray;
  DestCanvas.FrameRect(Bounds(2, 2, GlassWidth - 4, GlassHeight - 4));
  DestCanvas.FrameRect(Bounds(3, 3, GlassWidth - 6, GlassHeight - 6));

  {*******************************************************************
  *  If the mouse is inside the window, draw the mouse too
  *******************************************************************}
  if PtInRect(Bounds(GlassLeft, GlassTop, GlassWidth, GlassHeight), Mouse.CursorPos) then
  begin
    centerPt.X := GlassLeft + GlassWidth div 2;
    centerPt.Y := GlassTop + GlassHeight div 2;
    // Mouse position in the magnifier window in client coordinates
    MagMousePt.X := Round((Mouse.CursorPos.X - centerPt.X)
      * vConfigurations.iMagnification + GlassWidth div 2);
    MagMousePt.Y := Round((Mouse.CursorPos.Y - centerPt.Y)
      * vConfigurations.iMagnification + GlassHeight div 2);
    // Hack fix for the new dynamic mode =( ToDo: Figure out a proper fix
    MagMousePt.Y := MagMousePt.Y - 70;
    MagMousePt.X := MagMousePt.X - 20;

    DestCanvas.Pen.Color := clBlack;
    DestCanvas.Pen.Width := 3;
    DestCanvas.Line(MagMousePt.X - 20, MagMousePt.Y, MagMousePt.X + 20, MagMousePt.Y);
    DestCanvas.Line(MagMousePt.X, MagMousePt.Y - 20, MagMousePt.X, MagMousePt.Y + 20);
  end;

  end; // with
end;

{@@
  Processes Paint messages for the TGlass control
}
procedure TGlass.Paint;
var
  DrawInfo: TGlassDrawInfo;
begin
  // Loads the AntiAliasing configuration
  if vConfigurations.AntiAliasing then
    Canvas.AntialiasingMode := amOn
  else Canvas.AntialiasingMode := amOff;

  {$if defined(Unix) and not defined(Darwin)}
    // Cleans the bitmap to solve a bug on some systems
    bmpOutput.Canvas.Brush.Color := clWhite;
    bmpOutput.Canvas.FillRect(Bounds(0, 0, CXScreen, CYScreen));
  {$ifend}

  // Ask for update of the whole window
  CalculateDrawInfo(bmpOutput.Canvas, DrawInfo);
  if vConfigurations.UsePlugins then
  begin
    DrawDynamicModeGlass(bmpOutput.Canvas, DrawInfo);
  end
  else
  begin
    DrawStandardGlass(bmpOutput.Canvas, DrawInfo);
    DrawGraphicalTools(bmpOutput.Canvas, DrawInfo);
  end;

  // Copies the buffer bitmap to the canvas
  Canvas.Draw(0, 0, bmpOutput);

  inherited Paint;
end;

procedure TGlass.EraseBackground(DC: HDC);
begin

end;

procedure TGlass.WMEraseBkgnd(var Message: TLMEraseBkgnd);
begin
  Message.Result := 1;
end;

end.