获得所选文件夹的项目进行WinAPI的 [英] Get selected items of folder with WinAPI

查看:205
本文介绍了获得所选文件夹的项目进行WinAPI的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我试图让该用户正在使用的文件夹中选定的文件。我有以下的code已经运行,但只能在桌面上的文件:

Hey guys I try to get the selected files of a folder which the user is using. I have the following code which is already running, but only on desktop files:

private string selectedFiles()
{
    // get the handle of the desktop listview
    IntPtr vHandle = WinApiWrapper.FindWindow("Progman", "Program Manager");
    vHandle = WinApiWrapper.FindWindowEx(vHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
    vHandle = WinApiWrapper.FindWindowEx(vHandle, IntPtr.Zero, "SysListView32", "FolderView");

    //IntPtr vHandle = WinApiWrapper.GetForegroundWindow();

    //Get total count of the icons on the desktop
    int vItemCount = WinApiWrapper.SendMessage(vHandle, WinApiWrapper.LVM_GETITEMCOUNT, 0, 0);
    //MessageBox.Show(vItemCount.ToString());
    uint vProcessId;
    WinApiWrapper.GetWindowThreadProcessId(vHandle, out vProcessId);
    IntPtr vProcess = WinApiWrapper.OpenProcess(WinApiWrapper.PROCESS_VM_OPERATION | WinApiWrapper.PROCESS_VM_READ |
    WinApiWrapper.PROCESS_VM_WRITE, false, vProcessId);
    IntPtr vPointer = WinApiWrapper.VirtualAllocEx(vProcess, IntPtr.Zero, 4096,
    WinApiWrapper.MEM_RESERVE | WinApiWrapper.MEM_COMMIT, WinApiWrapper.PAGE_READWRITE);
    try
    {
        for (int j = 0; j < vItemCount; j++)
        {
            byte[] vBuffer = new byte[256];
            WinApiWrapper.LVITEM[] vItem = new WinApiWrapper.LVITEM[1];
            vItem[0].mask = WinApiWrapper.LVIF_TEXT;
            vItem[0].iItem = j;
            vItem[0].iSubItem = 0;
            vItem[0].cchTextMax = vBuffer.Length;
            vItem[0].pszText = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(WinApiWrapper.LVITEM)));
            uint vNumberOfBytesRead = 0;
            WinApiWrapper.WriteProcessMemory(vProcess, vPointer,
            Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0),
            Marshal.SizeOf(typeof(WinApiWrapper.LVITEM)), ref vNumberOfBytesRead);
            WinApiWrapper.SendMessage(vHandle, WinApiWrapper.LVM_GETITEMW, j, vPointer.ToInt32());
            WinApiWrapper.ReadProcessMemory(vProcess,
            (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(WinApiWrapper.LVITEM))),
            Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),
            vBuffer.Length, ref vNumberOfBytesRead);
            string vText = Encoding.Unicode.GetString(vBuffer, 0,
            (int)vNumberOfBytesRead);
            string IconName = vText;

            //Check if item is selected
            var result = WinApiWrapper.SendMessage(vHandle, WinApiWrapper.LVM_GETITEMSTATE, j, (int)WinApiWrapper.LVIS_SELECTED);
            if (result == WinApiWrapper.LVIS_SELECTED)
            {
                return vText;
            }
        }
    }
    finally
    {
        WinApiWrapper.VirtualFreeEx(vProcess, vPointer, 0, WinApiWrapper.MEM_RELEASE);
        WinApiWrapper.CloseHandle(vProcess);
    }
    return String.Empty;
}

我就先用GetForegroundWindow()的窗口句柄,然后调用SHELLDLL_DefView没有成功。

I tried to get the window handle with GetForegroundWindow() and then call the SHELLDLL_DefView without success.

所以,我怎么可以改变的第一个3行,让我当前文件夹的手柄用?

So how can I change the first 3 rows to get me the handle of the current folder in use?

推荐答案

这是很多黑客做一些明确由各个外壳对象和接口支持。授予的文档不让它轻易发现,但功能是存在的。雷蒙德陈写了一大篇有关使用这些接口。似乎没有成为一个方式来获得的当前文件夹中,但我想你可以得到HWNDs,看看是否有是前台窗口。

That's a lot of hacking to do something that is explicitly supported by the various shell objects and interfaces. Granted the documentation doesn't make it easily discoverable, but the functionality is there. Raymond Chen wrote a great article about using these interfaces. There doesn't appear to be a way to get the "current" folder, though I guess you could get the HWNDs and see if any is the foreground window.

这篇关于获得所选文件夹的项目进行WinAPI的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