Okay, non ho il tempo di terminare completamente questo codice (ed è in C# che non ho idea di ciò che si desidera, ma in realtà non si specificava). La premessa di base di questo è l'hosting del controllo ExplorerBrowser all'interno di un modulo .NET (utilizzando lo WindowsAPICodePack per il quale è necessario ottenere e aggiungere un riferimento), attendere fino a quando TreeView non è stato creato e sottoclasse la finestra per consentirci di intercettare il inserzioni di oggetti.
Sfortunatamente niente è mai semplice, il testo non ti dà un'idea diretta di cosa sia l'oggetto (perché non lo impostano), quello che dovresti fare è ottenere il PIDL dal insertStruct.lParam
e analizzarlo in qualcosa di significativo, probabilmente usando l'interfaccia IShellFolder
. È quindi possibile rimuovere selettivamente gli elementi (restituendo 0 come m.Result
) e si può intercettare qualsiasi altra cosa è necessario. Penseresti che ci sarebbe una soluzione semplice ma immagino che la tua fortuna non sia in;) Spero che aiuti un po '.
Un'alternativa potrebbe essere simile (host explorer direttamente) ma utilizzare qualcosa come detours per agganciare le funzioni del Registro di sistema e modificare selettivamente la visualizzazione del controllo di Esplora risorse che consente il corretto funzionamento di alcuni dei tweaking del Registro di sistema.
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:\\"));
}
}
}
Forse potresti spiegare perché vorresti farlo. Penserei che un utente lo odiasse. – PeteT
@ petebob796: Io lo uso così posso avere una finestra speciale aperta che mostra qualunque progetto stia lavorando. Il programma è un'utilità che metto nel menu di avvio per "lanciare la cartella _____ con una vista radicata", quindi non è come se stessi inserendo l'impostazione sulle persone. –