This class is a wrapper around the Win32 api window handles
Inheritance: ManagedWinapi.Windows.SystemWindow
示例#1
0
        private void ActivateAndFocusMainWindow()
        {
            // What happens below looks a bit weird, but for Switcheroo to get focus when using the Alt+Tab hook,
            // it is needed to simulate an Alt keypress will bring Switcheroo to the foreground. Otherwise Switcheroo
            // will become the foreground window, but the previous window will retain focus, and receive keep getting
            // the keyboard input.
            // http://www.codeproject.com/Tips/76427/How-to-bring-window-to-top-with-SetForegroundWindo

            var thisWindowHandle = new WindowInteropHelper(this).Handle;
            var thisWindow       = new AppWindow(thisWindowHandle);

            var altKey        = new KeyboardKey(Keys.Alt);
            var altKeyPressed = false;

            // Press the Alt key if it is not already being pressed
            if ((altKey.AsyncState & 0x8000) == 0)
            {
                altKey.Press();
                altKeyPressed = true;
            }

            // Bring the Switcheroo window to the foreground
            Show();
            SystemWindow.ForegroundWindow = thisWindow;
            Activate();

            // Release the Alt key if it was pressed above
            if (altKeyPressed)
            {
                altKey.Release();
            }
        }
示例#2
0
        private void MoveToScreen(AppWindow appWindow)
        {
            var fromScreen = Screen.FromRectangle(appWindow.Rectangle);
            var toScreen   = Screen.FromPoint(System.Windows.Forms.Control.MousePosition);

            if (fromScreen.Bounds == toScreen.Bounds)
            {
                return;
            }

            if (appWindow.WindowState == FormWindowState.Maximized)
            {
                appWindow.WindowState = FormWindowState.Normal;
                appWindow.Position    = toScreen.Bounds;
                appWindow.WindowState = FormWindowState.Maximized;
                return;
            }

            var newPos = appWindow.Position;

            var xOffset = fromScreen.Bounds.Location.X - toScreen.Bounds.Location.X;
            var yOffset = fromScreen.Bounds.Location.Y - toScreen.Bounds.Location.Y;

            newPos.Left  -= xOffset;
            newPos.Right -= xOffset;

            newPos.Top    -= yOffset;
            newPos.Bottom -= yOffset;

            appWindow.Position = newPos;
        }
示例#3
0
        public String IconImageDataUri(S.AppWindow self)
        {
            var key = "IconImageDataUri-" + self.HWnd;
            var iconImageDataUri = System.Runtime.Caching.MemoryCache.Default.Get(key) as String;;

            if (iconImageDataUri == null)
            {
                var iconImage = self.IconImage;
                try
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        BitmapEncoder encoder = new PngBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(iconImage));
                        encoder.Save(memoryStream);
                        var b64String = Convert.ToBase64String(memoryStream.ToArray());
                        iconImageDataUri = "data:image/png;base64," + b64String;
                        System.Runtime.Caching.MemoryCache.Default.Add(key, iconImageDataUri, DateTimeOffset.Now.AddHours(1));
                    }
                }
                catch {
                    return(null);
                }
            }
            return(iconImageDataUri);
        }
示例#4
0
 /// <summary>
 /// Switches the window associated with the selected item.
 /// </summary>
 private void Switch()
 {
     if (lb.Items.Count > 0)
     {
         AppWindow win = (AppWindow)lb.SelectedItem ?? (AppWindow)lb.Items[0];
         win.SwitchTo();
     }
     Hide();
 }
示例#5
0
 private void CloseWindow(object sender, ExecutedRoutedEventArgs e)
 {
     if (lb.Items.Count > 0)
     {
         Hide();
         AppWindow win = (AppWindow)lb.SelectedItem;
         win.PostClose();
         win.SwitchTo();
     }
     else
     {
         Hide();
     }
     e.Handled = true;
 }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var handle        = (IntPtr)value;
            var key           = "IconImage-" + handle;
            var shortCacheKey = key + "-shortCache";
            var longCacheKey  = key + "-longCache";
            var iconImage     = MemoryCache.Default.Get(shortCacheKey) as BitmapImage;

            if (iconImage == null)
            {
                var window = new AppWindow(handle);
                var icon   = ShouldUseSmallTaskbarIcons() ? window.SmallWindowIcon : window.LargeWindowIcon;
                iconImage = _iconToBitmapConverter.Convert(icon) ?? new BitmapImage();
                MemoryCache.Default.Add(shortCacheKey, iconImage, DateTimeOffset.Now.AddSeconds(5));
                MemoryCache.Default.Add(longCacheKey, iconImage, DateTimeOffset.Now.AddMinutes(120));
            }
            return(iconImage);
        }
 public AppWindowViewModel(AppWindow appWindow)
 {
     AppWindow = appWindow;
 }
示例#8
0
文件: Core.cs 项目: svenna/Switcheroo
        private static bool EnumWindows(IntPtr hWnd, int lParam)
        {
            if (!WinApi.IsWindowVisible(hWnd))
                return true;

            var title = new StringBuilder(256);
            WinApi.GetWindowText(hWnd, title, 256);

            if (string.IsNullOrEmpty(title.ToString())) {
                return true;
            }

            //Exclude windows on the exclusion list
            if (ExceptionList.Contains(title.ToString())) {
                return true;
            }

            if (title.Length != 0 || (title.Length == 0 & hWnd != WinApi.statusbar))
            {
                var appWindow = new AppWindow(hWnd);
                if (appWindow.IsAltTabWindow())
                {
                    WindowList.Add(appWindow);
                }
            }

            return true;
        }