James06
James06

Reputation: 47

Find the equivalent of CurrentViewMode in C#

I'm browsing a local folder within a webBrowser control and I'm using .NET 4.7:

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

This folder displays pdfs in vertical line with the last update, the size and the type. I just need to find the property that will allow me to display this list in "mosaic view" with the icons bigger and without any extra info like the last update, the size or the type of document ... After some digging I found out some vb.net users got the property webBrowser.Document.CurrentViewMode. I have indeed webBrowser.Document but nothing such as CurrentViewMode. It should be a silly thing I guess as this is the equivalent of right-clicking and choose properties of the displayed list of documents...)

Upvotes: 0

Views: 215

Answers (1)

Jack J Jun- MSFT
Jack J Jun- MSFT

Reputation: 5986

Based on my research, I can not find a direct property called CurrentViewMode in webbrower.document.

However, I find another way to change the view of webbrowser.

Here is a code example you can refer to.

 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);
        }
    }

Update the explanation for the code:

First, We used FindListViewHandle() to find the listview control's handle(listViewHandle) in the webbrowser, so I used EnumChildWindows method to iterate child control.

Second, we can use the combobox1 to store different listview style.

Third, the message LVM_SETVIEW indicates that sets the view of a list-view control.

Finally, SendMessage method will find the listviewcontrol in the webbrowser and it will set the view of list-view control and set its style by the combobox value.

Here are some reference come from Microsoft doc you can refer to.

LVM_SETVIEW message

EnumChildWindows

SendMessage

Result:

enter image description here

Upvotes: 0

Related Questions