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(); } }
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); }