【问题标题】:Find the equivalent of CurrentViewMode in C#在 C# 中找到 CurrentViewMode 的等价物
【发布时间】:2021-07-31 21:17:07
【问题描述】:

我正在浏览 webBrowser 控件中的本地文件夹,并且我使用的是 .NET 4.7:

webBrowser1.Navigate("C:/some_folder");

此文件夹以垂直线显示 pdf,其中包含上次更新、大小和类型。 我只需要找到允许我在“马赛克视图”中显示此列表的属性,图标更大,并且没有任何额外信息,如上次更新、文档的大小或类型...... 经过一番挖掘后,我发现一些 vb.net 用户获得了 webBrowser.Document.CurrentViewMode 属性。我确实有 webBrowser.Document 但没有诸如 CurrentViewMode 之类的东西...我猜这应该是一件愚蠢的事情,因为这相当于右键单击并选择显示的文档列表的属性...)

感谢您的帮助

【问题讨论】:

  • 有更新吗?请检查我的答案是否适合您。
  • 嘿@JackJJun-MSFT,感谢您的回答,我回复了您的评论。如果您不介意,我想知道更多关于您使用的课​​程以及我如何适用于以下案例的信息

标签: c# .net visual-studio webbrowser-control


【解决方案1】:

根据我的研究,我在 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

结果:

【讨论】:

  • 首先,谢谢杰克。我只是想使您的代码适应我的使用。就我而言,基本上,我单击一个触发本地文件视图的图片框。正是在这一刻(仍在图片框的双击事件中),我需要展示您提出的一种观点。因此,我尝试了解您的代码中的内容。如果你不介意,稍微解释一下会加快我的搜索过程:)
  • @James06,抱歉回复晚了,我现在更新了答案中的一些解释。另外,如果要使用picturebox显示视图,可以将button_click更改为picturebox_Double_click事件。
  • 嗨,杰克,再次为我迟到的回复感到抱歉。确实我使用了您的解决方案,非常感谢您的分享
猜你喜欢
  • 1970-01-01
  • 2022-12-08
  • 1970-01-01
  • 1970-01-01
  • 2011-04-02
  • 2021-10-15
  • 2013-10-20
  • 2011-04-10
  • 2021-12-02
相关资源
最近更新 更多