private bool CreateConsole()
        {
            try
            {
                bool consoleSuccess = false;
                // If the uppermost window a cmd process...
                if (_startedInConsole && _process != null)
                {
                    consoleSuccess = ConverterWindowsAPI.AttachConsole(_process.Id);
                    if (!consoleSuccess)
                    {
                        consoleSuccess = ConverterWindowsAPI.AllocConsole();
                        if (consoleSuccess)
                        {
                            Console.Title = "SVG-WPF Converter";
                        }
                    }
                }
                else
                {
                    consoleSuccess = ConverterWindowsAPI.AllocConsole();
                    if (consoleSuccess)
                    {
                        Console.Title = "SVG-WPF Converter";
                    }
                }

                return(consoleSuccess);
            }
            catch
            {
                return(false);
            }
        }
        public void DestroyConsole()
        {
            try
            {
                this.AppendLine(String.Empty);
                this.AppendLine("Press the Enter key to continue...");

                if (_commandLines.BeepOnEnd)
                {
                    if (_isConversionError)
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            Console.Beep(4000, 50);
                            Console.Beep(1000, 25);
                            Thread.Sleep(30);
                        }
                    }
                    else
                    {
                        Console.Beep(2000, 150);
                        Console.Beep(2000, 150);
                        Console.Beep(2000, 150);
                        Console.Beep(1500, 300);
                    }
                }

                ConverterWindowsAPI.FreeConsole();
            }
            catch
            {
            }
        }
        private IntPtr GetWindow()
        {
            if (_process != null && _process.MainWindowHandle != IntPtr.Zero)
            {
                return(_process.MainWindowHandle);
            }

            return(ConverterWindowsAPI.GetConsoleWindow());
        }
Example #4
0
        static int Main(string[] args)
        {
            // 1. Get a pointer to the foreground window.  The idea here is that
            // If the user is starting our application from an existing console
            // shell, that shell will be the uppermost window.  We'll get it
            // and attach to it.
            // Uses this idea from, Jeffrey Knight, since it fits our model instead
            // of the recommended ATTACH_PARENT_PROCESS (DWORD)-1 parameter
            bool   startedInConsole = false;
            IntPtr ptr       = ConverterWindowsAPI.GetForegroundWindow();
            int    processId = -1;

            ConverterWindowsAPI.GetWindowThreadProcessId(ptr, out processId);
            Process process = Process.GetProcessById(processId);

            startedInConsole = (process != null && string.Equals(
                                    process.ProcessName, "cmd", StringComparison.OrdinalIgnoreCase));

            // 2. Parse the command-line options to determine the requested task...
            ConverterCommandLines commandLines = new ConverterCommandLines(args);
            bool parseSuccess = commandLines.Parse(startedInConsole);

            ConverterUIOption uiOption = commandLines.Ui;
            bool isQuiet = (uiOption == ConverterUIOption.None);

            // 3. If the parsing is successful...
            if (parseSuccess)
            {
                // A test for possible file drag/drop on application icon
                int sourceCount = 0;
                if (commandLines != null && !commandLines.IsEmpty)
                {
                    IList <string> sourceFiles = commandLines.SourceFiles;
                    if (sourceFiles != null)
                    {
                        sourceCount = sourceFiles.Count;
                    }
                    else
                    {
                        string sourceFile = commandLines.SourceFile;
                        if (!string.IsNullOrWhiteSpace(sourceFile) &&
                            File.Exists(sourceFile))
                        {
                            sourceCount = 1;
                        }
                    }
                }

                // For the console or quiet mode...
                if (startedInConsole)
                {
                    // If explicitly asked for a Windows GUI...
                    if (uiOption == ConverterUIOption.Windows)
                    {
                        if (args != null && args.Length != 0 &&
                            args.Length == sourceCount)
                        {
                            // if it passes our simple drag/drop test, show
                            // the minimal window for quick conversion of files...
                            return(MainStartup.RunWindows(commandLines, false));
                        }
                        else
                        {
                            //...otherwise, display the main window.
                            return(MainStartup.RunWindows(commandLines, true));
                        }
                    }
                    else
                    {
                        int exitCode = MainStartup.RunConsole(commandLines,
                                                              isQuiet, startedInConsole, process);

                        // Exit the application...
                        ConverterWindowsAPI.ExitProcess((uint)exitCode);

                        return(exitCode);
                    }
                }
                else if (isQuiet || uiOption == ConverterUIOption.Console)
                {
                    int exitCode = MainStartup.RunConsole(commandLines,
                                                          isQuiet, startedInConsole, process);

                    // Exit the application...
                    ConverterWindowsAPI.ExitProcess((uint)exitCode);

                    return(exitCode);
                }
                else //...for the GUI Windows mode...
                {
                    if (args != null && args.Length != 0 &&
                        args.Length == sourceCount)
                    {
                        // if it passes our simple drag/drop test, show
                        // the minimal window for quick conversion of files...
                        return(MainStartup.RunWindows(commandLines, false));
                    }
                    else
                    {
                        //...otherwise, display the main window.
                        return(MainStartup.RunWindows(commandLines, true));
                    }
                }
            }
            else //... else if the parsing failed...
            {
                if (commandLines != null)
                {
                    commandLines.ShowHelp = true;
                }

                if (startedInConsole ||
                    (commandLines != null && uiOption == ConverterUIOption.Console))
                {
                    int exitCode = MainStartup.RunConsoleHelp(commandLines,
                                                              isQuiet, startedInConsole, process);

                    // Exit the application...
                    ConverterWindowsAPI.ExitProcess((uint)exitCode);

                    return(exitCode);
                }
                else
                {
                    return(MainStartup.RunWindows(commandLines, true));
                }
            }
        }