How to simulate a mouse click on WebBrowser control using c#

kwok kan ho 21 Reputation points
2022-12-30T06:58:24.227+00:00

I am trying to simulate a mouse click on WebBrowser control.
I can move the mouse to the required position using Cursor.Position = new Point(this.Left + 200, this.Top + 170);
I try to use the mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, UIntPtr.Zero) to simulate the mouse click but failed.
My goal is to click on the "Date modified" button on the top of the WebBrowser control which I use it as a file explorer to display the image in the folder.

Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2022-12-30T17:05:02.853+00:00

    I did a test with [IExplorerBrowser][1]; I added a Button to test sorting by Modified Date
    You can improve/configure it (by hiding/showing panels or other things), change initial folder (pidlInit), etc... :

    ![275068-explorerbrowser.gif][2]

    public partial class Form1 : Form  
        {  
            public enum HRESULT : uint  
            {  
                S_OK = 0,  
                S_FALSE = 1,  
                E_NOINTERFACE = 0x80004002,  
                E_NOTIMPL = 0x80004001,  
                E_FAIL = 0x80004005,  
                E_INVALIDARG = 0x80070057,  
                E_UNEXPECTED = 0x8000FFFF  
            }  
      
            [StructLayout(LayoutKind.Sequential)]  
            public struct RECT  
            {  
                public int left;  
                public int top;  
                public int right;  
                public int bottom;  
                public RECT(int left, int top, int right, int bottom)  
                {  
                    this.left = left;  
                    this.top = top;  
                    this.right = right;  
                    this.bottom = bottom;  
                }  
            }  
      
            [ComImport]  
            [Guid("dfd3b6b5-c10c-4be9-85f6-a66969f402f6")]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            public interface IExplorerBrowser  
            {  
                HRESULT Initialize(IntPtr hwndParent, ref RECT prc, ref FOLDERSETTINGS pfs);  
                HRESULT Destroy();  
                HRESULT SetRect(IntPtr phdwp, ref RECT rcBrowser);  
                HRESULT SetPropertyBag(string pszPropertyBag);  
                HRESULT SetEmptyText(string pszEmptyText);  
                HRESULT SetFolderSettings(ref FOLDERSETTINGS pfs);  
      
                // IExplorerBrowserEvents *psbe  
                HRESULT Advise(IntPtr psbe, ref int pdwCookie);  
                HRESULT Unadvise(int dwCookie);  
                HRESULT SetOptions(EXPLORER_BROWSER_OPTIONS dwFlag);  
                HRESULT GetOptions(ref EXPLORER_BROWSER_OPTIONS pdwFlag);  
                HRESULT BrowseToIDList(IntPtr pidl, uint uFlags);  
      
                // IUnknown *punk,  
                HRESULT BrowseToObject(IntPtr punk, uint uFlags);  
                HRESULT FillFromObject(IntPtr punk, EXPLORER_BROWSER_FILL_FLAGS dwFlags);  
                HRESULT RemoveAll();  
                HRESULT GetCurrentView(ref Guid riid, ref IntPtr ppv);  
            }  
      
            public const int SBSP_ABSOLUTE = 0x0;  
      
            public enum EXPLORER_BROWSER_OPTIONS : int  
            {  
                EBO_NONE = 0,  
                EBO_NAVIGATEONCE = 0x1,  
                EBO_SHOWFRAMES = 0x2,  
                EBO_ALWAYSNAVIGATE = 0x4,  
                EBO_NOTRAVELLOG = 0x8,  
                EBO_NOWRAPPERWINDOW = 0x10,  
                EBO_HTMLSHAREPOINTVIEW = 0x20,  
                EBO_NOBORDER = 0x40,  
                EBO_NOPERSISTVIEWSTATE = 0x80  
            }  
      
            public enum EXPLORER_BROWSER_FILL_FLAGS : int  
            {  
                EBF_NONE = 0,  
                EBF_SELECTFROMDATAOBJECT = 0x100,  
                EBF_NODROPTARGET = 0x200  
            }  
      
            [StructLayout(LayoutKind.Sequential)]  
            public struct FOLDERSETTINGS  
            {  
                public int ViewMode;  
                public uint fFlags;  
            }  
      
            public enum FOLDERVIEWMODE  
            {  
                FVM_AUTO = -1,  
                FVM_FIRST = 1,  
                FVM_ICON = 1,  
                FVM_SMALLICON = 2,  
                FVM_LIST = 3,  
                FVM_DETAILS = 4,  
                FVM_THUMBNAIL = 5,  
                FVM_TILE = 6,  
                FVM_THUMBSTRIP = 7,  
                FVM_CONTENT = 8,  
                FVM_LAST = 8  
            }  
      
            public enum FOLDERFLAGS :uint  
            {  
                FWF_NONE = 0,  
                FWF_AUTOARRANGE = 0x1,  
                FWF_ABBREVIATEDNAMES = 0x2,  
                FWF_SNAPTOGRID = 0x4,  
                FWF_OWNERDATA = 0x8,  
                FWF_BESTFITWINDOW = 0x10,  
                FWF_DESKTOP = 0x20,  
                FWF_SINGLESEL = 0x40,  
                FWF_NOSUBFOLDERS = 0x80,  
                FWF_TRANSPARENT = 0x100,  
                FWF_NOCLIENTEDGE = 0x200,  
                FWF_NOSCROLL = 0x400,  
                FWF_ALIGNLEFT = 0x800,  
                FWF_NOICONS = 0x1000,  
                FWF_SHOWSELALWAYS = 0x2000,  
                FWF_NOVISIBLE = 0x4000,  
                FWF_SINGLECLICKACTIVATE = 0x8000,  
                FWF_NOWEBVIEW = 0x10000,  
                FWF_HIDEFILENAMES = 0x20000,  
                FWF_CHECKSELECT = 0x40000,  
                FWF_NOENUMREFRESH = 0x80000,  
                FWF_NOGROUPING = 0x100000,  
                FWF_FULLROWSELECT = 0x200000,  
                FWF_NOFILTERS = 0x400000,  
                FWF_NOCOLUMNHEADER = 0x800000,  
                FWF_NOHEADERINALLVIEWS = 0x1000000,  
                FWF_EXTENDEDTILES = 0x2000000,  
                FWF_TRICHECKSELECT = 0x4000000,  
                FWF_AUTOCHECKSELECT = 0x8000000,  
                FWF_NOBROWSERVIEWSTATE = 0x10000000,  
                FWF_SUBSETGROUPS = 0x20000000,  
                FWF_USESEARCHFOLDER = 0x40000000,  
                FWF_ALLOWRTLREADING = 0x80000000  
            }  
      
            [ComImport]  
            [Guid("cde725b0-ccc9-4519-917e-325d72fab4ce")]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            public interface IFolderView  
            {  
                HRESULT GetCurrentViewMode(ref uint pViewMode);  
                HRESULT SetCurrentViewMode(uint ViewMode);  
                HRESULT GetFolder(ref Guid riid, ref IntPtr ppv);  
                HRESULT Item(int iItemIndex, ref IntPtr ppidl);  
                HRESULT ItemCount(uint uFlags, ref int pcItems);  
                HRESULT Items(uint uFlags, ref Guid riid, ref IntPtr ppv);  
                HRESULT GetSelectionMarkedItem(ref int piItem);  
                HRESULT GetFocusedItem(ref int piItem);  
                HRESULT GetItemPosition(IntPtr pidl, ref Point ppt);  
                HRESULT GetSpacing(ref Point ppt);  
                HRESULT GetDefaultSpacing(ref Point ppt);  
                HRESULT GetAutoArrange();  
                HRESULT SelectItem(int iItem, int dwFlags);  
                HRESULT SelectAndPositionItems(uint cidl, IntPtr apidl, Point apt, int dwFlags);  
            }  
      
            [ComImport]  
            [Guid("1af3a467-214f-4298-908e-06b03e0b39f9")]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            public interface IFolderView2 : IFolderView  
            {  
                new HRESULT GetCurrentViewMode(ref uint pViewMode);  
                new HRESULT SetCurrentViewMode(uint ViewMode);  
                new HRESULT GetFolder(ref Guid riid, ref IntPtr ppv);  
                new HRESULT Item(int iItemIndex, ref IntPtr ppidl);  
                new HRESULT ItemCount(uint uFlags, ref int pcItems);  
                new HRESULT Items(uint uFlags, ref Guid riid, ref IntPtr ppv);  
                new HRESULT GetSelectionMarkedItem(ref int piItem);  
                new HRESULT GetFocusedItem(ref int piItem);  
                new HRESULT GetItemPosition(IntPtr pidl, ref Point ppt);  
                new HRESULT GetSpacing(ref Point ppt);  
                new HRESULT GetDefaultSpacing(ref Point ppt);  
                new HRESULT GetAutoArrange();  
                new HRESULT SelectItem(int iItem, int dwFlags);  
                new HRESULT SelectAndPositionItems(uint cidl, IntPtr apidl, Point apt, int dwFlags);  
                HRESULT SetGroupBy(PROPERTYKEY key, bool fAscending);  
                HRESULT GetGroupBy(ref PROPERTYKEY pkey, ref bool pfAscending);  
      
                // DEPRECATED  
                HRESULT SetViewProperty(IntPtr pidl, PROPERTYKEY propkey, PROPVARIANT propvar);  
                // DEPRECATED  
                HRESULT GetViewProperty(IntPtr pidl, PROPERTYKEY propkey, ref PROPVARIANT ppropvar);  
                // DEPRECATED  
                HRESULT SetTileViewProperties(IntPtr pidl, string pszPropList);  
                // DEPRECATED  
                HRESULT SetExtendedTileViewProperties(IntPtr pidl, string pszPropList);  
      
                HRESULT SetText(FVTEXTTYPE iType, string pwszText);  
                HRESULT SetCurrentFolderFlags(int dwMask, int dwFlags);  
                HRESULT GetCurrentFolderFlags(ref int pdwFlags);  
                HRESULT GetSortColumnCount(ref int pcColumns);  
                //HRESULT SetSortColumns(SORTCOLUMN rgSortColumns, int cColumns);  
                HRESULT SetSortColumns(IntPtr rgSortColumns, int cColumns);  
                //HRESULT GetSortColumns(ref SORTCOLUMN rgSortColumns, int cColumns);  
                HRESULT GetSortColumns(ref IntPtr rgSortColumns, int cColumns);  
                HRESULT GetItem(int iItem, ref Guid riid, ref IntPtr ppv);  
                HRESULT GetVisibleItem(int iStart, bool fPrevious, ref int piItem);  
                HRESULT GetSelectedItem(int iStart, ref int piItem);  
                HRESULT GetSelection(bool fNoneImpliesFolder, ref IShellItemArray ppsia);  
                HRESULT GetSelectionState(IntPtr pidl, ref int pdwFlags);  
                HRESULT InvokeVerbOnSelection(string pszVerb);  
                HRESULT SetViewModeAndIconSize(FOLDERVIEWMODE uViewMode, int iImageSize);  
                HRESULT GetViewModeAndIconSize(ref FOLDERVIEWMODE puViewMode, ref int piImageSize);  
                HRESULT SetGroupSubsetCount(uint cVisibleRows);  
                HRESULT GetGroupSubsetCount(ref uint pcVisibleRows);  
                HRESULT SetRedraw(bool fRedrawOn);  
                HRESULT IsMoveInSameFolder();  
                HRESULT DoRename();  
            }  
      
            [StructLayout(LayoutKind.Sequential, Pack = 4)]  
            public struct PROPERTYKEY  
            {  
                private Guid fmtid;  
                private int pid;  
                public Guid FormatId  
                {  
                    get  
                    {  
                        return this.fmtid;  
                    }  
                }  
                public int PropertyId  
                {  
                    get  
                    {  
                        return this.pid;  
                    }  
                }  
                public PROPERTYKEY(Guid formatId, int propertyId)  
                {  
                    this.fmtid = formatId;  
                    this.pid = propertyId;  
                }  
                public static readonly PROPERTYKEY PKEY_DateModified = new PROPERTYKEY(new Guid("B725F130-47EF-101A-A5F1-02608C9EEBAC"), 14);  
                public static readonly PROPERTYKEY PKEY_DateCreated = new PROPERTYKEY(new Guid("B725F130-47EF-101A-A5F1-02608C9EEBAC"), 15);              
            }  
      
            public enum FVTEXTTYPE  
            {  
                FVST_EMPTYTEXT = 0  
            }  
      
            [StructLayout(LayoutKind.Sequential)]  
            public struct PROPARRAY  
            {  
                public uint cElems;  
                public IntPtr pElems;  
            }  
      
            [StructLayout(LayoutKind.Explicit, Pack = 1)]  
            public struct PROPVARIANT  
            {  
                [FieldOffset(0)]  
                public ushort varType;  
                [FieldOffset(2)]  
                public ushort wReserved1;  
                [FieldOffset(4)]  
                public ushort wReserved2;  
                [FieldOffset(6)]  
                public ushort wReserved3;  
                [FieldOffset(8)]  
                public byte bVal;  
                [FieldOffset(8)]  
                public sbyte cVal;  
                [FieldOffset(8)]  
                public ushort uiVal;  
                [FieldOffset(8)]  
                public short iVal;  
                [FieldOffset(8)]  
                public UInt32 uintVal;  
                [FieldOffset(8)]  
                public Int32 intVal;  
                [FieldOffset(8)]  
                public UInt64 ulVal;  
                [FieldOffset(8)]  
                public Int64 lVal;  
                [FieldOffset(8)]  
                public float fltVal;  
                [FieldOffset(8)]  
                public double dblVal;  
                [FieldOffset(8)]  
                public short boolVal;  
                [FieldOffset(8)]  
                public IntPtr pclsidVal;  
                [FieldOffset(8)]  
                public IntPtr pszVal;  
                [FieldOffset(8)]  
                public IntPtr pwszVal;  
                [FieldOffset(8)]  
                public IntPtr punkVal;  
                [FieldOffset(8)]  
                public PROPARRAY ca;  
                [FieldOffset(8)]  
                public System.Runtime.InteropServices.ComTypes.FILETIME filetime;  
            }  
      
            [StructLayout(LayoutKind.Sequential)]  
            public struct SORTCOLUMN  
            {  
                public PROPERTYKEY propkey;  
                public SORTDIRECTION direction;  
                public SORTCOLUMN(PROPERTYKEY propkey, SORTDIRECTION direction)  
                {  
                   this.propkey = propkey;  
                   this.direction = direction;  
                }  
            }  
      
            public enum SORTDIRECTION  
            {  
                SORT_DESCENDING = -1,  
                SORT_ASCENDING = 1  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("b63ea76d-1f85-456f-a19c-48159efa858b")]  
            public interface IShellItemArray  
            {  
                HRESULT BindToHandler(IntPtr pbc, ref Guid bhid, ref Guid riid, ref IntPtr ppvOut);  
                HRESULT GetPropertyStore(GETPROPERTYSTOREFLAGS flags, ref Guid riid, ref IntPtr ppv);  
                HRESULT GetPropertyDescriptionList(PROPERTYKEY keyType, ref Guid riid, ref IntPtr ppv);  
                // Function GetAttributes(AttribFlags As SIATTRIBFLAGS, sfgaoMask As SFGAOF, ByRef psfgaoAttribs As SFGAOF) As HRESULT  
                HRESULT GetAttributes(SIATTRIBFLAGS AttribFlags, int sfgaoMask, ref int psfgaoAttribs);  
                HRESULT GetCount(ref int pdwNumItems);  
                HRESULT GetItemAt(int dwIndex, ref IShellItem ppsi);  
      
                // Function EnumItems(ByRef ppenumShellItems As IEnumShellItems) As HRESULT  
                HRESULT EnumItems(ref IntPtr ppenumShellItems);  
            }  
      
            public enum GETPROPERTYSTOREFLAGS  
            {  
                GPS_DEFAULT = 0,  
                GPS_HANDLERPROPERTIESONLY = 0x1,  
                GPS_READWRITE = 0x2,  
                GPS_TEMPORARY = 0x4,  
                GPS_FASTPROPERTIESONLY = 0x8,  
                GPS_OPENSLOWITEM = 0x10,  
                GPS_DELAYCREATION = 0x20,  
                GPS_BESTEFFORT = 0x40,  
                GPS_NO_OPLOCK = 0x80,  
                GPS_PREFERQUERYPROPERTIES = 0x100,  
                GPS_EXTRINSICPROPERTIES = 0x200,  
                GPS_EXTRINSICPROPERTIESONLY = 0x400,  
                GPS_MASK_VALID = 0x7FF  
            }  
      
            public enum SIATTRIBFLAGS  
            {  
                SIATTRIBFLAGS_AND = 0x1,  
                SIATTRIBFLAGS_OR = 0x2,  
                SIATTRIBFLAGS_APPCOMPAT = 0x3,  
                SIATTRIBFLAGS_MASK = 0x3,  
                SIATTRIBFLAGS_ALLITEMS = 0x4000  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE")]  
            public interface IShellItem  
            {  
                [PreserveSig()]  
                HRESULT BindToHandler(IntPtr pbc, ref Guid bhid, ref Guid riid, ref IntPtr ppv);  
                HRESULT GetParent(ref IShellItem ppsi);  
                HRESULT GetDisplayName(SIGDN sigdnName, ref StringBuilder ppszName);  
                HRESULT GetAttributes(uint sfgaoMask, ref uint psfgaoAttribs);  
                HRESULT Compare(IShellItem psi, uint hint, ref int piOrder);  
            }  
      
            public enum SIGDN : uint  
            {  
                SIGDN_NORMALDISPLAY = 0x0,  
                SIGDN_PARENTRELATIVEPARSING = 0x80018001,  
                SIGDN_DESKTOPABSOLUTEPARSING = 0x80028000,  
                SIGDN_PARENTRELATIVEEDITING = 0x80031001,  
                SIGDN_DESKTOPABSOLUTEEDITING = 0x8004C000,  
                SIGDN_FILESYSPATH = 0x80058000,  
                SIGDN_URL = 0x80068000,  
                SIGDN_PARENTRELATIVEFORADDRESSBAR = 0x8007C001,  
                SIGDN_PARENTRELATIVE = 0x80080001  
            }  
      
            [ComImport]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("000214E6-0000-0000-C000-000000000046")]  
            public interface IShellFolder  
            {  
                HRESULT ParseDisplayName(IntPtr hwnd, IntPtr pbc, [MarshalAs(UnmanagedType.LPWStr)] string pszDisplayName, ref uint pchEaten, out IntPtr ppidl, ref SFGAO pdwAttributes);  
                HRESULT EnumObjects(IntPtr hwnd, SHCONTF grfFlags, out IEnumIDList ppenumIDList);  
                HRESULT BindToObject(IntPtr pidl, IntPtr pbc, [In] ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppv);  
                HRESULT BindToStorage(IntPtr pidl, IntPtr pbc, [In] ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppv);  
                HRESULT CompareIDs(IntPtr lParam, IntPtr pidl1, IntPtr pidl2);  
                HRESULT CreateViewObject(IntPtr hwndOwner, [In] ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppv);  
                HRESULT GetAttributesOf(uint cidl, IntPtr apidl, ref SFGAO rgfInOut);  
                HRESULT GetUIObjectOf(IntPtr hwndOwner, uint cidl, ref IntPtr apidl, [In] ref Guid riid, ref uint rgfReserved, out IntPtr ppv);  
                HRESULT GetDisplayNameOf(IntPtr pidl, SHGDNF uFlags, out STRRET pName);  
                HRESULT SetNameOf(IntPtr hwnd, IntPtr pidl, [MarshalAs(UnmanagedType.LPWStr)] string pszName, SHGDNF uFlags, out IntPtr ppidlOut);  
            }  
      
            public enum SHCONTF  
            {  
                SHCONTF_CHECKING_FOR_CHILDREN = 0x10,  
                SHCONTF_FOLDERS = 0x20,  
                SHCONTF_NONFOLDERS = 0x40,  
                SHCONTF_INCLUDEHIDDEN = 0x80,  
                SHCONTF_INIT_ON_FIRST_NEXT = 0x100,  
                SHCONTF_NETPRINTERSRCH = 0x200,  
                SHCONTF_SHAREABLE = 0x400,  
                SHCONTF_STORAGE = 0x800,  
                SHCONTF_NAVIGATION_ENUM = 0x1000,  
                SHCONTF_FASTITEMS = 0x2000,  
                SHCONTF_FLATLIST = 0x4000,  
                SHCONTF_ENABLE_ASYNC = 0x8000  
            }  
      
            public enum SFGAO : uint  
            {  
                CANCOPY = 0x1,  
                CANMOVE = 0x2,  
                CANLINK = 0x4,  
                STORAGE = 0x8,  
                CANRENAME = 0x10,  
                CANDELETE = 0x20,  
                HASPROPSHEET = 0x40,  
                DROPTARGET = 0x100,  
                CAPABILITYMASK = 0x177,  
                ENCRYPTED = 0x2000,  
                ISSLOW = 0x4000,  
                GHOSTED = 0x8000,  
                LINK = 0x10000,  
                SHARE = 0x20000,  
                READONLY = 0x40000,  
                HIDDEN = 0x80000,  
                DISPLAYATTRMASK = 0xFC000,  
                STREAM = 0x400000,  
                STORAGEANCESTOR = 0x800000,  
                VALIDATE = 0x1000000,  
                REMOVABLE = 0x2000000,  
                COMPRESSED = 0x4000000,  
                BROWSABLE = 0x8000000,  
                FILESYSANCESTOR = 0x10000000,  
                FOLDER = 0x20000000,  
                FILESYSTEM = 0x40000000,  
                HASSUBFOLDER = 0x80000000,  
                CONTENTSMASK = 0x80000000,  
                STORAGECAPMASK = 0x70C50008,  
                PKEYSFGAOMASK = 0x81044000  
            }  
      
            public enum SHGDNF  
            {  
                SHGDN_NORMAL = 0,  
                SHGDN_INFOLDER = 0x1,  
                SHGDN_FOREDITING = 0x1000,  
                SHGDN_FORADDRESSBAR = 0x4000,  
                SHGDN_FORPARSING = 0x8000  
            }  
      
            [StructLayout(LayoutKind.Explicit, Size = 264)]  
            public struct STRRET  
            {  
                [FieldOffset(0)]  
                public uint uType;  
                [FieldOffset(4)]  
                public IntPtr pOleStr;  
                [FieldOffset(4)]  
                public uint uOffset;  
                [FieldOffset(4)]  
                public IntPtr cString;  
            }  
      
            [ComImport]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("000214F2-0000-0000-C000-000000000046")]  
            public interface IEnumIDList  
            {  
                [PreserveSig()]  
                HRESULT Next(uint celt, out IntPtr rgelt, out int pceltFetched);  
                [PreserveSig()]  
                HRESULT Skip(uint celt);  
                void Reset();  
                IEnumIDList Clone();  
            }  
      
            [ComImport]  
            [Guid("e07010ec-bc17-44c0-97b0-46c7c95b9edc")]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            internal interface IExplorerPaneVisibility  
            {  
                [PreserveSig]  
                HRESULT GetPaneState(ref Guid ep, out EXPLORERPANESTATE peps);  
            }  
      
            public enum EXPLORERPANESTATE  
            {  
                EPS_DONTCARE = 0,  
                EPS_DEFAULT_ON = 0x1,  
                EPS_DEFAULT_OFF = 0x2,  
                EPS_STATEMASK = 0xFFFF,  
                EPS_INITIALSTATE = 0x10000,  
                EPS_FORCE = 0x20000  
            }  
      
            private Guid EP_NavPane = new Guid("{cb316b22-25f7-42b8-8a09-540d23a43c2f}");  
            private Guid EP_Commands = new Guid("{d9745868-ca5f-4a76-91cd-f5a129fbb076}");  
            private Guid EP_Commands_Organize = new Guid("{72e81700-e3ec-4660-bf24-3c3b7b648806}");  
            private Guid EP_Commands_View = new Guid("{21f7c32d-eeaa-439b-bb51-37b96fd6a943}");  
            private Guid EP_DetailsPane = new Guid("{43abf98b-89b8-472d-b9ce-e69b8229f019}");  
            private Guid EP_PreviewPane = new Guid("{893c63d1-45c8-4d17-be19-223be71be365}");  
            private Guid EP_QueryPane = new Guid("{65bcde4f-4f07-4f27-83a7-1afca4df7ddd}");  
            private Guid EP_AdvQueryPane = new Guid("{b4e9db8b-34ba-4c39-b5cc-16a1bd2c411c}");  
            private Guid EP_StatusBar = new Guid("{65fe56ce-5cfe-4bc4-ad8a-7ae3fe7e8f7c}");  
            private Guid EP_Ribbon = new Guid("{D27524A8-C9F2-4834-A106-DF8889FD4F37}");  
      
            [DllImport("User32.dll", SetLastError = true)]  
            public static extern bool GetClientRect(IntPtr hWnd, ref RECT lpRect);  
      
            [DllImport("User32.dll", SetLastError = true)]  
            private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);  
      
            [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]  
            public static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string lpszWindow);  
      
            [DllImport("Shell32.dll", SetLastError = true)]  
            public static extern HRESULT SHGetKnownFolderIDList(ref Guid rfid, int dwFlags, IntPtr hToken, ref IntPtr ppidl);  
      
            [DllImport("Shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]  
            public static extern HRESULT SHILCreateFromPath([MarshalAs(UnmanagedType.LPWStr)] string pszPath, ref IntPtr ppIdl, ref uint rgflnOut);  
      
            public partial class CExplorerBrowserHost : CExplorerBrowserHost.IServiceProvider, IExplorerPaneVisibility  
            {  
                [ComImport()]  
                [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
                [Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]  
                public interface IServiceProvider  
                {  
                    [PreserveSig()]  
                    HRESULT QueryService(ref Guid guidService, ref Guid riid, ref IntPtr ppvObject);  
                }  
      
                [DllImport("Shlwapi.DLL", CharSet = CharSet.Unicode, SetLastError = true)]  
                internal static extern HRESULT IUnknown_SetSite([In()][MarshalAs(UnmanagedType.IUnknown)] object punk, [In()][MarshalAs(UnmanagedType.IUnknown)] object punkSite);  
      
                public HRESULT QueryService(ref Guid guidService, ref Guid riid, ref IntPtr ppvObject)  
                {  
                    HRESULT hr = HRESULT.S_OK;  
                    if (guidService.CompareTo(new Guid("e07010ec-bc17-44c0-97b0-46c7c95b9edc")) == 0)  
                    {  
                        ppvObject = Marshal.GetComInterfaceForObject(this, typeof(IExplorerPaneVisibility));  
                        hr = HRESULT.S_OK;  
                    }  
                    else  
                    {  
                        var nullObj = IntPtr.Zero;  
                        ppvObject = nullObj;  
                        hr = HRESULT.E_NOINTERFACE;  
                    }  
                    return hr;  
                }  
                  
                public HRESULT GetPaneState(ref Guid explorerPane, out EXPLORERPANESTATE peps)  
                {  
                    if (explorerPane == EP_DetailsPane)  
                    {  
                        peps = EXPLORERPANESTATE.EPS_DEFAULT_OFF | EXPLORERPANESTATE.EPS_INITIALSTATE;  
                        return HRESULT.S_OK;  
                    }  
                    if (explorerPane == EP_PreviewPane)  
                    {  
                        peps = EXPLORERPANESTATE.EPS_DEFAULT_OFF | EXPLORERPANESTATE.EPS_INITIALSTATE;  
                        return HRESULT.S_OK;  
                    }  
                    // peps = EXPLORERPANESTATE.EPS_DEFAULT_ON | EXPLORERPANESTATE.EPS_INITIALSTATE | EXPLORERPANESTATE.EPS_FORCE;  
                    peps = EXPLORERPANESTATE.EPS_DEFAULT_ON | EXPLORERPANESTATE.EPS_INITIALSTATE;  
                  //  peps = EXPLORERPANESTATE.EPS_DEFAULT_OFF | EXPLORERPANESTATE.EPS_INITIALSTATE;  
                    return HRESULT.S_OK;  
                }  
      
                public IExplorerBrowser pExplorerBrowser = default;  
      
                private Guid EP_NavPane = new Guid("{cb316b22-25f7-42b8-8a09-540d23a43c2f}");  
                private Guid EP_Commands = new Guid("{d9745868-ca5f-4a76-91cd-f5a129fbb076}");  
                private Guid EP_Commands_Organize = new Guid("{72e81700-e3ec-4660-bf24-3c3b7b648806}");  
                private Guid EP_Commands_View = new Guid("{21f7c32d-eeaa-439b-bb51-37b96fd6a943}");  
                private Guid EP_DetailsPane = new Guid("{43abf98b-89b8-472d-b9ce-e69b8229f019}");  
                private Guid EP_PreviewPane = new Guid("{893c63d1-45c8-4d17-be19-223be71be365}");  
                private Guid EP_QueryPane = new Guid("{65bcde4f-4f07-4f27-83a7-1afca4df7ddd}");  
                private Guid EP_AdvQueryPane = new Guid("{b4e9db8b-34ba-4c39-b5cc-16a1bd2c411c}");  
                private Guid EP_StatusBar = new Guid("{65fe56ce-5cfe-4bc4-ad8a-7ae3fe7e8f7c}");  
                private Guid EP_Ribbon = new Guid("{D27524A8-C9F2-4834-A106-DF8889FD4F37}");  
      
                public CExplorerBrowserHost(IntPtr hWnd)  
                {  
                    var CLSID_ExplorerBrowser = new Guid("71f96385-ddd6-48d3-a0c1-ae06e8b055fb");  
                    var ExplorerBrowserType = Type.GetTypeFromCLSID(CLSID_ExplorerBrowser, true);  
                    var ExplorerBrowser = Activator.CreateInstance(ExplorerBrowserType);  
                    pExplorerBrowser = (IExplorerBrowser)ExplorerBrowser;  
                    var fs = default(FOLDERSETTINGS);  
                    // fs.ViewMode = (int)FOLDERVIEWMODE.FVM_THUMBNAIL;  
                    fs.ViewMode = (int)FOLDERVIEWMODE.FVM_DETAILS;  
                    fs.fFlags = (int)FOLDERFLAGS.FWF_HIDEFILENAMES;  
                    var rc = default(RECT);  
                    GetClientRect(hWnd, ref rc);  
                    if (pExplorerBrowser != null)  
                    {  
                        HRESULT hr = pExplorerBrowser.Initialize(hWnd, rc, fs);  
                        if (hr == HRESULT.S_OK)  
                        {  
                            pExplorerBrowser.SetOptions(EXPLORER_BROWSER_OPTIONS.EBO_SHOWFRAMES | EXPLORER_BROWSER_OPTIONS.EBO_ALWAYSNAVIGATE | EXPLORER_BROWSER_OPTIONS.EBO_NOTRAVELLOG | EXPLORER_BROWSER_OPTIONS.EBO_NOWRAPPERWINDOW | EXPLORER_BROWSER_OPTIONS.EBO_HTMLSHAREPOINTVIEW | EXPLORER_BROWSER_OPTIONS.EBO_NOBORDER | EXPLORER_BROWSER_OPTIONS.EBO_NOPERSISTVIEWSTATE);  
      
                            var pUnknown = Marshal.GetIUnknownForObject(pExplorerBrowser);  
                            var pUnknownSite = Marshal.GetIUnknownForObject(this);  
                            // hr = IUnknown_SetSite(pUnknown, pUnknownSite)  
                            // hr = IUnknown_SetSite(pUnknown, Me)  
                            hr = IUnknown_SetSite(pExplorerBrowser, this);  
                            var pidlInit = IntPtr.Zero;  
                            var FOLDERID_ComputerFolder = new Guid("0AC0837C-BBF8-452A-850D-79D08E667CA7");  
                            hr = SHGetKnownFolderIDList(ref FOLDERID_ComputerFolder, 0, IntPtr.Zero, ref pidlInit);  
                            //uint rgflnOut = 0;  
                            //hr = SHILCreateFromPath("C:\\", ref pidlInit, ref rgflnOut);  
                            if (hr == HRESULT.S_OK)  
                            {  
                                pExplorerBrowser.BrowseToIDList(pidlInit, SBSP_ABSOLUTE);  
                            }  
                        }  
                    }  
                }  
            }  
      
            public System.Windows.Forms.TextBox TextBox1;  
            public System.Windows.Forms.Button Button1;  
            public System.Windows.Forms.Button Button2;  
      
            public CExplorerBrowserHost ebh;  
      
            public Form1()  
            {  
                //InitializeComponent();              
                this.Name = "Form1";  
                this.Text = "ExplorerBrowser";  
                this.Load += Form1_Load;  
                this.Resize += Form1_Resize;  
            }  
      
            private void Form1_Load(object sender, EventArgs e)  
            {  
                ebh = new CExplorerBrowserHost(this.Handle);  
      
                CMessageFilter mf = new CMessageFilter(ebh);  
                Application.AddMessageFilter(mf);  
      
                ClientSize = new System.Drawing.Size(1080, 800);  
      
                Button1 = new System.Windows.Forms.Button();  
                Button1.Anchor = (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);  
                Button1.Location = new System.Drawing.Point(10, ClientSize.Height - (32 - (32 - 20) / 2));  
                Button1.Name = "Button1";  
                Button1.Text = "Current Folder";  
                Button1.Size = new System.Drawing.Size(100, 20);  
                Button1.Click += Button1_Click;  
      
                TextBox1 = new System.Windows.Forms.TextBox();  
                TextBox1.Anchor = (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);  
                TextBox1.Location = new System.Drawing.Point(120, ClientSize.Height - (32 - (32 - 20) / 2));  
                TextBox1.Name = "TextBox1";  
                TextBox1.ReadOnly = true;  
                TextBox1.Size = new System.Drawing.Size(400, 20);  
      
              
    

  2. kwok kan ho 21 Reputation points
    2023-01-01T13:37:01.843+00:00

    Thank you for your patient. But I run into trouble. I don't really understand "cExplorerBrowserHost1 added in Designer". I copy part of your code "public partial class CExplorerBrowserHost : UserControl, CExplorerBrowserHost.IServiceProvider , IExplorerPaneVisibility" to create a userControl, but there is compilation error in "IExplorerPaneVisibility"
    If I copy the entire code into one project, the cExplorerBrowserHost1 is the problem. I don't know how to add this control to the designer. There are compilation error in wherever the "cExplorerBrowserHost1" appeared.

    Your advice please.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.