Example #1
0
        private ApplicationWindow addWindow(IntPtr hWnd, ApplicationWindow.WindowState initialState = ApplicationWindow.WindowState.Inactive, bool sanityCheck = false)
        {
            ApplicationWindow win = new ApplicationWindow(this, hWnd);

            // set window state if a non-default value is provided
            if (initialState != ApplicationWindow.WindowState.Inactive)
            {
                win.State = initialState;
            }

            // add window unless we need to validate it is eligible to show in taskbar
            if (!sanityCheck || win.CanAddToTaskbar)
            {
                Windows.Add(win);
            }

            // Only send TaskbarButtonCreated if we are shell, and if OS is not Server Core
            // This is because if Explorer is running, it will send the message, so we don't need to
            if (EnvironmentHelper.IsAppRunningAsShell)
            {
                sendTaskbarButtonCreatedMessage(win.Handle);
            }

            return(win);
        }
Example #2
0
        private void getInitialWindows()
        {
            EnumWindows((hwnd, lParam) =>
            {
                ApplicationWindow win = new ApplicationWindow(this, hwnd);

                if (win.CanAddToTaskbar && win.ShowInTaskbar && !Windows.Contains(win))
                {
                    Windows.Add(win);

                    sendTaskbarButtonCreatedMessage(win.Handle);
                }

                return(true);
            }, 0);

            IntPtr hWndForeground = GetForegroundWindow();

            if (Windows.Any(i => i.Handle == hWndForeground && i.ShowInTaskbar))
            {
                ApplicationWindow win = Windows.First(wnd => wnd.Handle == hWndForeground);
                win.State = ApplicationWindow.WindowState.Active;
                win.SetShowInTaskbar();
            }
        }
Example #3
0
 public void CloseWindow(ApplicationWindow window)
 {
     if (window.DoClose() != IntPtr.Zero)
     {
         ShellLogger.Debug($"TasksService: Removing window {window.Title} from collection due to no response");
         window.Dispose();
         Windows.Remove(window);
     }
 }
