コード例 #1
0
        /// <summary>
        ///     Get the file path to the exe for the process which owns this window
        /// </summary>
        public static string GetProcessPath(this IInteropWindow interopWindow)
        {
            // TODO: fix for apps
            var processid = interopWindow.GetProcessId();

            return(Kernel32Api.GetProcessPath(processid));
        }
コード例 #2
0
        /// <summary>
        ///     Get the icon for a hWnd
        /// </summary>
        /// <typeparam name="TIcon">The return type for the icon, can be Icon, Bitmap or BitmapSource</typeparam>
        /// <param name="window">IInteropWindow</param>
        /// <param name="useLargeIcons">true to try to get a big icon first</param>
        /// <returns>TIcon</returns>
        public static TIcon GetIcon <TIcon>(this IInteropWindow window, bool useLargeIcons = false) where TIcon : class
        {
            if (window.IsApp())
            {
                return(IconHelper.GetAppLogo <TIcon>(window));
            }
            var icon = GetIconFromWindow <TIcon>(window, useLargeIcons);

            if (icon != null)
            {
                return(icon);
            }
            var processId = window.GetProcessId();
            // Try to get the icon from the process file itself, if we can query the path
            var processPath = Kernel32Api.GetProcessPath(processId);

            if (processPath != null)
            {
                return(IconHelper.ExtractAssociatedIcon <TIcon>(processPath, useLargeIcon: useLargeIcons));
            }
            // Look at the windows of the other similar named processes
            using (var process = Process.GetProcessById(processId))
            {
                var processName = process.ProcessName;
                foreach (var possibleParentProcess in Process.GetProcessesByName(processName))
                {
                    var parentProcessWindow = InteropWindowFactory.CreateFor(possibleParentProcess.MainWindowHandle);
                    icon = GetIconFromWindow <TIcon>(parentProcessWindow, useLargeIcons);
                    if (icon != null)
                    {
                        return(icon);
                    }
                    possibleParentProcess.Dispose();
                }
            }
            // Try to find another window, which belongs to the same process, and get the icon from there
            foreach (var otherWindow in InteropWindowQuery.GetTopWindows().Where(interopWindow => interopWindow.GetProcessId() == processId))
            {
                if (otherWindow.Handle == window.Handle)
                {
                    continue;
                }
                icon = GetIconFromWindow <TIcon>(otherWindow, useLargeIcons);
                if (icon != null)
                {
                    return(icon);
                }
            }
            // Nothing found, REALLY!
            return(default(TIcon));
        }
コード例 #3
0
ファイル: Startup.cs プロジェクト: Vistritium/greenshot
        /// <summary>
        ///     Show all the running instances
        /// </summary>
        private static void ShowInstances()
        {
            var instanceInfo = new StringBuilder();
            var index        = 1;

            foreach (var process in Process.GetProcesses())
            {
                try
                {
                    if (process.ProcessName.ToLowerInvariant().Contains("greenshot"))
                    {
                        instanceInfo.AppendFormat("{0} : {1} (pid {2})", index++, Kernel32Api.GetProcessPath(process.Id), process.Id);
                        instanceInfo.Append(Environment.NewLine);
                    }
                }
                catch (Exception)
                {
                    //Log.Debug().WriteLine(ex);
                }
                process.Dispose();
            }

            // Placehold for the Extension
            IGreenshotLanguage language = null;

            // A dirty fix to make sure the messagebox is visible as a Greenshot window on the taskbar
            using (var multiInstanceForm = new Form
            {
                Icon = GreenshotResources.GetGreenshotIcon(),
                ShowInTaskbar = true,
                MaximizeBox = false,
                MinimizeBox = false,
                FormBorderStyle = FormBorderStyle.FixedDialog,
                Location = new Point(int.MinValue, int.MinValue),
                Text = language.TranslationOrDefault(l => l.Error),
                AutoSize = true,
                AutoSizeMode = AutoSizeMode.GrowAndShrink,
                StartPosition = FormStartPosition.CenterScreen
            })
            {
                var flowLayoutPanel = new FlowLayoutPanel
                {
                    AutoScroll    = true,
                    FlowDirection = System.Windows.Forms.FlowDirection.TopDown,
                    WrapContents  = false,
                    AutoSize      = true,
                    AutoSizeMode  = AutoSizeMode.GrowAndShrink
                };
                var internalFlowLayoutPanel = new FlowLayoutPanel
                {
                    AutoScroll    = true,
                    FlowDirection = System.Windows.Forms.FlowDirection.LeftToRight,
                    WrapContents  = false,
                    AutoSize      = true,
                    AutoSizeMode  = AutoSizeMode.GrowAndShrink
                };
                var pictureBox = new PictureBox
                {
                    Dock     = DockStyle.Left,
                    Image    = SystemIcons.Error.ToBitmap(),
                    SizeMode = PictureBoxSizeMode.AutoSize
                };
                internalFlowLayoutPanel.Controls.Add(pictureBox);
                var textbox = new Label
                {
                    Text     = language.TranslationOrDefault(l => l.ErrorMultipleinstances) + Environment.NewLine + instanceInfo,
                    AutoSize = true
                };
                internalFlowLayoutPanel.Controls.Add(textbox);
                flowLayoutPanel.Controls.Add(internalFlowLayoutPanel);
                var cancelButton = new Button
                {
                    Text   = language.TranslationOrDefault(l => l.BugreportCancel),
                    Dock   = DockStyle.Bottom,
                    Height = 20
                };
                flowLayoutPanel.Controls.Add(cancelButton);
                multiInstanceForm.Controls.Add(flowLayoutPanel);

                multiInstanceForm.CancelButton = cancelButton;

                multiInstanceForm.ShowDialog();
            }
        }