devwiki:cs

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
devwiki:cs [2022/05/02 09:05] – [C# Basic] yingdevwiki:cs [2022/05/07 06:53] (current) – [C# Basic] ying
Line 65: Line 65:
 // c# has Stack object for Push() and Pop() // c# has Stack object for Push() and Pop()
 // c# extra: SortedSet, HashSet // c# extra: SortedSet, HashSet
 +</code>
 +
 +  * timestamp to datetime conversion <code csharp>
 +using System;
 +public DateTime UnixTimeStampToDateTime(double unixTimeStamp)
 +{
 +  // Unix timestamp is seconds past epoch
 +  DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
 +  dateTime = dateTime.AddSeconds(unixTimeStamp).ToLocalTime();
 +  return dateTime;
 +}
 </code> </code>
 ====== C# and Visual Studio Community edition ====== ====== C# and Visual Studio Community edition ======
Line 297: Line 308:
  
 </code> </code>
 +
 +====== My App - Output Selected Item in Top Most Explorer in CSharp ======
 +
 +<code csharp Program.cs>
 +using System;
 +using System.Collections.Generic;
 +using System.Linq;
 +using System.Text;
 +
 +/*
 +ListSelection: (By Shining Ying)
 +  * v0.1: (2022.05.01) list selected item in top most explorer
 +
 +use with python:
 +import subprocess
 +app = r"D:\Projects\Dev\ListSelection\ListSelection\bin\Release\ListSelection.exe"
 +info=subprocess.Popen( '{0}'.format(app),stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE )
 +out, err = info.communicate()
 +result_list = [x.strip() for x in out.split('\n') if x.strip()!='' and not x.strip().startswith('#') ]
 +
 + */
 +namespace ListSelection
 +{
 +    
 +    class Program
 +    {
 +        // need user32 to use get foreground window
 +        [System.Runtime.InteropServices.DllImport("user32.dll")]
 +        private static extern IntPtr GetForegroundWindow();
 +
 +        [System.Runtime.InteropServices.DllImport("user32.dll")]
 +        private static extern IntPtr GetTopWindow(IntPtr hWnd);
 +
 +        [System.Runtime.InteropServices.DllImport("user32.dll")]
 +        private static extern bool IsWindowVisible(IntPtr hWnd);
 +
 +        [System.Runtime.InteropServices.DllImport("user32.dll")]
 +        static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
 +
 +        enum GetWindow_Cmd : uint
 +        {
 +            GW_HWNDFIRST = 0,
 +            GW_HWNDLAST = 1,
 +            GW_HWNDNEXT = 2,
 +            GW_HWNDPREV = 3,
 +            GW_OWNER = 4,
 +            GW_CHILD = 5,
 +            GW_ENABLEDPOPUP = 6
 +        }
 +
 +        // stathread needed to fix unable to cast com object error, as it needs sta thread to use shell object
 +        // https://stackoverflow.com/questions/31403956/exception-when-using-shell32-to-get-file-extended-properties
 +        [STAThread]
 +        static void Main(string[] args)
 +        {
 +            List<IntPtr> order_list = new List<IntPtr>();
 +            /*IntPtr fg_handle = GetForegroundWindow();*/
 +            IntPtr top_handle = GetTopWindow((IntPtr)null);
 +            if (top_handle != IntPtr.Zero)
 +            {
 +                //Console.WriteLine((int)top_handle);
 +                if (IsWindowVisible(top_handle))
 +                {
 +                    order_list.Add(top_handle);
 +                }
 +            }
 +
 +            IntPtr top_next = GetWindow(top_handle, 2);
 +            while (top_next != IntPtr.Zero) {
 +                //Console.WriteLine((int)top_next);
 +                if (IsWindowVisible(top_next)) { 
 +                    order_list.Add(top_next);
 +                }
 +                top_next = GetWindow(top_next, 2);
 +            }
 +            /*
 +            Console.WriteLine("===========================");
 +            Console.WriteLine("Order:");
 +            for (int i = 0; i < order_list.Count; i++)
 +            {
 +                Console.WriteLine((int)order_list[i]);
 +            }
 +            */
 +            
 +
 +            Shell32.Shell shell = new Shell32.Shell();
 +            // fix reference: right click project, Add > Reference; in COM, check on Microsoft Shell Controls and Automation
 +            List<int> win_list = new List<int>();
 +            foreach (SHDocVw.InternetExplorer window in shell.Windows())
 +            {
 +                win_list.Add(window.HWND);
 +            }
 +            /*
 +            Console.WriteLine("===========================");
 +            Console.WriteLine("win:");
 +            for (int i = 0; i < win_list.Count; i++)
 +            {
 +                Console.WriteLine(win_list[i]);
 +            }
 +            */
 +
 +            // top most explorer
 +            int top_explorer = 0;
 +            for (int i = 0; i < order_list.Count; i++)
 +            {
 +                if (win_list.Contains((int)order_list[i])) {
 +                    top_explorer = (int)order_list[i];
 +                    break;
 +                }
 +            }
 +
 +            List<string> selected = new List<string>();
 +            foreach (SHDocVw.InternetExplorer window in shell.Windows())
 +            {
 +                // fix SHDocVw: right click project, Add > Reference; in COM, check on Microsoft Internet Controls
 +                if (window.HWND == top_explorer)
 +                {
 +                    Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
 +                    foreach (Shell32.FolderItem item in items)
 +                    {
 +                        selected.Add(item.Path);
 +                        Console.WriteLine(item.Path);
 +                    }
 +                }
 +            }
 +
 +            Console.WriteLine("# By Shining Ying. - 2022/05/01");
 +            //return (string.Join(", ", selected.ToArray()));
 +
 +        }
 +    }
 +}
 +</code>
 +
  
  • devwiki/cs.1651482338.txt.gz
  • Last modified: 2022/05/02 09:05
  • by ying