none
WebBrowser to view local files

    Question

  • Hi,

    I’m using the WebBrowser control to view local files, whenever I specify a location and files are loaded in the WebBrowser the files are shown in Tiles view, I would like to change that pragmatically to List view. Is there a setting or a function that I can use to accomplish this?

     

    Thx in Advance.

    Friday, July 06, 2007 7:32 PM

Answers

  • Hi roman rus

     

    The code below is an example which illustrates how to do the work. For more information about P/Invoke, you can refer to the links below.

     

    Code Snippet

        public partial class MainForm : Form

        {

            private const int LV_VIEW_ICON = 0x0000;

            private const int LV_VIEW_DETAILS = 0x0001;

            private const int LV_VIEW_SMALLICON = 0x0002;

            private const int LV_VIEW_LIST = 0x0003;

            private const int LV_VIEW_TILE = 0x0004;

            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", ExactSpelling = true)]

            private static extern bool EnumChildWindows(HandleRef hwndParent, EnumChildrenCallback lpEnumFunc, HandleRef lParam);

     

            [DllImport("user32.dll", CharSet = CharSet.Auto)]

            private static extern int SendMessage(HandleRef hWnd, uint Msg, int wParam, int lParam);

     

            [DllImport("user32.dll", CharSet = CharSet.Auto)]

            private static extern uint RealGetWindowClass(IntPtr hwnd, [Out] StringBuilder pszType, uint cchType);

     

            private delegate bool EnumChildrenCallback(IntPtr hwnd, IntPtr lParam);

     

            private HandleRef listViewHandle;

     

            public MainForm()

            {

                InitializeComponent();

            }

     

            private void RadioButton_CheckedChanged(object sender, EventArgs e)

            {

                FindListViewHandle();

     

                if (this.listViewHandle.Handle != IntPtr.Zero)

                {

                    // we found windows list view

     

                    int view = 0;

     

                    if (this.detailsRadioButton.Checked)

                        view = LV_VIEW_DETAILS;

                    else if (this.largeIconRadioButton.Checked)

                        view = LV_VIEW_ICON;

                    else if (this.listRadioButton.Checked)

                        view = LV_VIEW_LIST;

                    else if (this.smallIconRadioButton.Checked)

                        view = LV_VIEW_SMALLICON;

                    else if (this.tileRadioButton.Checked)

                        view = LV_VIEW_TILE;

     

                    SendMessage(this.listViewHandle, LVM_SETVIEW, view, 0);

                }

            }

     

            private void FindListViewHandle()

            {

                this.listViewHandle = NullHandleRef;

     

                EnumChildrenCallback lpEnumFunc = new EnumChildrenCallback(EnumChildren);

                EnumChildWindows(new HandleRef(this.webBrowser, this.webBrowser.Handle), lpEnumFunc, NullHandleRef);

            }

     

            private bool EnumChildren(IntPtr hwnd, IntPtr lparam)

            {

                StringBuilder sb = new StringBuilder(100);

                RealGetWindowClass(hwnd, sb, 100);

                if (sb.ToString() == ListViewClassName) // is this a windows list view?

                {

                    // this is a windows list view control

                    this.listViewHandle = new HandleRef(null, hwnd);

                }

                return true;

            }

        }

     

    More information links:

    http://msdn2.microsoft.com/en-us/library/ms229610(VS.80).aspx

    http://msdn2.microsoft.com/en-us/library/ms172270(VS.80).aspx

     

    Regards

    Wei Zhou

    Thursday, July 12, 2007 8:31 AM

