根据我的研究,我在 webbrower.document 中找不到名为 CurrentViewMode 的直接属性。
但是,我找到了另一种方法来更改 webbrowser 的视图。
这是一个代码示例,您可以参考。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private const int LV_VIEW_ICON = 0x0;
private const int LV_VIEW_DETAILS = 0x1;
private const int LV_VIEW_SMALLICON = 0x2;
private const int LV_VIEW_LIST = 0x3;
private const int LV_VIEW_TILE = 0x4;
private const int EM_HIDEBALLOONTIP = 0x1504;
private const int LVM_SETVIEW = 0x108E;
private const string ListViewClassName ="SysListView32";
private static readonly HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(HandleRef hwndParent, EnumChildrenCallback lpEnumFunc, HandleRef lParam);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern uint RealGetWindowClass(IntPtr hwnd, [Out] StringBuilder pszType,uint cchType);
private delegate bool EnumChildrenCallback(IntPtr hwnd, IntPtr lParam);
private HandleRef listViewHandle;
private void FindListViewHandle()
{
this.listViewHandle = NullHandleRef;
EnumChildrenCallback lpEnumFunc = new EnumChildrenCallback(EnumChildren);
EnumChildWindows(new HandleRef(this.webBrowser1, this.webBrowser1.Handle), lpEnumFunc, NullHandleRef);
}
private bool EnumChildren(IntPtr hwnd, IntPtr lparam)
{
StringBuilder sb = new StringBuilder(100);
RealGetWindowClass(hwnd, sb, 100);
if (sb.ToString() == ListViewClassName)
this.listViewHandle = new HandleRef(null, hwnd);
return true;
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("D:/pdf");
comboBox1.Items.Add(LV_VIEW_ICON);
comboBox1.Items.Add(LV_VIEW_DETAILS);
comboBox1.Items.Add(LV_VIEW_SMALLICON);
comboBox1.Items.Add(LV_VIEW_LIST);
comboBox1.Items.Add(LV_VIEW_TILE);
}
private void button1_Click(object sender, EventArgs e)
{
FindListViewHandle();
int view = Convert.ToInt32(comboBox1.SelectedItem);
SendMessage((IntPtr)this.listViewHandle, LVM_SETVIEW, (IntPtr)view, (IntPtr)0);
}
}
更新代码说明:
首先,我们使用FindListViewHandle()在浏览器中找到listview控件的句柄(listViewHandle),所以我使用EnumChildWindows方法来迭代子控件。
其次,我们可以使用combobox1来存储不同的listview样式。
第三,消息LVM_SETVIEW表示设置了一个list-view控件的视图。
最后,SendMessage 方法会在 webbrowser 中找到 listviewcontrol,它会设置 list-view 控件的视图,并通过组合框值设置其样式。
这里有一些参考来自微软文档,你可以参考。
LVM_SETVIEW message
EnumChildWindows
SendMessage
结果: