Example #1
0
        private void SaveLayout()
        {
            Log.FunctionIndent("MainWindow", "SaveLayout");
            try
            {
                //Config.local.openworkflows = workflows.ToArray();
                var pos = new System.Drawing.Rectangle((int)Left, (int)Top, (int)Width, (int)Height);
                if (pos.Left > 0 && pos.Top > 0 && pos.Width > 100 && pos.Height > 100)
                {
                    var newpos = pos.ToString();
                    var oldpos = Config.local.mainwindow_position.ToString();
                    if (newpos != oldpos)
                    {
                        Config.local.mainwindow_position = pos;
                        Config.Save();
                    }
                }

                try
                {
                    var serializer = new Xceed.Wpf.AvalonDock.Layout.Serialization.XmlLayoutSerializer(DManager);
                    using (var stream = new System.IO.StreamWriter(System.IO.Path.Combine(Interfaces.Extensions.ProjectsDirectory, "layoutagent.config")))
                        serializer.Serialize(stream);
                }
                catch (Exception ex)
                {
                    Log.Error(ex.ToString());
                }
            }
            catch (Exception)
            {
                throw;
            }
            Log.FunctionOutdent("MainWindow", "SaveLayout");
        }
        /// <summary>
        /// Show our Preview user control in the little window provided by the Screen Saver control panel.
        /// </summary>
        /// <param name="hWnd">The hWnd passed to us by the Screen Saver control panel.</param>
        private void ShowPreview(IntPtr hWnd)
        {
            Log("ShowPreview(): Entered.");
            Log("   ShowPreview(): cpl hWnd passed to us = " + hWnd);

            if (NativeMethods.IsWindow(hWnd))
            {
                // Get the rect of the desired parent
                int error = 0;
                System.Drawing.Rectangle ParentRect = new System.Drawing.Rectangle();
                NativeMethods.SetLastErrorEx(0, 0);
                Log("  ShowPreview(): Let's Get the ClientRect of that puppy:");
                Log("  ShowPreview(): Calling GetClientRect(" + hWnd + ")...");
                bool fSuccess = NativeMethods.GetClientRect(hWnd, ref ParentRect);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Log("  ShowPreview(): GetClientRect() returned bool = " + fSuccess + ", rect = " + ParentRect.ToString());
                Log("      GetLastError() returned: " + error.ToString());
                Log(" ");

                // Create the HwndSource which will host our Preview user control
                Log("  ShowPreview(): Let's build a new HwndSource (src), and attach it to the cpl window:");
                HwndSourceParameters parameters = new HwndSourceParameters();
                parameters.WindowStyle = NativeMethods.WindowStyles.WS_CHILD | NativeMethods.WindowStyles.WS_VISIBLE;
                parameters.SetPosition(0, 0);  // in theory, our child will use values relative to parents position
                parameters.SetSize(ParentRect.Width, ParentRect.Height);
                parameters.ParentWindow = hWnd;
                HwndSource src = new HwndSource(parameters);

            #if DEBUG
                // Let's see what Windows thinks
                Log("  ShowPreview(): Attached it. Let's see what Windows thinks:");
                Log("  ShowPreview(): Calling GetParent(src.Handle)...");
                NativeMethods.SetLastErrorEx(0, 0);
                IntPtr handle = IntPtr.Zero;
                handle = NativeMethods.GetParent(src.Handle);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Log("  ShowPreview(): GetParent() returned: " + handle.ToString());
                Log("      GetLastError() returned: " + error.ToString());
                Log(" ");
            #endif

                // Create the user control and attach it
                Log("  ShowPreview(): Creating the user control (Preview)");
                PreviewControl Preview = new PreviewControl();
                Log("  ShowPreview(): setting src.RootVisual to user control...");
                src.RootVisual = Preview;
                Preview.Visibility = Visibility.Visible;

            #if DEBUG
                // Let's find out what Windows thinks
                Log("  ShowPreview(): Set it. Let's see what Windows thinks the HwndSource for Preview is:");
                HwndSource hs = (HwndSource)HwndSource.FromVisual(Preview);
                Log("  ShowPreview(): HwndSource.FromVisual(Preview) is: " + HwndSource.FromVisual(Preview));
                Log("  ShowPreview(): Let's see what Windows thinks the parent hWnd of Preview is:");
                Log("  ShowPreview(): Calling GetParent((HwndSource)HwndSource.FromVisual(Preview).handle)...");
                NativeMethods.SetLastErrorEx(0, 0);
                IntPtr ucHandle = IntPtr.Zero;
                handle = NativeMethods.GetParent(hs.Handle);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Log("  ShowPreview(): GetParent() returned: " + handle.ToString());
                Log("      GetLastError() returned: " + error.ToString());
                Log(" ");

                Log("  ShowPreview(): Is the src window visible?");
                Log("  ShowPreview(): Calling IsWindowVisible(src.Handle)...");
                NativeMethods.SetLastErrorEx(0, 0);
                bool fVisible = NativeMethods.IsWindowVisible(src.Handle);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Log("  ShowPreview(): IsWindowVisible() returned: " + fVisible.ToString());
                Log("      GetLastError() returned: " + error.ToString());
                Log(" ");
            #endif

                // Let's hook into src's message pump
                Log("  ShowPreview(): Let's hook into src's message pump");
                // TODO: determine if we need to hook into message pump

            }
            else
            {
                Log("  ShowPreview(): Invalid hWnd passed: " + hWnd.ToString());
                throw new ArgumentException("Invalid hWnd passed to ShowPreview(): " + hWnd.ToString());
            }

            Log("ShowPreview(): exiting.");
        }
Example #3
0
        public static System.Drawing.Bitmap GuessContour(FlaUI.Core.AutomationElements.Infrastructure.AutomationElement element,
                                                         int x, int y, out int OffsetX, out int OffsetY, out System.Drawing.Rectangle resultrect)
        {
            //var element = automationutil.getElementAt(x, y);
            var elementx = (int)element.BoundingRectangle.X;
            var elementy = (int)element.BoundingRectangle.Y;
            var elementw = (int)element.BoundingRectangle.Width;
            var elementh = (int)element.BoundingRectangle.Height;

            System.Diagnostics.Trace.WriteLine(string.Format("Snap screenshot of element at ({0}, {1},{2},{3})",
                                                             elementx, elementy, elementx + elementw, elementy + elementh), "Debug");
            List <System.Drawing.Rectangle> con = null;
            var desktopb = Interfaces.Image.Util.Screenshot(elementx, elementy, elementw, elementh);

            con = FindContours(desktopb);
            //var point = new System.Drawing.Point(x - elementx, y - elementy);
            var point = new System.Drawing.Point(x, y);

            var saveimage = new Image <Emgu.CV.Structure.Bgr, Byte>(desktopb);

            foreach (var match in con)
            {
                saveimage.Draw(match, new Emgu.CV.Structure.Bgr(System.Drawing.Color.Red), 2);
            }
            // rpaactivities.image.util.saveImage(saveimage, "FoundContours");
            saveimage.Dispose();


            //var con = FindContours(bitmap);
            var overlaps = new List <System.Drawing.Rectangle>();
            // Make all matches a certain size
            var minSize = 30;

            for (var i = 0; i < con.Count; i++)
            {
                var match = con[i];
                if (match.Width < minSize)
                {
                    int dif = (minSize - match.Width);
                    match.Width += dif;
                    match.X     -= (dif / 2);
                    if (match.X < 0)
                    {
                        match.X = 0;
                    }
                    con[i] = match;
                }
                if (match.Height < minSize)
                {
                    int dif = (minSize - match.Height);
                    match.Height += dif;
                    match.Y      -= (dif / 2);
                    if (match.Y < 0)
                    {
                        match.Y = 0;
                    }
                    con[i] = match;
                }
            }
            // Take only hits that
            foreach (var match in con)
            {
                if (match.Contains(point))
                {
                    overlaps.Add(match);
                }
            }


            //if(overlaps.Count > 0)
            //{
            //    saveimage = new Image<Emgu.CV.Structure.Bgr, Byte>(desktopb);
            //    foreach (var match in con)
            //    {
            //        saveimage.Draw(match, new Bgr(System.Drawing.Color.Red), 2);
            //    }
            //    rpaactivities.image.util.saveImage(saveimage, "GuessContour-hits");
            //    saveimage.Dispose();
            //}

            System.Diagnostics.Trace.WriteLine("Found " + con.Count + " Contours with " + overlaps.Count + " overlaps");
            var rect = System.Drawing.Rectangle.Empty;

            //bool again = false;
            foreach (var match in overlaps)
            {
                if (match.Width < 500 && match.Height < 500)
                {
                    //if (rect == System.Drawing.Rectangle.Empty)
                    //{
                    //    var testb = desktopb.Clone(match, System.Drawing.Imaging.PixelFormat.Undefined);
                    //    var test = Matches.FindMatches(desktopb, testb, 0.8, 2, false);
                    //    System.Diagnostics.Trace.WriteLine("Testing with " + match.ToString() + " yelded " + test.Count() + " results");
                    //    if (test.Count() > 0)
                    //    {
                    //        rect = match;
                    //    }
                    //}
                    if (rect == System.Drawing.Rectangle.Empty)
                    {
                        rect = match;
                    }
                    else if (rect.Width < match.Width || rect.Height < match.Height)
                    {
                        //if (again)
                        //{
                        //    rect = match;
                        //    again = false;
                        //}
                        //rect = match;
                    }
                }
                else
                {
                    System.Diagnostics.Trace.WriteLine("skipping: " + match.ToString());
                }
            }

            if (rect != System.Drawing.Rectangle.Empty)
            {
                saveimage = new Image <Emgu.CV.Structure.Bgr, Byte>(desktopb);
                saveimage.Draw(rect, new Emgu.CV.Structure.Bgr(System.Drawing.Color.Red), 2);
                // rpaactivities.image.util.saveImage(saveimage, "GuessContour-result");
                saveimage.Dispose();
                desktopb.Dispose();
                System.Diagnostics.Trace.WriteLine("using overlaps " + rect.ToString());
                if (rect.Width > elementw)
                {
                    rect.Width = elementw;
                }
                if (rect.Height > elementh)
                {
                    rect.Height = elementh;
                }
                OffsetX    = x - rect.X;
                OffsetY    = y - rect.Y;
                resultrect = new System.Drawing.Rectangle(elementx + rect.X, elementy + rect.Y, rect.Width, rect.Height);
                System.Diagnostics.Trace.WriteLine(string.Format("Snap screenshot found Contour at ({0}, {1},{2},{3})",
                                                                 elementx + rect.X, elementy + rect.Y, rect.Width, rect.Height), "Debug");
                return(Interfaces.Image.Util.Screenshot(elementx + rect.X, elementy + rect.Y, rect.Width, rect.Height));
            }
            OffsetX = x;
            OffsetY = y;
            desktopb.Dispose();
            resultrect = System.Drawing.Rectangle.Empty;
            return(null);
        }
Example #4
0
        static void ParseCommandLineAndLaunch(string[] mainArgs)
        {
            Logging.LogLineIf(fDebugTrace, "ParseCommandLineAndLaunch() entered.");

            IntPtr hWnd = IntPtr.Zero;
            string launchString = M_SCREENSAVER;
            int lastProcessedIndex = -1;

            // first argument must be /scr
            if ((mainArgs.Length > 0) && (mainArgs[0].ToLowerInvariant() != @"/scr"))
            {
                throw new ArgumentException(@"CommandLine: /scr can only be the first argument." +
                Environment.NewLine + Environment.CommandLine);
            }
            lastProcessedIndex = 0;

            // second arg must be one of four valid /scr-related arguments
            if ((mainArgs.Length > 1) && !scrArgs.Contains(mainArgs[1].ToLowerInvariant()))
            {
                throw new ArgumentException(@"CommandLine: /scr can only be followed by a valid /scr-related argument." +
                Environment.NewLine + Environment.CommandLine);
            }
            lastProcessedIndex = 1;

            // if second arg starts with cp_ it must be followed with a valid window handle
            if (mainArgs[1].ToLowerInvariant() == M_CP_CONFIGURE || mainArgs[1].ToLowerInvariant() == M_CP_MINIPREVIEW)
            {
                if ((mainArgs.Length > 2) && mainArgs[2].ToLowerInvariant().StartsWith("-"))
                {
                    string subArg = mainArgs[2].ToLowerInvariant();
                    string longCandidate = subArg.Substring(1);
                    if (!String.IsNullOrEmpty(longCandidate))
                    {
                        long val;
                        bool worked = long.TryParse(longCandidate, out val);
                        if (worked)
                        {
                            Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): passed hWnd was " + DecNHex(val));

                            hWnd = new IntPtr(val);
                        }
                        else  // bad parse
                        {
                            throw new ArgumentException(@"CommandLine: invalid window handle passed: " + longCandidate +
                                Environment.NewLine + Environment.CommandLine);
                        }
                    }
                    else   // null or empty
                    {
                        throw new ArgumentException(@"CommandLine: invalid window handle passed." +
                            Environment.NewLine + Environment.CommandLine);
                    }
                }
                else  // missing required sub argument
                {
                    throw new ArgumentException(@"CommandLine: /cp_ argument missing required subargument." +
                        Environment.NewLine + Environment.CommandLine);
                }
                lastProcessedIndex = 2;
            }

            // by this point, our mode is in mainArgs[1] and hWnd is either IntPtr.Zero or a numerically validated hWnd.
            launchString = mainArgs[1].ToLowerInvariant();

            // launch
            if (launchString == M_CP_MINIPREVIEW)
            {
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): target form is CPPreview.");
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): calling new CP_Preview("+ DecNHex(hWnd) +")...");
                CP_Preview preview = new CP_Preview(hWnd);
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Constructor returned. Window handle is: " + DecNHex(preview.Handle));

                int error = 0;

                // Determine what the initial parent of our form is.
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Getting initial value of parent: Calling GetParent(" + DecNHex(preview.Handle) + ")...");
                NativeMethods.SetLastErrorEx(0, 0);
                IntPtr originalParent = NativeMethods.GetParent(preview.Handle);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): GetParent() returned IntPtr = " + DecNHex(originalParent));
                Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                Logging.LogLineIf(fDebugTrace, " ");

                // Set the passed hWnd to be the parent of the form window.
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Changing parent to passed hWnd: Calling SetParent(" + DecNHex(preview.Handle) + ", " + DecNHex(hWnd) + ")...");
                NativeMethods.SetLastErrorEx(0, 0);
                IntPtr newParent = NativeMethods.SetParent(preview.Handle, hWnd);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): SetParent() returned IntPtr = " + DecNHex(newParent));
                Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                Logging.LogLineIf(fDebugTrace, " ");

                // Verify if the form now has the expected new parent.
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Verifying new parent: Calling GetParent(" + DecNHex(preview.Handle) + ")...");
                NativeMethods.SetLastErrorEx(0, 0);
                IntPtr verifyParent = NativeMethods.GetParent(preview.Handle);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): GetParent() returned IntPtr = " + DecNHex(verifyParent));
                Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                Logging.LogLineIf(fDebugTrace, " ");

                // Set the size of the form to the size of the passed hWnd
                System.Drawing.Rectangle ParentRect = new System.Drawing.Rectangle();
                NativeMethods.SetLastErrorEx(0, 0);
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Calling GetClientRect(" + DecNHex(hWnd) +")...");
                bool fSuccess = NativeMethods.GetClientRect(hWnd, ref ParentRect);
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): GetClientRect() returned bool = " + fSuccess + ", rect = " + ParentRect.ToString());
                Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                Logging.LogLineIf(fDebugTrace, " ");

                // Set our size to new rect and location at (0, 0)
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Setting Size and Position with C# code:");
                preview.Size = ParentRect.Size;
                preview.Location = new System.Drawing.Point(0, 0);

                //// Do the Win32 API magic
                //// Get initial value of WindowLong
                //IntPtr WindowLong1 = new IntPtr();
                //int index = (int)NativeMethods.WindowLongFlags.GWL_STYLE;
                //Logging.LogLineIf(fDebugTrace, " ");
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Getting initial value of window style: calling GetWindowLongPtr(" + DecNHex(preview.Handle) + ", " + DecNHex(index) + ")...");
                //NativeMethods.SetLastErrorEx(0, 0);
                //WindowLong1 = NativeMethods.GetWindowLongPtr(preview.Handle, index);
                //error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): GetWindowLongPtr() returned long pointer: " + DecNHex(WindowLong1));
                //Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                //Logging.LogLineIf(fDebugTrace, " ");

                //// Get modified value of WindowLong
                //IntPtr WindowLong2 = new IntPtr();
                //index = (int)NativeMethods.WindowLongFlags.GWL_STYLE | 0x40000000;
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Getting modified window style: calling GetWindowLongPtr(" + DecNHex(preview.Handle) + ", " + DecNHex(index) + ")...");
                //NativeMethods.SetLastErrorEx(0, 0);
                //WindowLong2 = NativeMethods.GetWindowLongPtr(preview.Handle, index);
                //error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): GetWindowLongPtr() returned long pointer: " + DecNHex(WindowLong2));
                //Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                //Logging.LogLineIf(fDebugTrace, " ");

                //// Use modified value of WindowLong to set the new window style.
                //object ohRef = new object();
                //HandleRef hRef = new HandleRef(ohRef, preview.Handle);
                //IntPtr ip2 = new IntPtr();

                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Setting new window style: calling SetWindowLongPtr(" + DecNHex(hRef) + ", " + DecNHex(index) + ", " + DecNHex(WindowLong1) + ")");
                //NativeMethods.SetLastErrorEx(0, 0);
                //index = (int)NativeMethods.WindowLongFlags.GWL_STYLE;
                //ip2 = NativeMethods.SetWindowLongPtr(hRef, index, WindowLong1);
                //error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): SetWindowLongPtr() returned IntPtr: " + DecNHex(ip2) + ", which should be: " + DecNHex(WindowLong1));
                //Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                //Logging.LogLineIf(fDebugTrace, " ");

                //// For kicks, let's learn the current Parent of our Window.
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Who is the original parent? Calling GetParent(" + DecNHex(preview.Handle) + ")");
                //NativeMethods.SetLastErrorEx(0, 0);
                //IntPtr originalParent = NativeMethods.GetParent(preview.Handle);
                //error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): GetParent() returned IntPtr = " + DecNHex(originalParent));
                //Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                //Logging.LogLineIf(fDebugTrace, " ");

                //// Now make the passed hWnd miniprev's parent window.
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Changing parent to passed hWnd: Calling SetParent(" + DecNHex(preview.Handle) + ", " + DecNHex(hWnd) + ")");
                //NativeMethods.SetLastErrorEx(0, 0);
                //IntPtr newParent = NativeMethods.SetParent(preview.Handle, hWnd);
                //error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): SetParent() returned IntPtr = " + DecNHex(newParent));
                //Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                //Logging.LogLineIf(fDebugTrace, " ");

                //// For kicks, let's verify the new parent.
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): New parent should be " + DecNHex(newParent) + ": Calling GetParent(" + DecNHex(preview.Handle) + ")");
                //NativeMethods.SetLastErrorEx(0, 0);
                //IntPtr currentParent = NativeMethods.GetParent(preview.Handle);
                //error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): GetParent() returned IntPtr = " + DecNHex(currentParent));
                //Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                //Logging.LogLineIf(fDebugTrace, " ");

                //// Set miniprev window size to the size of our window's new parent.
                //// First, get that size.
                //System.Drawing.Rectangle ParentRect = new System.Drawing.Rectangle();
                //NativeMethods.SetLastErrorEx(0, 0);
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Calling GetClientRect to get a new rect for our form:");
                //bool fSuccess = NativeMethods.GetClientRect(hWnd, ref ParentRect);
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): GetClientRect() returned bool = " + fSuccess);
                //Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                //Logging.LogLineIf(fDebugTrace, " ");

                //// Set our size to new rect and location at (0, 0)
                //Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Setting Size and Position with C# code:");
                //preview.Size = ParentRect.Size;
                //preview.Location = new System.Drawing.Point(0, 0);

                //// Show with owner = passed hwnd
                //NativeMethods.WindowWrapper ww = new NativeMethods.WindowWrapper(hwnd);
                //miniprev.Show(ww);

                // Show the form
                Logging.LogLineIf(fDebugTrace, " ");
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Calling preview.Show()...");
                preview.Show();
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Show() has returned.");

                // Run the app
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Calling Application.Run():");
                Application.Run(preview);
            }
            else if (launchString == M_CP_CONFIGURE)
            {
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): target form is CP_Configuration.");
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): calling new CP_Configuration()...");
                CP_Configuration configDlg = new CP_Configuration();
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Constructor returned. configDlg window handle is: " + DecNHex(configDlg.Handle));

                int error = 0;

                // Get the root owner of the passed hWnd
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): Getting Root Ancestor: calling GetAncestor(hWnd, GetRoot)...");
                NativeMethods.SetLastErrorEx(0, 0);
                IntPtr passedWndRoot = NativeMethods.GetAncestor(hWnd, NativeMethods.GetAncestorFlags.GetRoot);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Logging.LogLineIf(fDebugTrace, "  ParseCommandLineAndLaunch(): GetAncestor() returned IntPtr: " + EntryPoint.DecNHex(passedWndRoot));
                Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                Logging.LogLineIf(fDebugTrace, " ");

                // and show ourselves modal to that window
                NativeMethods.WindowWrapper ww = new NativeMethods.WindowWrapper(passedWndRoot);
                DialogResult dr = configDlg.ShowDialog(ww);
            }

            Logging.LogLineIf(fDebugTrace, "ParseCommandLineAndLaunch() exiting.");
        }
 public override string ToString()
 {
     return($"{(CmdFlags)showCmd} - min {ptMinPosition} - max {ptMaxPosition} - @ {rcNormalPosition.ToString()}");
 }
Example #6
0
        /// <summary>
        /// Show the little miniControlPanelForm in the Control Panel window
        /// </summary>
        /// <param name="hWnd">hwnd to the little window of the control panel.</param>
        static void ShowMiniPreview(IntPtr hWnd)
        {
            Logging.LogLineIf(fDebugTrace, "ShowMiniPreview(): Entered.");
            Logging.LogLineIf(fDebugTrace, "   ShowMiniPreview(): hWnd = " + Logging.DecNHex(hWnd));

            if (NativeMethods.IsWindow(hWnd))
            {
                Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): calling miniControlPanelForm constructor with argument: " + Logging.DecNHex(hWnd) + " ...");
                miniControlPanelForm preview = new miniControlPanelForm(hWnd);
                Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): Constructor returned. Window handle is: " + Logging.DecNHex(preview.Handle));

                int error = 0;

                // Use Win32 API's to connect our little form to the Control Panel window handles

                //// Determine who the initial parent of our form is.
                //Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): Getting initial parent of form: Calling GetParent(" + Logging.DecNHex(preview.Handle) + ")...");
                //NativeMethods.SetLastErrorEx(0, 0);
                //IntPtr originalParent = NativeMethods.GetParent(preview.Handle);
                //error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                //Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): GetParent() returned IntPtr = " + Logging.DecNHex(originalParent));
                //Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                //Logging.LogLineIf(fDebugTrace, " ");

                // Set the passed hWnd to be the parent of the form window.
                Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): Changing parent of form to passed hWnd: Calling SetParent(" + Logging.DecNHex(preview.Handle) + ", " + Logging.DecNHex(hWnd) + ")...");
                NativeMethods.SetLastErrorEx(0, 0);
                IntPtr newParent = NativeMethods.SetParent(preview.Handle, hWnd);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): SetParent() returned IntPtr = " + Logging.DecNHex(newParent));
                Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                Logging.LogLineIf(fDebugTrace, " ");

                // Verify that the form now has the expected new parent.
                Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): Verifying new parent: Calling GetParent(" + Logging.DecNHex(preview.Handle) + ")...");
                NativeMethods.SetLastErrorEx(0, 0);
                IntPtr verifyParent = NativeMethods.GetParent(preview.Handle);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): GetParent() returned IntPtr = " + Logging.DecNHex(verifyParent));
                Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                Logging.LogLineIf(fDebugTrace, " ");

                // Set the size of the form to the size of the parent window (using the passed hWnd).
                // Get the rect
                System.Drawing.Rectangle ParentRect = new System.Drawing.Rectangle();
                NativeMethods.SetLastErrorEx(0, 0);
                Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): Calling GetClientRect(" + Logging.DecNHex(hWnd) + ")...");
                bool fSuccess = NativeMethods.GetClientRect(hWnd, ref ParentRect);
                Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): GetClientRect() returned bool = " + fSuccess + ", rect = " + ParentRect.ToString());
                Logging.LogLineIf(fDebugTrace, "      GetLastError() returned: " + error.ToString());
                Logging.LogLineIf(fDebugTrace, " ");

                // Set our size to new rect and location at (0, 0)
                Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): Setting Size and Position with C# code:");
                preview.Size = ParentRect.Size;
                preview.Location = new System.Drawing.Point(0, 0);

                // Show the form
                Logging.LogLineIf(fDebugTrace, " ");
                Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): Calling preview.Show()...");
                preview.Show();
                Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): Show() has returned.");

                // and run it
                Logging.LogLineIf(fDebugTrace, "  ShowMiniPreview(): calling Application.Run(preview)).");
                Application.Run(preview);
            }
            else
            {
                Logging.LogLineIf(fDebugOutput, "  ShowMiniPreview(): Invalid hWnd passed: " + hWnd.ToString());
                throw new ArgumentException("Invalid hWnd passed to ShowMiniPreview(): " + hWnd.ToString());
            }

            Logging.LogLineIf(fDebugTrace, "ShowMiniPreview(): Exiting.");
        }