0

下面是一些在 Windows XP 上运行良好的代码,但在 Windows 7 上不起作用。我想知道如何为 Windows 7 解决这个问题,或者可能是不同的方法......

我正在开发一个 C# 应用程序,我想在其中启动一个通用 OpenFileDialog 浏览器并在列表中预先选择一个特定文件。据我所知,OpenFileDialog 本身不支持在列表中选择特定文件,所以我使用 Windows API 将消息直接发送到 ListView 的句柄。

在 UISpy 中,OpenFileDialog 窗口中的 ListView 在 Windows XP 中被标识为 ClassName=SysListView32。然而,在 Windows 7 中,此列表由 ClassName=DUIListView 标识。我似乎找不到 DUI 库的任何文档。

[DllImport("User32.dll")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct LV_ITEM
{
    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;
}

//get a handle to the ListView object 
//    findList is my own function which drills down through the UI tree for me
AutomationElement list = findList("Title of containing window", "Name of List in UISpy");
int handle_list = list.Current.NativeWindowHandle;

//some constants right out of CommCtrl.h
const int LVM_FIRST = 0x1000;
const int LVM_SETITEMSTATE = LVM_FIRST + 43;
const int LVIS_FOCUSED = 0x0001;
const int LVIS_SELECTED = 0x0002;

//build a LV_ITEM object to send in the API message
LV_ITEM lvi = new LV_ITEM();
lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
lvi.state = LVIS_FOCUSED | LVIS_SELECTED;

//pick the index number of the file to select in the ListView
int wParam = 8;

IntPtr ptrLvi = Marshal.AllocHGlobal(Marshal.SizeOf(lvi));
Marshal.StructureToPtr(lvi, ptrLvi, false);

//send the message to the ListView!!
SendMessage(new IntPtr(handle_list), LVM_SETITEMSTATE, new IntPtr(wParam), ptrLvi);

有谁知道与新的 DUIListView 对象交互的正确方法?有谁知道我可以采取不同的方法来完成这项任务?太感谢了。

4

0 回答 0