I am trying to get a list of selected files from the Windows Desktop and the Explorer Windows.
The requirement is that I should be able to retrieve the current selection from the active explorer window or the Desktop.
I have managed to put together the following code, after going through online resources, but it does not provide a list of selected items from the Desktop.
ArrayList selected = new ArrayList();
var shell = new Shell32.Shell();
IntPtr handle = IntPtr.Zero;
handle = GetForegroundWindow();
int intHandle = handle.ToInt32();
//For each explorer
foreach (InternetExplorer window in new ShellWindowsClass())
{
if (window.HWND == (int)handle)
{
Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
foreach (Shell32.FolderItem item in items)
{
selected.Add(item.Path);
}
}
}
Other than that, I tried the following but it just gives a list of all selected elements in all open explorer windows while ignoring the Desktop.
string filename; = Path.GetFileNameWithoutExtension(window.FullName).ToLower();
if (filename.ToLowerInvariant() == "explorer")
{
Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
foreach (Shell32.FolderItem item in items)
{
//MessageBox.Show(item.Path.ToString());
selected.Add(item.Path);
}
}
So I just always end up with a list from the explorer windows and get no results even when no explorer windows are open. The current techniques seem to be ignoring the Desktop altogether.
I would really appreciate it if someone could help me out to get a list of selected files from the currently active window/desktop.
Thank You.
It is easy for desktop since it is still a listview, just find the correct handle. list view is a child of the desktop handle.
Desktop
+- Progman (for backward compatibility)
+- Shell Def View
+- SysListView32 (even under 64 bit)
then you can do all listview operations on the list view. but other explorer windows does not contain a list view. Instead they use window with class DirectUIHWND which is a mystery to many. I've just found a post that describes a way to the unravel that mystery.
http://smartbear.com/forums?forumid=81&threadid=68427#68428
I hope it helps.
I think you should communicate between processes.
Following topics will help.
This is an example of retrieving icons from desktop. List of desktop items and their current positions are fetched.
http://social.msdn.microsoft.com/Forums/windows/en-US/d7df8a4d-fc0f-4b62-80c9-7768756456e6/how-can-i-get-desktops-icons-information-?forum=winforms
Here the parameter LVM_GETITEMSTATE can be used within the sample code from the link above.
http://msdn.microsoft.com/en-us/library/windows/desktop/bb761053(v=vs.85).aspx
Good luck..
using System.Runtime.InteropServices;
public class ShellItems
{
[StructLayoutAttribute(LayoutKind.Sequential)]
private struct LVITEM
{
public uint mask;
public int iItem;
public int iSubItem;
public uint state;
public uint stateMask;
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public IntPtr lParam;
}
const int LVM_FIRST = 0x1000;
const int LVM_GETSELECTEDCOUNT = 4146;
const int LVM_GETNEXTITEM = LVM_FIRST + 12;
const int LVNI_SELECTED = 2;
const int LVM_GETITEMCOUNT = LVM_FIRST + 4;
const int LVM_GETITEM = LVM_FIRST + 75;
const int LVIF_TEXT = 0x0001;
[DllImport("user32.dll", EntryPoint = "GetShellWindow")]
public static extern System.IntPtr GetShellWindow();
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
public static extern int SendMessagePtr(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);
public int SelectedItemCount
{
get
{
return SendMessage(ShellListViewHandle, LVM_GETSELECTEDCOUNT, IntPtr.Zero.ToInt32(), IntPtr.Zero.ToInt32());
}
}
public int Count
{
get
{
return SendMessage(ShellListViewHandle, LVM_GETITEMCOUNT, IntPtr.Zero.ToInt32(), IntPtr.Zero.ToInt32());
}
}
public string GetItemText(int idx)
{
// Declare and populate the LVITEM structure
LVITEM lvi = new LVITEM();
lvi.mask = LVIF_TEXT;
lvi.cchTextMax = 512;
lvi.iItem = idx; // the zero-based index of the ListView item
lvi.iSubItem = 0; // the one-based index of the subitem, or 0 if this
// structure refers to an item rather than a subitem
lvi.pszText = Marshal.AllocHGlobal(512);
// Send the LVM_GETITEM message to fill the LVITEM structure
IntPtr ptrLvi = Marshal.AllocHGlobal(Marshal.SizeOf(lvi));
Marshal.StructureToPtr(lvi, ptrLvi, false);
try
{
SendMessagePtr(ShellListViewHandle, LVM_GETITEM, IntPtr.Zero, ptrLvi);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
// Extract the text of the specified item
string itemText = Marshal.PtrToStringAuto(lvi.pszText);
return itemText;
}
IntPtr ShellListViewHandle
{
get
{
IntPtr _ProgMan = GetShellWindow();
IntPtr _SHELLDLL_DefViewParent = _ProgMan;
IntPtr _SHELLDLL_DefView = FindWindowEx(_ProgMan, IntPtr.Zero, "SHELLDLL_DefView", null);
IntPtr _SysListView32 = FindWindowEx(_SHELLDLL_DefView, IntPtr.Zero, "SysListView32", "FolderView");
return _SysListView32;
}
}
public int GetSelectedItemIndex(int iPos = -1)
{
return SendMessage(ShellListViewHandle, LVM_GETNEXTITEM, iPos, LVNI_SELECTED);
}
}
Related
I need to check what window the user currently has selected, and do stuff if they have a specific program selected.
I haven't used the GetForegroundWindow function before, and can't find any information on how to use it in this manner.
I simply need an if comparing the current window to see if its a specific program. However the GetForegroundWindow function doesn't give back a string or int it seems. So mainly I don't know how to find out the value of the program window I want to compare it to.
I currently have the code to get the current window:
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
IntPtr selectedWindow = GetForegroundWindow();
I need to be able to apply it as follows ideally:
If (selectedWindow!="SpecificProgram")
{
<Do this stuff>
}
I'm hoping the GetForegroundWindow value/object is unique to each program and doesn't function in some way that each specific program/window has different values each-time.
I'm also doing this as part of a windows form though I doubt it matters.
-Thanks for any help
Edit: This way works, and uses the tile of the current window, which makes it perfect for checking if the window is right easily:
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
private string GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
and then I can just do:
if (GetActiveWindowTitle()=="Name of Window")
{
DoStuff.jpg
}
It has some code but it works:
#region Retrieve list of windows
[DllImport("user32")]
private static extern int GetWindowLongA(IntPtr hWnd, int index);
[DllImport("USER32.DLL")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("USER32.DLL")]
private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
private const int GWL_STYLE = -16;
private const ulong WS_VISIBLE = 0x10000000L;
private const ulong WS_BORDER = 0x00800000L;
private const ulong TARGETWINDOW = WS_BORDER | WS_VISIBLE;
internal class Window
{
public string Title;
public IntPtr Handle;
public override string ToString()
{
return Title;
}
}
private List<Window> windows;
private void GetWindows()
{
windows = new List<Window>();
EnumWindows(Callback, 0);
}
private bool Callback(IntPtr hwnd, int lParam)
{
if (this.Handle != hwnd && (GetWindowLongA(hwnd, GWL_STYLE) & TARGETWINDOW) == TARGETWINDOW)
{
StringBuilder sb = new StringBuilder(100);
GetWindowText(hwnd, sb, sb.Capacity);
Window t = new Window();
t.Handle = hwnd;
t.Title = sb.ToString();
windows.Add(t);
}
return true; //continue enumeration
}
#endregion
And to check user window:
IntPtr selectedWindow = GetForegroundWindow();
GetWindows();
for (i = 0; i < windows.Count; i++)
{
if(selectedWindow == windows[i].Handle && windows[i].Title == "Program Title X")
{
//Do stuff
break;
}
}
Valter
I've been struggling with this issue for quite a long time and I still can't find a decent documentation or example for this matter.
I'm simply trying to get the node text of a tree view located on another application using WinApi in C# and I can't find a way of doing it anywhere.
I tried exploring the TVM_GETITEM message but I couldn't get the TVITEM out of it.
How can I achieve such a thing?
So I ran into this problem myself and thought I'd share what I found. It requires getting the IntPtr handle of the TreeView control first, so this example assumes that part is done.
private string getTVNodeText(IntPtr tvHandle, IntPtr nodeHandle)
{
TVITEM tvi = new TVITEM();
IntPtr pszText = LocalAlloc(0x40, 260);
tvi.mask = TreeViewMsg.TVIF_TEXT;
tvi.hItem = nodeHandle;
tvi.cchTextMax = 260;
tvi.pszText = Convert.ToInt32(pszText);
SendMessageTVI(tvHandle, TreeViewMsg.TVM_GETITEM, 0, tvi);
string nodeText = Marshal.PtrToStringAnsi((IntPtr)tvi.pszText, 260);
LocalFree(pszText);
return nodeText;
}
Here are the PInvoke declarations you'll need:
[DllImport("user32", EntryPoint = "SendMessageW")]
public int SendMessageTVI(IntPtr hWnd, int wMsg, int wParam, ref TVITEM tvi { }
[DllImport("kernel32.dll")]
public IntPtr LocalAlloc(uint flags, uint cb) { }
[DllImport("kernel32.dll")]
public IntPtr LocalFree(IntPtr p) { }
Here's the TVITEM structure
public struct TVITEM
{
public int mask;
public int hItem;
public int state;
public int stateMask;
public int pszText;
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public int lParam;
public int iIntegral;
}
Here are the constants for the SendMessage calls
[Flags()]
public enum TreeViewMsg
{
BN_CLICKED = 0xf5,
TV_CHECKED = 0x2000,
TV_FIRST = 0x1100,
TVGN_ROOT = 0x0,
TVGN_NEXT = 0x1,
TVGN_CHILD = 0x4,
TVGN_FIRSTVISIBLE = 0x5,
TVGN_NEXTVISIBLE = 0x6,
TVGN_CARET = 0x9,
TVM_SELECTITEM = (TV_FIRST + 11),
TVM_GETNEXTITEM = (TV_FIRST + 10),
TVM_GETITEM = (TV_FIRST + 12),
TVIF_TEXT = 0x1
}
You can similarly look at the state of the TVITEM to determine if the node is checked. The TV_CHECKED constant (8192) worked for me, but I'd inspect this at runtime to see what the TVITEM.state value is when it's checked.
Here is the link to where I found this from someone trying to get the text back in Unicode.
I'm using PInvoke in C#, trying to read tooltips visible in a window with a known handler, but the apps who's windows I try to inspect in this manner crash with memory access violation errors, or simply don't reveal the tooltip text in the lpszText TOOLINFO member.
I'm calling EnumWindows with a callback and then sending a message to the tooltip window in that function:
public delegate bool CallBackPtr(IntPtr hwnd, IntPtr lParam);
static void Main(string[] args)
{
callBackPtr = new CallBackPtr(Report);
IntPtr hWnd = WindowFromPoint(<mouse coordinates point>);
if (hWnd != IntPtr.Zero)
{
Console.Out.WriteLine("Window with handle " + hWnd +
" and class name " +
getWindowClassName(hWnd));
EnumWindows(callBackPtr, hWnd);
Console.Out.WriteLine();
}
public static bool Report(IntPtr hWnd, IntPtr lParam)
{
String windowClassName = getWindowClassName(hWnd);
if (windowClassName.Contains("tool") &&
GetParent(hWnd) == lParam)
{
string szToolText = new string(' ', 250);
TOOLINFO ti = new TOOLINFO();
ti.cbSize = Marshal.SizeOf(typeof(TOOLINFO));
ti.hwnd = GetParent(hWnd);
ti.uId = hWnd;
ti.lpszText = szToolText;
SendMessage(hWnd, TTM_GETTEXT, (IntPtr)250, ref ti);
Console.WriteLine("Child window handle is " + hWnd + " and class name " + getWindowClassName(hWnd) + " and value " + ti.lpszText);
}
return true;
}
Here's how I defined the TOOLINFO structure:
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
private int _Left;
private int _Top;
private int _Right;
private int _Bottom;
}
struct TOOLINFO
{
public int cbSize;
public int uFlags;
public IntPtr hwnd;
public IntPtr uId;
public RECT rect;
public IntPtr hinst;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpszText;
public IntPtr lParam;
}
the TTM_GETTEXT value
private static UInt32 WM_USER = 0x0400;
private static UInt32 TTM_GETTEXT = (WM_USER + 56);
and the SendMessage overload
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, ref TOOLINFO lParam);
So, is there any obvious error that I'm missing in my code, what should I change so that this situation is resolved?
Edit: Here is the whole code, so you could test.
You are sending a private message across processes, which requires manual marshaling. Here's another stackoverflow question on the same topic. Better would be to change direction entirely and use Active Accessibility and/or UI Automation, which are designed for this sort of thing.
I ended up using UI Automation, as Raymond suggested. AutomationElement, who's Name property value contains the text in case of tooltips, proved to be exactly what the code required. I'm cycling through all the Desktop's child windows, where all the tooltips reside and I only display those that belong to the process that owns the window under the mouse:
public static bool Report(IntPtr hWnd, IntPtr lParam)
{
if (getWindowClassName(hWnd).Contains("tool"))
{
AutomationElement element = AutomationElement.FromHandle(hWnd);
string value = element.Current.Name;
if (value.Length > 0)
{
uint currentWindowProcessId = 0;
GetWindowThreadProcessId(currentWindowHWnd, out currentWindowProcessId);
if (element.Current.ProcessId == currentWindowProcessId)
Console.WriteLine(value);
}
}
return true;
}
static void Main(string[] args)
{
callBackPtr = new CallBackPtr(Report);
do
{
System.Drawing.Point mouse = System.Windows.Forms.Cursor.Position; // use Windows forms mouse code instead of WPF
currentWindowHWnd = WindowFromPoint(mouse);
if (currentWindowHWnd != IntPtr.Zero)
EnumChildWindows((IntPtr)0, callBackPtr, (IntPtr)0);
Thread.Sleep(1000);
}
while (true);
}
How can I use the images within shell32.dll in my C# project?
You can extract icons from a DLL with this code:
public class IconExtractor
{
public static Icon Extract(string file, int number, bool largeIcon)
{
IntPtr large;
IntPtr small;
ExtractIconEx(file, number, out large, out small, 1);
try
{
return Icon.FromHandle(largeIcon ? large : small);
}
catch
{
return null;
}
}
[DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons);
}
...
form.Icon = IconExtractor.Extract("shell32.dll", 42, true);
Of course you need to know the index of the image in the DLL...
This thread on the MSDN developer forums offers a solution:
The typical way to implement these in .NET is to use the graphics provided in the ZIP file located at C:\Program Files\Microsoft Visual Studio X\Common7\VS200XImageLibrary.
You don't state which version of Visual Studio you have installed but you'll need to replace the "200X" with your version number.
The code in the accepted answer leaks one icon handle each time it's called, as it always asks for two icon handles and only gives one back.
Here is a version that doesn't leak a handle:
public static Icon Extract(string filePath, int index, bool largeIcon = true)
{
if (filePath == null)
throw new ArgumentNullException(nameof(filePath));
IntPtr hIcon;
if (largeIcon)
{
ExtractIconEx(filePath, index, out hIcon, IntPtr.Zero, 1);
}
else
{
ExtractIconEx(filePath, index, IntPtr.Zero, out hIcon, 1);
}
return hIcon != IntPtr.Zero ? Icon.FromHandle(hIcon) : null;
}
[DllImport("shell32", CharSet = CharSet.Unicode)]
private static extern int ExtractIconEx(string lpszFile, int nIconIndex, out IntPtr phiconLarge, IntPtr phiconSmall, int nIcons);
[DllImport("shell32", CharSet = CharSet.Unicode)]
private static extern int ExtractIconEx(string lpszFile, int nIconIndex, IntPtr phiconLarge, out IntPtr phiconSmall, int nIcons);
Some of them are available in %Program Files%\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary - for others, you'd need to speak to Microsoft's lawyers about licensing them for redistribution in your application
See this code. It will be help
public class ExtractIcon
{
[DllImport("Shell32.dll")]
private static extern int SHGetFileInfo(
string pszPath, uint dwFileAttributes,
out SHFILEINFO psfi, uint cbfileInfo,
SHGFI uFlags);
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
public string szDisplayName;
public string szTypeName;
};
private enum SHGFI
{
SmallIcon = 0x00000001,
OpenIcon = 0x00000002,
LargeIcon = 0x00000000,
Icon = 0x00000100,
DisplayName = 0x00000200,
Typename = 0x00000400,
SysIconIndex = 0x00004000,
LinkOverlay = 0x00008000,
UseFileAttributes = 0x00000010
}
public static Icon GetIcon(string strPath, bool bSmall, bool bOpen)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (bSmall)
flags = SHGFI.Icon | SHGFI.SmallIcon;
else
flags = SHGFI.Icon | SHGFI.LargeIcon;
if (bOpen) flags = flags | SHGFI.OpenIcon;
SHGetFileInfo(strPath, 0, out info, (uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
}
I am basically writing a specialized macro player/recorder in C#. One thing I need to be able to do is wait for a pop up window (something like a Save As... dialog box) that I can then select to continue playing macro input into. Ideally, I would like to be able to poll for open windows and search through their titles for a matching window title.
Obviously I can't use Processes.GetProcesses() because a dialog most likely will not show up as a new process.
Where do I look to get open windows and their titles?
If you want to poll all open windows, you might use EnumWindows(). I didn't compile this code, but it should be pretty close to functional.
public class ProcessWindows
{
List<Window> visibleWindows = new List<Window>();
List<IntPtr> allWindows = new List<IntPtr>();
/// <summary>
/// Contains information about visible windows.
/// </summary>
public struct Window
{
public IntPtr Handle { get; set; }
public string Title { get; set; }
}
[DllImport("user32.dll")]
static extern int EnumWindows(EnumWindowsCallback lpEnumFunc, int lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern void GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
delegate bool EnumWindowsCallback(IntPtr hwnd, int lParam);
public ProcessWindows()
{
int returnValue = EnumWindows(Callback, 0);
if (returnValue == 0)
{
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "EnumWindows() failed");
}
}
private bool Callback(IntPtr hwnd, int lParam)
{
const int WS_BORDER = 0x800000;
const int WS_VISIBLE = 0x10000000;
const int GWL_STYLE = (-16);
// You'll have to figure out which windows you want here...
int visibleWindow = WS_BORDER | WS_VISIBLE;
if ((GetWindowLong(hwnd, GWL_STYLE) & visibleWindow) == visibleWindow)
{
StringBuilder sb = new StringBuilder(100);
GetWindowText(hwnd, sb, sb.Capacity);
this.visibleWindows.Add(new Window()
{
Handle = hwnd,
Title = sb.ToString()
});
}
return true; //continue enumeration
}
public ReadOnlyCollection<Window> GetVisibleWindows()
{
return this.visibleWindows.AsReadOnly();
}
}
}
I think you want FindWindow().