Exemplo n.º 1
0
        private void WindowGetMsg(object sender, WndProcEventArgs e)
        {
            var message = e.Message.ToInt64();

            if (message == WM_SYSCOMMAND)
            {
                //string dbgMessage = string.Format("WM_SYSCOMMAND, Form, Handle = {0}, WParam = {1}", e.Handle, e.WParam);
                //System.Diagnostics.Trace.WriteLine(dbgMessage);
                var window = _windows.FirstOrDefault(w => w.Handle == e.Handle);
                if (window != null)
                {
                    var lowOrder = e.WParam.ToInt64() & 0x0000FFFF;
                    switch (lowOrder)
                    {
                    case MenuItemId.SC_MAXIMIZE:
                    {
                        window.Menu.UncheckSizeMenu();
                    }
                    break;

                    case MenuItemId.SC_MINIMIZE_TO_SYSTEMTRAY:
                    {
                        window.MinimizeToSystemTray();
                    }
                    break;

                    case MenuItemId.SC_MINIMIZE_ALWAYS_TO_SYSTEMTRAY:
                    {
                        bool r = window.Menu.IsMenuItemChecked(MenuItemId.SC_MINIMIZE_ALWAYS_TO_SYSTEMTRAY);
                        window.Menu.CheckMenuItem(MenuItemId.SC_MINIMIZE_ALWAYS_TO_SYSTEMTRAY, !r);
                        window.SetStateMinimizeToTrayAlways(!r);
                    }
                    break;

                    case MenuItemId.SC_SUSPEND_TO_SYSTEMTRAY:
                    {
                        window.MinimizeToSystemTray();
                        Thread.Sleep(100);
                        window.Suspend();
                    }
                    break;

                    case MenuItemId.SC_INFORMATION:
                    {
                        var infoForm = new InfoForm(window.GetWindowInfo(), _settings.Language);
                        infoForm.Show(window.Win32Window);
                    }
                    break;

                    case MenuItemId.SC_SAVE_SCREEN_SHOT:
                    {
                        var bitmap = WindowUtils.PrintWindow(window.Handle);
                        var dialog = new SaveFileDialog
                        {
                            OverwritePrompt  = true,
                            ValidateNames    = true,
                            Title            = _settings.Language.GetValue("save_screenshot_title"),
                            FileName         = _settings.Language.GetValue("save_screenshot_filename"),
                            DefaultExt       = _settings.Language.GetValue("save_screenshot_default_ext"),
                            RestoreDirectory = false,
                            Filter           = _settings.Language.GetValue("save_screenshot_filter")
                        };
                        if (dialog.ShowDialog(window.Win32Window) == DialogResult.OK)
                        {
                            var fileExtension = Path.GetExtension(dialog.FileName).ToLower();
                            var imageFormat   = fileExtension == ".bmp" ? ImageFormat.Bmp :
                                                fileExtension == ".gif" ? ImageFormat.Gif :
                                                fileExtension == ".jpeg" ? ImageFormat.Jpeg :
                                                fileExtension == ".png" ? ImageFormat.Png :
                                                fileExtension == ".tiff" ? ImageFormat.Tiff : ImageFormat.Wmf;
                            bitmap.Save(dialog.FileName, imageFormat);
                        }
                    }
                    break;

                    case MenuItemId.SC_COPY_WINDOW_TEXT:
                    {
                        var text = window.ExtractText();
                        if (!string.IsNullOrEmpty(text))
                        {
                            Clipboard.SetText(text);
                        }
                    }
                    break;

                    case MenuItemId.SC_COPY_WINDOW_TITLE:
                    {
                        var text = window.GetWindowText();
                        if (!string.IsNullOrEmpty(text))
                        {
                            Clipboard.SetText(text);
                        }
                    }
                    break;

                    case MenuItemId.SC_COPY_FULL_PROCESS_PATH:
                    {
                        var path = window.Process?.GetMainModuleFileName();
                        if (!string.IsNullOrEmpty(path))
                        {
                            Clipboard.SetText(path);
                        }
                    }
                    break;

                    case MenuItemId.SC_CLEAR_CLIPBOARD:
                    {
                        Clipboard.Clear();
                    }
                    break;

                    case MenuItemId.SC_DRAG_BY_MOUSE:
                    {
                        var isChecked = window.Menu.IsMenuItemChecked(MenuItemId.SC_DRAG_BY_MOUSE);
                        window.Menu.CheckMenuItem(MenuItemId.SC_DRAG_BY_MOUSE, !isChecked);
                    }
                    break;

                    case MenuItemId.SC_OPEN_FILE_IN_EXPLORER:
                    {
                        try
                        {
                            SystemUtils.RunAs("explorer.exe", "/select, " + window.Process.GetMainModuleFileName(), true, UserType.Normal);
                        }
                        catch
                        {
                        }
                    }
                    break;

                    case MenuItemId.SC_MINIMIZE_OTHER_WINDOWS:
                    case MenuItemId.SC_CLOSE_OTHER_WINDOWS:
                    {
                        EnumWindows((IntPtr handle, int lParam) =>
                            {
                                if (handle != IntPtr.Zero && handle != Handle && handle != window.Handle && WindowUtils.IsAltTabWindow(handle))
                                {
                                    if (lowOrder == MenuItemId.SC_CLOSE_OTHER_WINDOWS)
                                    {
                                        PostMessage(handle, WM_CLOSE, 0, 0);
                                    }
                                    else
                                    {
                                        PostMessage(handle, WM_SYSCOMMAND, MenuItemId.SC_MINIMIZE, 0);
                                    }
                                }
                                return(true);
                            }, 0);
                    }
                    break;

                    case MenuItemId.SC_TOPMOST:
                    {
                        var isChecked = window.Menu.IsMenuItemChecked(MenuItemId.SC_TOPMOST);
                        window.Menu.CheckMenuItem(MenuItemId.SC_TOPMOST, !isChecked);
                        window.MakeTopMost(!isChecked);
                    }
                    break;

                    case MenuItemId.SC_HIDE_FOR_ALT_TAB:
                    {
                        var isChecked = window.Menu.IsMenuItemChecked(MenuItemId.SC_HIDE_FOR_ALT_TAB);
                        window.Menu.CheckMenuItem(MenuItemId.SC_HIDE_FOR_ALT_TAB, !isChecked);
                        window.HideForAltTab(!isChecked);
                    }
                    break;

                    case MenuItemId.SC_SEND_TO_BOTTOM:
                    {
                        window.SendToBottom();
                    }
                    break;

                    case MenuItemId.SC_AERO_GLASS:
                    {
                        var isChecked = window.Menu.IsMenuItemChecked(MenuItemId.SC_AERO_GLASS);
                        window.AeroGlass(!isChecked);
                        window.Menu.CheckMenuItem(MenuItemId.SC_AERO_GLASS, !isChecked);
                    }
                    break;

                    case MenuItemId.SC_ROLLUP:
                    {
                        var isChecked = window.Menu.IsMenuItemChecked(MenuItemId.SC_ROLLUP);
                        window.Menu.CheckMenuItem(MenuItemId.SC_ROLLUP, !isChecked);
                        if (!isChecked)
                        {
                            window.RollUp();
                            window.Menu.UncheckSizeMenu();
                        }
                        else
                        {
                            window.UnRollUp();
                        }
                    }
                    break;


                    case MenuItemId.SC_SIZE_DEFAULT:
                    {
                        window.Menu.UncheckSizeMenu();
                        window.Menu.CheckMenuItem(MenuItemId.SC_SIZE_DEFAULT, true);
                        window.ShowNormal();
                        window.RestoreSize();
                        window.Menu.UncheckMenuItems(MenuItemId.SC_ROLLUP);
                    }
                    break;

                    case MenuItemId.SC_SIZE_CUSTOM:
                    {
                        var sizeForm = new SizeForm(window, _settings);
                        var result   = sizeForm.ShowDialog(window.Win32Window);
                        if (result == DialogResult.OK)
                        {
                            window.ShowNormal();

                            if (_settings.Sizer == WindowSizerType.WindowWithMargins)
                            {
                                window.SetSize(sizeForm.WindowWidth, sizeForm.WindowHeight, sizeForm.WindowLeft, sizeForm.WindowTop);
                            }
                            else if (_settings.Sizer == WindowSizerType.WindowWithoutMargins)
                            {
                                var margins = window.GetSystemMargins();
                                window.SetSize(sizeForm.WindowWidth + margins.Left + margins.Right, sizeForm.WindowHeight + margins.Top + margins.Bottom, sizeForm.WindowLeft, sizeForm.WindowTop);
                            }
                            else
                            {
                                window.SetSize(sizeForm.WindowWidth + (window.Size.Width - window.ClientSize.Width), sizeForm.WindowHeight + (window.Size.Height - window.ClientSize.Height), sizeForm.WindowLeft, sizeForm.WindowTop);
                            }

                            window.Menu.UncheckSizeMenu();
                            window.Menu.CheckMenuItem(MenuItemId.SC_SIZE_CUSTOM, true);
                            window.Menu.UncheckMenuItems(MenuItemId.SC_ROLLUP);
                        }
                    }
                    break;

                    case MenuItemId.SC_TRANS_DEFAULT:
                    {
                        window.Menu.UncheckTransparencyMenu();
                        window.Menu.CheckMenuItem(MenuItemId.SC_TRANS_DEFAULT, true);
                        window.RestoreTransparency();
                    }
                    break;

                    case MenuItemId.SC_TRANS_CUSTOM:
                    {
                        var opacityForm = new TransparencyForm(window, _settings);
                        var result      = opacityForm.ShowDialog(window.Win32Window);
                        if (result == DialogResult.OK)
                        {
                            window.SetTransparency(opacityForm.WindowTransparency);
                            window.Menu.UncheckTransparencyMenu();
                            window.Menu.CheckMenuItem(MenuItemId.SC_TRANS_CUSTOM, true);
                        }
                    }
                    break;

                    case MenuItemId.SC_ALIGN_DEFAULT:
                    {
                        window.Menu.UncheckAlignmentMenu();
                        window.Menu.CheckMenuItem(MenuItemId.SC_ALIGN_DEFAULT, true);
                        window.RestorePosition();
                    }
                    break;

                    case MenuItemId.SC_ALIGN_CUSTOM:
                    {
                        var positionForm = new PositionForm(window, _settings.Language);
                        var result       = positionForm.ShowDialog(window.Win32Window);

                        if (result == DialogResult.OK)
                        {
                            window.ShowNormal();
                            window.SetPosition(positionForm.WindowLeft, positionForm.WindowTop);
                            window.Menu.UncheckAlignmentMenu();
                            window.Menu.CheckMenuItem(MenuItemId.SC_ALIGN_CUSTOM, true);
                        }
                    }
                    break;

                    case MenuItemId.SC_TRANS_100:
                    case MenuItemId.SC_TRANS_90:
                    case MenuItemId.SC_TRANS_80:
                    case MenuItemId.SC_TRANS_70:
                    case MenuItemId.SC_TRANS_60:
                    case MenuItemId.SC_TRANS_50:
                    case MenuItemId.SC_TRANS_40:
                    case MenuItemId.SC_TRANS_30:
                    case MenuItemId.SC_TRANS_20:
                    case MenuItemId.SC_TRANS_10:
                    case MenuItemId.SC_TRANS_00:
                        SetTransparencyMenuItem(window, (int)lowOrder);
                        break;

                    case MenuItemId.SC_PRIORITY_REAL_TIME:
                    case MenuItemId.SC_PRIORITY_HIGH:
                    case MenuItemId.SC_PRIORITY_ABOVE_NORMAL:
                    case MenuItemId.SC_PRIORITY_NORMAL:
                    case MenuItemId.SC_PRIORITY_BELOW_NORMAL:
                    case MenuItemId.SC_PRIORITY_IDLE:
                        SetPriorityMenuItem(window, (int)lowOrder);
                        break;

                    case MenuItemId.SC_ALIGN_TOP_LEFT:
                    case MenuItemId.SC_ALIGN_TOP_CENTER:
                    case MenuItemId.SC_ALIGN_TOP_RIGHT:
                    case MenuItemId.SC_ALIGN_MIDDLE_LEFT:
                    case MenuItemId.SC_ALIGN_MIDDLE_CENTER:
                    case MenuItemId.SC_ALIGN_MIDDLE_RIGHT:
                    case MenuItemId.SC_ALIGN_BOTTOM_LEFT:
                    case MenuItemId.SC_ALIGN_BOTTOM_CENTER:
                    case MenuItemId.SC_ALIGN_BOTTOM_RIGHT:
                    case MenuItemId.SC_ALIGN_CENTER_HORIZONTALLY:
                    case MenuItemId.SC_ALIGN_CENTER_VERTICALLY:
                        SetAlignmentMenuItem(window, (int)lowOrder);
                        break;
                    }

                    var moveToSubMenuItem = (int)lowOrder - MenuItemId.SC_MOVE_TO;
                    if (window.Menu.MoveToMenuItems.ContainsKey(moveToSubMenuItem))
                    {
                        var monitorHandle = window.Menu.MoveToMenuItems[moveToSubMenuItem];
                        window.MoveToMonitor(monitorHandle);
                    }

                    var windowSizeItem = _settings.MenuItems.WindowSizeItems.FirstOrDefault(x => x.Id == lowOrder);
                    if (windowSizeItem != null)
                    {
                        SetSizeMenuItem(window, (int)lowOrder, windowSizeItem);
                    }

                    for (int i = 0; i < _settings.MenuItems.StartProgramItems.Count; i++)
                    {
                        if (lowOrder - MenuItemId.SC_START_PROGRAM == i)
                        {
                            try
                            {
                                var item                 = _settings.MenuItems.StartProgramItems[i];
                                var arguments            = item.Arguments;
                                var argumentParameters   = arguments.GetParams(item.BeginParameter, item.EndParameter);
                                var allParametersInputed = true;
                                var processPath          = window.Process?.GetMainModuleFileName() ?? string.Empty;
                                foreach (var parameter in argumentParameters)
                                {
                                    var parameterName = parameter.TrimStart(item.BeginParameter).TrimEnd(item.EndParameter);
                                    if (string.Compare(parameterName, StartProgramMenuItem.PARAMETER_PROCESS_ID, true) == 0)
                                    {
                                        arguments = arguments.Replace(parameter, window.Process?.Id.ToString() ?? string.Empty);
                                        continue;
                                    }

                                    if (string.Compare(parameterName, StartProgramMenuItem.PARAMETER_PROCESS_NAME, true) == 0)
                                    {
                                        arguments = arguments.Replace(parameter, Path.GetFileName(processPath));
                                        continue;
                                    }

                                    if (string.Compare(parameterName, StartProgramMenuItem.PARAMETER_WINDOW_TITLE, true) == 0)
                                    {
                                        arguments = arguments.Replace(parameter, window.GetWindowText());
                                        continue;
                                    }

                                    var parameterForm = new ParameterForm(parameterName, _settings.Language);
                                    var result        = parameterForm.ShowDialog(window.Win32Window);

                                    if (result == DialogResult.OK)
                                    {
                                        arguments = arguments.Replace(parameter, parameterForm.ParameterValue);
                                    }
                                    else
                                    {
                                        allParametersInputed = false;
                                        break;
                                    }
                                }

                                if (allParametersInputed)
                                {
                                    SystemUtils.RunAs(item.FileName, arguments, item.ShowWindow, item.RunAs, item.UseWindowWorkingDirectory ? Path.GetDirectoryName(processPath) : null);
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            break;
                        }
                    }

                    var isSaveItemChecked = window.Menu.IsMenuItemChecked(MenuItemId.SC_SAVE_SELECTED_ITEMS);
                    if (lowOrder == MenuItemId.SC_SAVE_SELECTED_ITEMS)
                    {
                        isSaveItemChecked = !isSaveItemChecked;
                        window.Menu.CheckMenuItem(MenuItemId.SC_SAVE_SELECTED_ITEMS, isSaveItemChecked);
                        window.RefreshState();
                        var windowClassName = window.GetClassName();
                        var processPath     = window.Process?.GetMainModuleFileName() ?? string.Empty;
                        if (!string.IsNullOrEmpty(windowClassName) && !string.IsNullOrEmpty(processPath))
                        {
                            var states = _windowSettings.Find(windowClassName, processPath);
                            if (states.Any())
                            {
                                _windowSettings.Remove(windowClassName, processPath);
                            }

                            if (isSaveItemChecked)
                            {
                                _windowSettings.Items.Add((WindowState)window.State.Clone());
                            }

#if WIN32
                            var fileName = Path.Combine(AssemblyUtils.AssemblyDirectory, "Window.xml");
#else
                            var fileName = Path.Combine(AssemblyUtils.AssemblyDirectory, "Window64.xml");
#endif
                            WindowSettings.Save(fileName, _windowSettings);
                        }
                    }
                    else if (isSaveItemChecked &&
                             lowOrder != MenuItemId.SC_MOVE &&
                             lowOrder != MenuItemId.SC_MINIMIZE &&
                             lowOrder != MenuItemId.SC_MAXIMIZE &&
                             lowOrder != MenuItemId.SC_RESTORE &&
                             lowOrder != MenuItemId.SC_RESIZE &&
                             lowOrder != MenuItemId.SC_CLOSE)
                    {
                        window.RefreshState();
                        var windowClassName = window.GetClassName();
                        var processPath     = window.Process?.GetMainModuleFileName() ?? string.Empty;
                        if (!string.IsNullOrEmpty(windowClassName) && !string.IsNullOrEmpty(processPath))
                        {
                            var states = _windowSettings.Find(windowClassName, processPath);
                            if (states.Any())
                            {
                                _windowSettings.Remove(windowClassName, processPath);
                            }

                            _windowSettings.Items.Add((WindowState)window.State.Clone());
#if WIN32
                            var fileName = Path.Combine(AssemblyUtils.AssemblyDirectory, "Window.xml");
#else
                            var fileName = Path.Combine(AssemblyUtils.AssemblyDirectory, "Window64.xml");
#endif
                            WindowSettings.Save(fileName, _windowSettings);
                        }
                    }
                }
            }
        }