Browse Source

BGRADialogs Added Test to show under Windows

Massimo Magnano 10 months ago
parent
commit
695465d989

+ 92 - 2
bgradialogs.pas

@@ -8,9 +8,16 @@ unit BGRADialogs;
 
 {$mode objfpc}{$H+}
 
+{$ifdef WINDOWS}
+  //{$define Show_PreviewControl}  //THIS IS JUST FOR TESTING, It is not recommended for now under Windows
+{$endif}
+
 interface
 
 uses
+  {$ifdef Show_PreviewControl}
+  Windows, Graphics,
+  {$endif}
   Classes, SysUtils, ExtDlgs, Controls, StdCtrls, ExtCtrls,
   BGRABitmapTypes, BCRoundedImage;
 
@@ -18,6 +25,9 @@ resourcestring
   rsSelectAPreviewFile = 'Select the File to preview';
 
 type
+
+  { TBGRAOpenPictureDialog }
+
   TBGRAOpenPictureDialog = class(TPreviewFileDialog)
    private
     FDefaultFilter: string;
@@ -27,12 +37,22 @@ type
     FPreviewFilename: string;
 
   protected
+    {$ifdef Show_PreviewControl}
+    DialogWnd,
+    pParentWnd, pBrotherWnd : HWnd;
+    {$endif}
+
     class procedure WSRegisterClass; override;
     function  IsFilterStored: Boolean; virtual;
     procedure InitPreviewControl; override;
     procedure ClearPreview; virtual;
     procedure UpdatePreview; virtual;
 
+    {$ifdef Show_PreviewControl}
+    procedure GetDialogWnd;
+    procedure ResizePreviewControl;
+    {$endif}
+
     property ImageCtrl: TBCRoundedImage read FImageCtrl;
     property PicturePanel: TPanel read FPicturePanel;
     property PictureDetails: TLabel read FPictureDetails;
@@ -69,7 +89,8 @@ procedure Register;
 
 implementation
 
-uses WSExtDlgs, Masks, FileUtil, LazFileUtils, LCLStrConsts, LCLType;
+uses
+  WSExtDlgs, Masks, FileUtil, LazFileUtils, LCLStrConsts, LCLType;
 
 function GetBGRAFormatFilter(AFormat: TBGRAImageFormat): String;
 begin
@@ -144,8 +165,8 @@ end;
 
 procedure TBGRAOpenPictureDialog.DoClose;
 begin
-  ClearPreview;
   inherited DoClose;
+//  PreviewFileControl.ParentWindow:=0;
 end;
 
 procedure TBGRAOpenPictureDialog.DoSelectionChange;
@@ -197,7 +218,13 @@ procedure TBGRAOpenPictureDialog.UpdatePreview;
 var
   CurFilename: String;
   FileIsValid: boolean;
+
 begin
+  {$ifdef Show_PreviewControl}
+  if (DialogWnd = 0) then GetDialogWnd;
+  ResizePreviewControl;
+  {$endif}
+
   FPicturePanel.Caption:= '';
   FPictureDetails.Caption:='';
 
@@ -221,6 +248,63 @@ begin
   if not FileIsValid then ClearPreview;
 end;
 
+{$ifdef Show_PreviewControl}
+procedure TBGRAOpenPictureDialog.GetDialogWnd;
+var
+  pHandle: HWND;
+  thID, prID, appID:DWord;
+
+begin
+  pBrotherWnd:= 0;
+  pParentWnd:= 0;
+
+  //LCL doesn't pass us the Dialog Handle, so we have to look for it the old fashioned way
+  appID:= GetProcessId;
+  repeat
+    DialogWnd:= FindWindowEx(0, DialogWnd, PChar('#32770'), nil);
+    thID:= GetWindowThreadProcessId(DialogWnd, prID);
+  until (DialogWnd=0) or (prID = appID);
+
+  //Get Parent and Brother Control
+  //  this depends on the OS and needs to be tested as much as possible (for now it works with Windows 10)
+  if (DialogWnd<>0) then
+  begin
+    pHandle:= FindWindowEx(DialogWnd, 0, PChar('DUIViewWndClassName'), nil);
+    if (pHandle<>0) then  //Windows 10
+    begin
+      pParentWnd:= FindWindowEx(pHandle, 0, PChar('DirectUIHWND'), nil);
+      if (pParentWnd<>0) then
+      begin
+        repeat
+          pBrotherWnd:= FindWindowEx(pParentWnd, pBrotherWnd, PChar('CtrlNotifySink'), nil);
+          pHandle:= FindWindowEx(pBrotherWnd, 0, PChar('SHELLDLL_DefView'), nil);
+        until (pBrotherWnd=0) or (pHandle<>0);
+
+        if (pBrotherWnd<>0) and (pHandle<>0) then PreviewFileControl.ParentWindow:=pParentWnd;
+      end;
+    end;
+  end;
+end;
+
+procedure TBGRAOpenPictureDialog.ResizePreviewControl;
+var
+  rectParent, rectBrother: TRect;
+
+begin
+  if (DialogWnd<>0) and (pParentWnd<>0) and (pBrotherWnd<>0) then
+  begin
+    if GetClientRect(pParentWnd, rectParent) and GetWindowRect(pBrotherWnd, rectBrother) then
+    begin
+      ScreenToClient(pParentWnd, rectBrother.TopLeft);
+      ScreenToClient(pParentWnd, rectBrother.BottomRight);
+      PreviewFileControl.SetBounds(rectBrother.Left+4+rectBrother.Width, rectBrother.Top+4,
+                                   rectParent.Right-rectBrother.Right-8,
+                                   rectParent.Bottom-rectBrother.Top-8);
+    end;
+  end;
+end;
+{$endif}
+
 constructor TBGRAOpenPictureDialog.Create(TheOwner: TComponent);
 begin
   inherited Create(TheOwner);