All replies

  • Hi roman rus

     

    Most basically, the .NET WebBrowser control is a Windows Internet Explorer ActiveX WebBrowser control. The WebBrowser control use a windows SysListView control to display local files. So we can use P/Invoke technology to archieve our goal.

     

    Regards

    Wei Zhou

    Monday, July 09, 2007 11:07 AM
  • Thx for the quick answer,

    How would i use P/Invoke to do this? I never used P/Invoke before.

    Monday, July 09, 2007 2:30 PM
  • Hi roman rus

     

    The code below is an example which illustrates how to do the work. For more information about P/Invoke, you can refer to the links below.

     

    Code Snippet

        public partial class MainForm : Form

        {

            private const int LV_VIEW_ICON = 0x0000;

            private const int LV_VIEW_DETAILS = 0x0001;

            private const int LV_VIEW_SMALLICON = 0x0002;

            private const int LV_VIEW_LIST = 0x0003;

            private const int LV_VIEW_TILE = 0x0004;

            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", ExactSpelling = true)]

            private static extern bool EnumChildWindows(HandleRef hwndParent, EnumChildrenCallback lpEnumFunc, HandleRef lParam);

     

            [DllImport("user32.dll", CharSet = CharSet.Auto)]

            private static extern int SendMessage(HandleRef hWnd, uint Msg, int wParam, int lParam);

     

            [DllImport("user32.dll", CharSet = CharSet.Auto)]

            private static extern uint RealGetWindowClass(IntPtr hwnd, [Out] StringBuilder pszType, uint cchType);

     

            private delegate bool EnumChildrenCallback(IntPtr hwnd, IntPtr lParam);

     

            private HandleRef listViewHandle;

     

            public MainForm()

            {

                InitializeComponent();

            }

     

            private void RadioButton_CheckedChanged(object sender, EventArgs e)

            {

                FindListViewHandle();

     

                if (this.listViewHandle.Handle != IntPtr.Zero)

                {

                    // we found windows list view

     

                    int view = 0;

     

                    if (this.detailsRadioButton.Checked)

                        view = LV_VIEW_DETAILS;

                    else if (this.largeIconRadioButton.Checked)

                        view = LV_VIEW_ICON;

                    else if (this.listRadioButton.Checked)

                        view = LV_VIEW_LIST;

                    else if (this.smallIconRadioButton.Checked)

                        view = LV_VIEW_SMALLICON;

                    else if (this.tileRadioButton.Checked)

                        view = LV_VIEW_TILE;

     

                    SendMessage(this.listViewHandle, LVM_SETVIEW, view, 0);

                }

            }

     

            private void FindListViewHandle()

            {

                this.listViewHandle = NullHandleRef;

     

                EnumChildrenCallback lpEnumFunc = new EnumChildrenCallback(EnumChildren);

                EnumChildWindows(new HandleRef(this.webBrowser, this.webBrowser.Handle), lpEnumFunc, NullHandleRef);

            }

     

            private bool EnumChildren(IntPtr hwnd, IntPtr lparam)

            {

                StringBuilder sb = new StringBuilder(100);

                RealGetWindowClass(hwnd, sb, 100);

                if (sb.ToString() == ListViewClassName) // is this a windows list view?

                {

                    // this is a windows list view control

                    this.listViewHandle = new HandleRef(null, hwnd);

                }

                return true;

            }

        }

     

    More information links:

    http://msdn2.microsoft.com/en-us/library/ms229610(VS.80).aspx

    http://msdn2.microsoft.com/en-us/library/ms172270(VS.80).aspx

     

    Regards

    Wei Zhou

    Thursday, July 12, 2007 8:31 AM
  • great example but how would you do the "vista zoom thing" with the SysListView control. I mean all the icons appear at the minimum size, but what if i want "Extra Large Icons".

    O for anyone that is trying to get that example working, try setting your webbrowser controls Url property to something like "file:///G:/", where G:\ is a harddrive path i have, which makes the webbrowser become a folder browser, and then the code kicks in with finding the SysListView control handle.
    Wednesday, December 24, 2008 12:03 AM
  • Don't forget to add the statement

    using System.Runtime.InteropServices;
    
    

    Create a web form and add a browser named "webBrowser"
    Add 5 radion buttons named: detailsRadioButton, largeIconRadioButton, listRadioButton, smallIconRadioButton, and tileRadioButton.
    Wire up each checkChanged event to the RadioButton_CheckChanged event handler above.

    Thanks for the example Wei!!!
    Wednesday, April 08, 2009 11:11 AM
  • Hi Wei.

    I created my app in VS 2008 using C# and 3.5 framework. The app displays websites and folders. My goal was to have the folder display as details by default.

    Your code seems to work well, but I am having some unusual 'side effects' as follows:

    * when I select Details or List the icons don't display
    * some drop down selections won't display in detail view unless I right click on the web browser control.

    Any suggestions? Thank you, WHEELS

     

    Monday, November 09, 2009 4:43 PM
  • This code almost works.  In VS 2008 when I use this code, and navigate to file://C:/  nothing shows until I right click on the right window, then the details show up.  I put the setView code into the document complete event.  Question why doesn't sendmessage effect a refresh?  And why does a right click event show the files?
    Javaman
    Wednesday, January 27, 2010 3:45 PM