navigation - 操作 Windows 7 Explorer 导航 Pane

标签 navigation windows-explorer windows-shell

根据我在 superuser 收到的答案,很明显,我必须将以下内容添加到自定义资源管理器窗口启动器中。我想启动一个 Root过的资源管理器 View ,对于 就是那个窗口使导航 Pane 看起来像旧的 Windows XP 文件夹 Pane 。我已经wrote a program将这些文件夹 View 的快捷方式放置在“开始”菜单上,因此更改快捷方式以通过启动器运行是微不足道的。

这是 XP 文件夹 Pane :

Windows XP Explorer Folders Pane

这是 Windows 7 导航 Pane :

Windows 7 Explorer Navigation Pane
(来源:280z28.org)

最佳答案

好吧,我没有时间完全完成这段代码(它是在 C# 中,我不知道你想要什么,但你没有真正指定)。其基本前提是在 .NET 窗体中托管 ExplorerBrowser 控件(使用您需要获取并添加引用的 WindowsAPICodePack),等待直到 TreeView 创建并子类化窗口以允许我们拦截项目插入。

不幸的是,没有什么是简单的,文本并没有让您直接了解项目是什么(因为他们没有设置它),您需要做的是从 insertStruct.lParam 获取 PIDL。并将其解析为有意义的内容,可能使用 IShellFolder界面。然后,您可以有选择地删除项目(通过将 0 作为 m.Result 返回),并且您可以拦截任何您需要的内容。你会认为会有一个简单的解决方案,但我想你的运气并不好 ;) 希望它会有所帮助。

另一种可能是做类似的(直接使用主机资源管理器)但使用类似 detours 的东西 Hook 注册表功能并有选择地更改资源管理器控件获得的 View ,允许某些注册表调整工作。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.Shell;
using System.Runtime.InteropServices;

namespace MyExplorer
{
    public partial class Form1 : Form
    {
        const int WH_CALLWNDPROC = 4;        
        const int WM_CREATE = 1;

        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto,
         CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn,
        IntPtr hInstance, int threadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto,
         CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(IntPtr hHook);

        [DllImport("user32.dll", CharSet = CharSet.Auto,
         CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(IntPtr hHook, int nCode,
        IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        IntPtr m_hHook;
        HookProc HookDelegate;

        struct WindowHookStruct
        {            
            public IntPtr lParam;
            public IntPtr wParam;
            public uint   message;
            public IntPtr hwnd;
        }

        public class SubclassTreeView : NativeWindow
        {           
            const int TV_FIRST = 0x1100;
            //const int TVM_INSERTITEMA = (TV_FIRST + 0);
            const int TVM_INSERTITEMW = (TV_FIRST + 50);

            struct TVINSERTSTRUCTW 
            {
               public IntPtr hParent;
               public IntPtr hInsertAfter;    
               /* TVITEMW */
               public uint mask;
               public IntPtr hItem;
               public uint state;
               public uint stateMask;
               public IntPtr pszText;
               public int cchTextMax;
               public int iImage;
               public int iSelectedImage;
               public int cChildren;
               public IntPtr lParam;
            }

            int count = 0;

            protected override void WndProc(ref Message m)
            {                
                bool bHandled = false;                             

                switch (m.Msg)
                {
                    case TVM_INSERTITEMW:                        
                        TVINSERTSTRUCTW insertStruct = (TVINSERTSTRUCTW)Marshal.PtrToStructure(m.LParam, typeof(TVINSERTSTRUCTW));

                        /* Change text to prove a point */
                        string name = String.Format("{0:X} {1} Hello", insertStruct.hParent.ToInt64(), count++);
                        insertStruct.pszText = Marshal.StringToBSTR(name);
                        insertStruct.cchTextMax = name.Length+1;
                        Marshal.StructureToPtr(insertStruct, m.LParam, false);                        

                        /* insertStruct.lParam is a pointer to a IDL, 
                           use IShellFolder::GetDisplayNameOf to pull out a sensible 
                           name to work out what to hide */
                        /* Uncomment this code to delete the entry */
                        //bHandled = true;
                        //m.Result = IntPtr.Zero;                                                  
                        break;
                }

                if (!bHandled)
                {
                    base.WndProc(ref m);
                }
            }
        }

        /* Not complete structure, don't need it */
        struct MSG
        {
            public IntPtr hwnd;
            public uint   message;
            public IntPtr wParam;
            public IntPtr lParam;   
        }

        SubclassTreeView sc = null;

        public Form1()
        {
            InitializeComponent();
            HookDelegate = new HookProc(HookWindowProc);
            m_hHook = SetWindowsHookEx(WH_CALLWNDPROC, HookDelegate, (IntPtr)0, AppDomain.GetCurrentThreadId());
        }

        int HookWindowProc(int nCode, IntPtr wParam, IntPtr lParam)
        {           
            if (nCode < 0)
            {
                return CallNextHookEx(m_hHook, nCode, wParam, lParam);
            }
            else
            {

                WindowHookStruct hookInfo = (WindowHookStruct)Marshal.PtrToStructure(lParam, typeof(WindowHookStruct));
                StringBuilder sb = new StringBuilder(1024);

                if (hookInfo.message == WM_CREATE)
                {
                    if (GetClassName(hookInfo.hwnd, sb, 1024) > 0)
                    {
                        System.Diagnostics.Debug.WriteLine(sb.ToString());
                        if (sb.ToString() == "SysTreeView32")
                        {
                            sc = new SubclassTreeView();
                            sc.AssignHandle(hookInfo.hwnd);
                            UnhookWindowsHookEx(m_hHook);
                        }
                    }
                }

                return CallNextHookEx(m_hHook, nCode, wParam, lParam);                
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {                        
            explorerBrowser1.Navigate(ShellLink.FromParsingName("C:\\"));
        }
    }
}

关于navigation - 操作 Windows 7 Explorer 导航 Pane ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2128040/

相关文章:

html - 什么时候应该在新标签/窗口中打开链接?

ios - 在 iOS 中调用 RemovePage 后未清除导航堆栈 - Xamarin.Forms

namespaces - 如何在 Windows 资源管理器中托管根命名空间扩展

c# - 将文件夹重命名命令发送到 Windows 资源管理器

c++ - 子类化 SHBrowseForFolder 并处理 WM_NOTIFY

objective-c - 插入 UIImageView 作为导航栏按钮

HTML导航定位

c++ - ShellExecute for mailto : doesn't work with Google Chrome

excel - 创建zip错误: Namespace method fails on IShellDispatch

c++ - Windows 7 64 位 shell 无法识别 Shell 扩展安装