@@ -228,6 +312,12 @@ begin
                     Format(rsAllFiles,[GetAllFilesMask, GetAllFilesMask,'']);
   Filter:=FDefaultFilter;
 
+  {$ifdef Show_PreviewControl}
+  DialogWnd:= 0;
+  pBrotherWnd:= 0;
+  pParentWnd:= 0;
+  {$endif}
+
   FPicturePanel:=TPanel.Create(Self);
   with FPicturePanel do begin
     Name:='FPicturePanel';

+ 5 - 0
test/test_bcroundedimage_pictdialogs/test_bcroundedimage_pictdialogs.lpi

@@ -56,6 +56,11 @@
       <IncludeFiles Value="$(ProjOutDir)"/>
       <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
     </SearchPaths>
+    <CodeGeneration>
+      <Optimizations>
+        <OptimizationLevel Value="0"/>
+      </Optimizations>
+    </CodeGeneration>
     <Linking>
       <Debugging>
         <DebugInfoType Value="dsDwarf3"/>

+ 12 - 12
test/test_bcroundedimage_pictdialogs/test_bcroundedimage_pictdialogs_main.lfm

@@ -22,7 +22,7 @@ object Form1: TForm1
     ChildSizing.Layout = cclLeftToRightThenTopToBottom
     ChildSizing.ControlsPerLine = 1
     ClientHeight = 60
-    ClientWidth = 166
+    ClientWidth = 164
     ItemIndex = 2
     Items.Strings = (
       'Circle'
@@ -35,14 +35,14 @@ object Form1: TForm1
   end
   object Label1: TLabel
     Left = 320
-    Height = 19
+    Height = 15
     Top = 219
-    Width = 62
+    Width = 55
     Caption = 'Rounding:'
   end
   object edRounding: TFloatSpinEdit
     Left = 384
-    Height = 29
+    Height = 23
     Top = 216
     Width = 56
     MaxValue = 100
@@ -61,9 +61,9 @@ object Form1: TForm1
   end
   object cbProportional: TCheckBox
     Left = 320
-    Height = 21
+    Height = 19
     Top = 32
-    Width = 100
+    Width = 84
     Caption = 'Proportional'
     TabOrder = 3
     OnChange = cbProportionalChange
@@ -104,7 +104,7 @@ object Form1: TForm1
     ChildSizing.Layout = cclLeftToRightThenTopToBottom
     ChildSizing.ControlsPerLine = 1
     ClientHeight = 60
-    ClientWidth = 77
+    ClientWidth = 75
     ItemIndex = 2
     Items.Strings = (
       'Left'
@@ -129,7 +129,7 @@ object Form1: TForm1
     ChildSizing.Layout = cclLeftToRightThenTopToBottom
     ChildSizing.ControlsPerLine = 1
     ClientHeight = 61
-    ClientWidth = 77
+    ClientWidth = 75
     ItemIndex = 1
     Items.Strings = (
       'Top'
@@ -141,9 +141,9 @@ object Form1: TForm1
   end
   object cbStretch: TCheckBox
     Left = 320
-    Height = 21
+    Height = 19
     Top = 8
-    Width = 67
+    Width = 55
     Caption = 'Stretch'
     Checked = True
     State = cbChecked
@@ -179,9 +179,9 @@ object Form1: TForm1
   end
   object lbDetails: TLabel
     Left = 320
-    Height = 19
+    Height = 15
     Top = 304
-    Width = 40
+    Width = 36
     Caption = 'image:'
   end
   object Button1: TButton