Example #4
0
 private void UncloakEventCallback(IntPtr hWinEventHook, uint eventType, IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
 {
     if (hWnd != IntPtr.Zero && idObject == 0 && idChild == 0)
     {
         if (Windows.Any(i => i.Handle == hWnd))
         {
             ApplicationWindow win = Windows.First(wnd => wnd.Handle == hWnd);
             win.Uncloak();
         }
     }
 }
Example #5
0
        private void redrawWindow(ApplicationWindow win)
        {
            win.UpdateProperties();

            foreach (ApplicationWindow wind in Windows)
            {
                if (wind.WinFileName == win.WinFileName && wind.Handle != win.Handle)
                {
                    wind.UpdateProperties();
                }
            }
        }
Example #6
0
 private void removeWindow(IntPtr hWnd)
 {
     if (Windows.Any(i => i.Handle == hWnd))
     {
         do
         {
             ApplicationWindow win = Windows.First(wnd => wnd.Handle == hWnd);
             win.Dispose();
             Windows.Remove(win);
         }while (Windows.Any(i => i.Handle == hWnd));
     }
 }
Example #7
0
        private void ShellWinProc(Message msg)
        {
            if (msg.Msg == WM_SHELLHOOKMESSAGE)
            {
                try
                {
                    lock (_windowsLock)
                    {
                        switch ((HSHELL)msg.WParam.ToInt32())
                        {
                        case HSHELL.WINDOWCREATED:
                            ShellLogger.Debug("TasksService: Created: " + msg.LParam);
                            if (!Windows.Any(i => i.Handle == msg.LParam))
                            {
                                addWindow(msg.LParam);
                            }
                            else
                            {
                                ApplicationWindow win = Windows.First(wnd => wnd.Handle == msg.LParam);
                                win.UpdateProperties();
                            }
                            break;

                        case HSHELL.WINDOWDESTROYED:
                            ShellLogger.Debug("TasksService: Destroyed: " + msg.LParam);
                            removeWindow(msg.LParam);
                            break;

                        case HSHELL.WINDOWREPLACING:
                            ShellLogger.Debug("TasksService: Replacing: " + msg.LParam);
                            if (Windows.Any(i => i.Handle == msg.LParam))
                            {
                                ApplicationWindow win = Windows.First(wnd => wnd.Handle == msg.LParam);
                                win.State = ApplicationWindow.WindowState.Inactive;
                                win.SetShowInTaskbar();
                            }
                            else
                            {
                                addWindow(msg.LParam);
                            }
                            break;

                        case HSHELL.WINDOWREPLACED:
                            ShellLogger.Debug("TasksService: Replaced: " + msg.LParam);
                            // TODO: If a window gets replaced, we lose app-level state such as overlay icons.
                            removeWindow(msg.LParam);
                            break;

                        case HSHELL.WINDOWACTIVATED:
                        case HSHELL.RUDEAPPACTIVATED:
                            ShellLogger.Debug("TasksService: Activated: " + msg.LParam);

                            foreach (var aWin in Windows.Where(w => w.State == ApplicationWindow.WindowState.Active))
                            {
                                aWin.State = ApplicationWindow.WindowState.Inactive;
                            }

                            if (msg.LParam != IntPtr.Zero)
                            {
                                ApplicationWindow win = null;

                                if (Windows.Any(i => i.Handle == msg.LParam))
                                {
                                    win       = Windows.First(wnd => wnd.Handle == msg.LParam);
                                    win.State = ApplicationWindow.WindowState.Active;
                                    win.SetShowInTaskbar();
                                }
                                else
                                {
                                    win = addWindow(msg.LParam, ApplicationWindow.WindowState.Active);
                                }

                                if (win != null)
                                {
                                    foreach (ApplicationWindow wind in Windows)
                                    {
                                        if (wind.WinFileName == win.WinFileName && wind.Handle != win.Handle)
                                        {
                                            wind.SetShowInTaskbar();
                                        }
                                    }
                                }
                            }
                            break;

                        case HSHELL.FLASH:
                            ShellLogger.Debug("TasksService: Flashing window: " + msg.LParam);
                            if (Windows.Any(i => i.Handle == msg.LParam))
                            {
                                ApplicationWindow win = Windows.First(wnd => wnd.Handle == msg.LParam);

                                if (win.State != ApplicationWindow.WindowState.Active)
                                {
                                    win.State = ApplicationWindow.WindowState.Flashing;
                                }

                                redrawWindow(win);
                            }
                            else
                            {
                                addWindow(msg.LParam, ApplicationWindow.WindowState.Flashing, true);
                            }
                            break;

                        case HSHELL.ACTIVATESHELLWINDOW:
                            ShellLogger.Debug("TasksService: Activate shell window called.");
                            break;

                        case HSHELL.ENDTASK:
                            ShellLogger.Debug("TasksService: EndTask called: " + msg.LParam);
                            removeWindow(msg.LParam);
                            break;

                        case HSHELL.GETMINRECT:
                            ShellLogger.Debug("TasksService: GetMinRect called: " + msg.LParam);
                            SHELLHOOKINFO winHandle = (SHELLHOOKINFO)Marshal.PtrToStructure(msg.LParam, typeof(SHELLHOOKINFO));
                            winHandle.rc = new NativeMethods.Rect {
                                Bottom = 100, Left = 0, Right = 100, Top = 0
                            };
                            Marshal.StructureToPtr(winHandle, msg.LParam, true);
                            msg.Result = winHandle.hwnd;
                            return;     // return here so the result isnt reset to DefWindowProc

                        case HSHELL.REDRAW:
                            ShellLogger.Debug("TasksService: Redraw called: " + msg.LParam);
                            if (Windows.Any(i => i.Handle == msg.LParam))
                            {
                                ApplicationWindow win = Windows.First(wnd => wnd.Handle == msg.LParam);

                                if (win.State == ApplicationWindow.WindowState.Flashing)
                                {
                                    win.State = ApplicationWindow.WindowState.Inactive;
                                }

                                redrawWindow(win);
                            }
                            else
                            {
                                addWindow(msg.LParam, ApplicationWindow.WindowState.Inactive, true);
                            }
                            break;

                        // TaskMan needs to return true if we provide our own task manager to prevent explorers.
                        // case HSHELL.TASKMAN:
                        //     SingletonLogger.Instance.Info("TaskMan Message received.");
                        //     break;

                        default:
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    ShellLogger.Error("TasksService: Error in ShellWinProc. ", ex);
                    Debugger.Break();
                }
            }
            else if (msg.Msg == WM_TASKBARCREATEDMESSAGE)
            {
                ShellLogger.Debug("TasksService: TaskbarCreated received, setting ITaskbarList window");
                setTaskbarListHwnd(_HookWin.Handle);
            }
            else
            {
                // Handle ITaskbarList functions, most not implemented yet

                ApplicationWindow win = null;

                switch (msg.Msg)
                {
                case (int)WM.USER + 50:
                    // ActivateTab
                    // Also sends WM_SHELLHOOK message
                    ShellLogger.Debug("TasksService: ITaskbarList: ActivateTab HWND:" + msg.LParam);
                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 60:
                    // MarkFullscreenWindow
                    ShellLogger.Debug("TasksService: ITaskbarList: MarkFullscreenWindow HWND:" + msg.LParam + " Entering? " + msg.WParam);
                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 64:
                    // SetProgressValue
                    ShellLogger.Debug("TasksService: ITaskbarList: SetProgressValue HWND:" + msg.WParam + " Progress: " + msg.LParam);

                    win = new ApplicationWindow(this, msg.WParam);
                    if (Windows.Contains(win))
                    {
                        win = Windows.First(wnd => wnd.Handle == msg.WParam);
                        win.ProgressValue = (int)msg.LParam;
                    }

                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 65:
                    // SetProgressState
                    ShellLogger.Debug("TasksService: ITaskbarList: SetProgressState HWND:" + msg.WParam + " Flags: " + msg.LParam);

                    win = new ApplicationWindow(this, msg.WParam);
                    if (Windows.Contains(win))
                    {
                        win = Windows.First(wnd => wnd.Handle == msg.WParam);
                        win.ProgressState = (TBPFLAG)msg.LParam;
                    }

                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 67:
                    // RegisterTab
                    ShellLogger.Debug("TasksService: ITaskbarList: RegisterTab MDI HWND:" + msg.LParam + " Tab HWND: " + msg.WParam);
                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 68:
                    // UnregisterTab
                    ShellLogger.Debug("TasksService: ITaskbarList: UnregisterTab Tab HWND: " + msg.WParam);
                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 71:
                    // SetTabOrder
                    ShellLogger.Debug("TasksService: ITaskbarList: SetTabOrder HWND:" + msg.WParam + " Before HWND: " + msg.LParam);
                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 72:
                    // SetTabActive
                    ShellLogger.Debug("TasksService: ITaskbarList: SetTabActive HWND:" + msg.WParam);
                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 75:
                    // Unknown
                    ShellLogger.Debug("TasksService: ITaskbarList: Unknown HWND:" + msg.WParam + " LParam: " + msg.LParam);
                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 76:
                    // ThumbBarAddButtons
                    ShellLogger.Debug("TasksService: ITaskbarList: ThumbBarAddButtons HWND:" + msg.WParam);
                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 77:
                    // ThumbBarUpdateButtons
                    ShellLogger.Debug("TasksService: ITaskbarList: ThumbBarUpdateButtons HWND:" + msg.WParam);
                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 78:
                    // ThumbBarSetImageList
                    ShellLogger.Debug("TasksService: ITaskbarList: ThumbBarSetImageList HWND:" + msg.WParam);
                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 79:
                    // SetOverlayIcon - Icon
                    ShellLogger.Debug("TasksService: ITaskbarList: SetOverlayIcon - Icon HWND:" + msg.WParam);

                    win = new ApplicationWindow(this, msg.WParam);
                    if (Windows.Contains(win))
                    {
                        win = Windows.First(wnd => wnd.Handle == msg.WParam);
                        win.SetOverlayIcon(msg.LParam);
                    }

                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 80:
                    // SetThumbnailTooltip
                    ShellLogger.Debug("TasksService: ITaskbarList: SetThumbnailTooltip HWND:" + msg.WParam);
                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 81:
                    // SetThumbnailClip
                    ShellLogger.Debug("TasksService: ITaskbarList: SetThumbnailClip HWND:" + msg.WParam);
                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 85:
                    // SetOverlayIcon - Description
                    ShellLogger.Debug("TasksService: ITaskbarList: SetOverlayIcon - Description HWND:" + msg.WParam);

                    win = new ApplicationWindow(this, msg.WParam);
                    if (Windows.Contains(win))
                    {
                        win = Windows.First(wnd => wnd.Handle == msg.WParam);
                        win.SetOverlayIconDescription(msg.LParam);
                    }

                    msg.Result = IntPtr.Zero;
                    return;

                case (int)WM.USER + 87:
                    // SetTabProperties
                    ShellLogger.Debug("TasksService: ITaskbarList: SetTabProperties HWND:" + msg.WParam);
                    msg.Result = IntPtr.Zero;
                    return;
                }
            }

            msg.Result = DefWindowProc(msg.HWnd, msg.Msg, msg.WParam, msg.LParam);
        }