0

以下は、Windows XP では問題なく動作するが、Windows 7 では動作しないコードです。Windows 7 でこれを修正する方法、またはおそらく別のアプローチを知りたいです...

一般的な OpenFileDialog ブラウザーを起動し、リスト内の特定のファイルを事前に選択したい C# アプリケーションを開発しています。私が知る限り、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