Program.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  using System.Globalization;
 5  using System.Windows.Threading;
 6  
 7  using Common.UI;
 8  using Microsoft.PowerToys.Telemetry;
 9  using PowerToys.Interop;
10  
11  namespace Microsoft.PowerToys.PreviewHandler.Pdf
12  {
13      internal static class Program
14      {
15          private static CancellationTokenSource _tokenSource = new CancellationTokenSource();
16  
17          private static PdfPreviewHandlerControl _previewHandlerControl;
18  
19          /// <summary>
20          ///  The main entry point for the application.
21          /// </summary>
22          [STAThread]
23          public static void Main(string[] args)
24          {
25              ApplicationConfiguration.Initialize();
26              if (args != null)
27              {
28                  if (args.Length == 6)
29                  {
30                      ETWTrace etwTrace = new ETWTrace(Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "AppData", "LocalLow", "Microsoft", "PowerToys", "etw"));
31  
32                      string filePath = args[0];
33                      IntPtr hwnd = IntPtr.Parse(args[1], NumberStyles.HexNumber, CultureInfo.InvariantCulture);
34  
35                      int left = Convert.ToInt32(args[2], 10);
36                      int right = Convert.ToInt32(args[3], 10);
37                      int top = Convert.ToInt32(args[4], 10);
38                      int bottom = Convert.ToInt32(args[5], 10);
39                      Rectangle s = new Rectangle(left, top, right - left, bottom - top);
40  
41                      _previewHandlerControl = new PdfPreviewHandlerControl();
42  
43                      if (!_previewHandlerControl.SetWindow(hwnd, s))
44                      {
45                          return;
46                      }
47  
48                      _previewHandlerControl.DoPreview(filePath);
49  
50                      NativeEventWaiter.WaitForEventLoop(
51                          Constants.PdfPreviewResizeEvent(),
52                          () =>
53                          {
54                              Rectangle s = default;
55                              if (!_previewHandlerControl.SetRect(s))
56                              {
57                                  etwTrace?.Dispose();
58  
59                                  // When the parent HWND became invalid, the application won't respond to Application.Exit().
60                                  Environment.Exit(0);
61                              }
62                          },
63                          Dispatcher.CurrentDispatcher,
64                          _tokenSource.Token);
65  
66                      etwTrace?.Dispose();
67                  }
68                  else
69                  {
70                      MessageBox.Show("Wrong number of args: " + args.Length.ToString(CultureInfo.InvariantCulture));
71                  }
72              }
73  
74              // To customize application configuration such as set high DPI settings or default font,
75              // see https://aka.ms/applicationconfiguration.
76              Application.Run();
77          }
78      }
79  }