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