none
Can UWP App or WPF app detect which "Task View" the user is current in? RRS feed

  • Question

  • Can UWP App or WPF app detect which "Task View" (aka. Desktop 1, Desktop 2.. etc) the user is current in?

    I am interested in making a personal app that tracks my usage in each Desktop.

    Thanks in advance. 


    DYY

    Monday, January 14, 2019 8:54 AM

All replies

  • I haven't looked into the implementation of task view in Windows 10 but I assume it is relying on the standard ability for a session to have multiple desktops. Getting this information just requires that you get the current user's desktop. 

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, January 14, 2019 3:37 PM
    Moderator
  • okay, but how do I get the current user's desktop? thanks

    DYY

    Monday, January 14, 2019 7:17 PM
  • You can use the EnumDesktops to enumerate the desktops that are open for a user. To get the user's station you can use the GetProcessWorkStation, assuming they are running your app. How the returned desktop(s) map to the task view I don't know though. The task view feature may not even rely on the Win32 desktop implementation so this is a test more than anything.

    To make these calls you'll have to use P/Invoke which unfortunately requires more effort. Use the Pinvoke.net site to get the P/Invoke signatures for the functions mentioned earlier. Sometimes there are also examples of how to call them from .NET to help you out.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, January 14, 2019 8:08 PM
    Moderator
  • I put a quick and dirty app together to see if task view is using the desktops and it doesn't seem to be.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main ( string[] args )
            {
                var tasks = new TaskView();
    
                foreach (var desktop in tasks.GetDesktops())
                    Console.WriteLine(desktop);
    
                Console.ReadLine();
            }        
        }
    
        class TaskView
        {
            public TaskView ()
            {
                _station = new Lazy<IntPtr>(GetUserStation);
            }
    
            public IEnumerable<string> GetDesktops ()
            {
                var enumor = new DesktopEnumerator(_station.Value);
    
                return enumor.GetDesktops();
            }
    
            [DllImport("user32.dll", SetLastError = true)]
            private static extern IntPtr GetProcessWindowStation ();
                            
            private IntPtr GetUserStation ()
            {
                var station = GetProcessWindowStation();
                if (station == IntPtr.Zero)
                {
                    var error = Marshal.GetLastWin32Error();
                    throw new Win32Exception(error);
                };
    
                return station;
            }
    
            private Lazy<IntPtr> _station;
        }
    
        internal class DesktopEnumerator
        {
            public DesktopEnumerator ( IntPtr instance )
            {
                _instance = instance;
            }
    
            public IEnumerable<string> GetDesktops ( )
            {
                _items.Clear();
    
                if (!EnumDesktops(_instance, EnumDesktop, IntPtr.Zero))
                {
                    var error = Marshal.GetLastWin32Error();
                    throw new Win32Exception(error);
                };
    
                return _items;
            }
    
            [DllImport("user32.dll", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool EnumDesktops ( IntPtr instance, EnumDesktopDelegate function, IntPtr lParam );
    
            [return: MarshalAs(UnmanagedType.Bool)]
            private delegate bool EnumDesktopDelegate ( string desktop, IntPtr lParam );
    
            private bool EnumDesktop ( string desktop, IntPtr lParam )
            {
                _items.Add(desktop);
                return true;
            }
    
    
            private readonly IntPtr _instance;
            private readonly List<string> _items = new List<string>();
        }
    }
    


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, January 14, 2019 8:49 PM
    Moderator