b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

我使用C#和WindowsAPI中的IContextMenu接口和IShellExtInit接口,为所有类型的文件和目录

电脑杂谈  发布时间:2021-02-10 09:01:41  来源:网络整理

我在C#和WindowsAPI中使用IContextMenu接口和IShellExtInit接口向WindowsXP资源管理器中的所有类型的文件和目录添加简单的菜单项“ HooCommandShell”。此菜单项通常显示在大多数类型的文件和目录上。压缩文件唯一的问题:

原来的“ OpenwithWinzip”,“资源管理器”和“打印”三个项目都不见了;

资源管理器右键菜单_右键新建菜单管理_右键菜单管理 360

TortoiseSVN及其子菜单项的文本标题已变为空白;

右键新建菜单管理_右键菜单管理 360_资源管理器右键菜单

“ HooCommandShell”可以正常显示。

右键菜单管理 360_资源管理器右键菜单_右键新建菜单管理

我恳请大师们指教!

主要类代码如下:


[Guid("24E4E314-AEE1-4906-8CE6-DA49D60E4D29")]
public class CommandContextMenu : IShellExtInit, IContextMenu {
protected static readonly string guid = "{" + typeof(CommandContextMenu).GUID.ToString() + "}";
protected uint hDrop = 0;

int IContextMenu.QueryContextMenu(uint hMenu, uint iMenu, int idCmdFirst, int idCmdLast, uint uFlags) {
// Create the popup to insert
uint handleMenuPopup = Win32Helpers.CreatePopupMenu(); 

int id = 1;
if ((uFlags & 0xf) == 0 || (uFlags & (uint)CMF.CMF_EXPLORE) != 0) {
uint count = Win32Helpers.DragQueryFile(hDrop, 0xffffffff, null, 0);
if (count == 0xffffffff) {
//No file selected.

} else if (count >= 1) {
StringBuilder sb = new StringBuilder(1024);
Win32Helpers.DragQueryFile(hDrop, 0, sb, sb.Capacity + 1);
string path = sb.ToString();

//Set current directory base on first selection
//If the first selection is a file, set its parent directory as current directory.
//If the first selection is a folder, set it as current directory.
if (Directory.Exists(path)) {
Directory.SetCurrentDirectory(path);
} else {
Directory.SetCurrentDirectory(Path.GetDirectoryName(path));
}
}

id = idCmdFirst + id;
MENUITEMINFO menuItemInfo = new MENUITEMINFO();
menuItemInfo.cbSize = 48;
menuItemInfo.fMask = (uint)MIIM.ID | (uint)MIIM.TYPE | (uint)MIIM.STATE;
menuItemInfo.wID = id;
menuItemInfo.fType = (uint)MF.STRING;
menuItemInfo.dwTypeData = "Hoo Command Shell";
menuItemInfo.fState = (uint)MF.ENABLED;
Win32Helpers.InsertMenuItem(hMenu, (uint)iMenu, 1, ref menuItemInfo);
}
Logger.Info(string.Format("Menu ID: {0}", id));
return id;
}
 
void IContextMenu.GetCommandString(int idCmd, uint uFlags, int pwReserved, StringBuilder commandString, int cchMax) {
switch (uFlags) {
case (uint)GCS.VERB:
commandString = new StringBuilder("...");
break;
case (uint)GCS.HELPTEXT:
commandString = new StringBuilder("...");
break;
}
}

void IContextMenu.InvokeCommand(IntPtr pici) {
Logger.Info("Inside InvokeCommand()");
try {
Type commandType = typeof(INVOKECOMMANDINFO);
INVOKECOMMANDINFO ici = (INVOKECOMMANDINFO)Marshal.PtrToStructure(pici, commandType);
switch (ici.verb - 1) {
case 0:
OpenCommandShell();
break;
default:
break;
}
} catch (Exception ex) {
}
}

private void OpenCommandShell() {            
Process p = new Process();    
p.StartInfo.FileName = "cmd.exe";           
p.StartInfo.Arguments = "/k pushd " + Directory.GetCurrentDirectory();
p.Start();       
}

int IShellExtInit.Initialize(IntPtr pidlFolder, IntPtr lpdobj, uint hKeyProgID) {
try {
StringBuilder folderPath = new StringBuilder(1024);
if (pidlFolder != IntPtr.Zero) {
if (Win32Helpers.SHGetPathFromIDListW(pidlFolder, folderPath)) {
string currentDirectory = folderPath.ToString();
Directory.SetCurrentDirectory(currentDirectory);
Logger.Info("Current folder: " + currentDirectory);
} else {
Logger.Info("Could not convert PIDL " + pidlFolder);
}
}

if (lpdobj != (IntPtr)0) {
// Get info about the directory
IDataObject dataObject = (IDataObject)Marshal.GetObjectForIUnknown(lpdobj);
FORMATETC fmt = new FORMATETC();
fmt.cfFormat = CLIPFORMAT.CF_HDROP;
fmt.ptd = 0;
fmt.dwAspect = DVASPECT.DVASPECT_CONTENT;
fmt.lindex = -1;
fmt.tymed = TYMED.TYMED_HGLOBAL;
STGMEDIUM medium = new STGMEDIUM();
dataObject.GetData(ref fmt, ref medium);
hDrop = medium.hGlobal;
}
} catch (Exception) {
}
return 0;
}

[System.Runtime.InteropServices.ComRegisterFunctionAttribute()]
static void RegisterServer(String str1) {
try {
Logger.Info("Register assembly: " + guid);
// For Winnt set me as an approved shellex
RegistryKey root;
RegistryKey rk;
root = Registry.LocalMachine;
rk = root.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved", true);
rk.SetValue(guid, "Hoo Command Shell");
rk.Close();

//Register for all files
root = Registry.ClassesRoot;
rk = root.OpenSubKey(@"*\shellex\ContextMenuHandlers\Hoo Windows Shell", true);
if (rk == null)
rk = root.CreateSubKey(@"*\shellex\ContextMenuHandlers\Hoo Windows Shell");
rk.SetValue("", guid);
rk.Close();

//Register for directory
rk = root.OpenSubKey(@"Directory\shellex\ContextMenuHandlers\Hoo Windows Shell", true);
if (rk == null)
rk = root.CreateSubKey(@"Directory\shellex\ContextMenuHandlers\Hoo Windows Shell");
rk.SetValue("", guid);
rk.Close();

//Register for directory background
rk = root.OpenSubKey(@"Directory\Background\shellex\ContextMenuHandlers\Hoo Windows Shell", true);
if (rk == null)
rk = root.CreateSubKey(@"Directory\Background\shellex\ContextMenuHandlers\Hoo Windows Shell");
rk.SetValue("", guid);
rk.Close();

} catch (Exception e) {
System.Console.WriteLine(e.ToString());
}
}

[System.Runtime.InteropServices.ComUnregisterFunctionAttribute()]
static void UnregisterServer(String str1) {
try {
RegistryKey root;
RegistryKey rk;

// Remove ShellExtenstions registration
root = Registry.LocalMachine;
rk = root.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved", true);
rk.DeleteValue(guid);
rk.Close();

// Delete  regkey
root = Registry.ClassesRoot;
root.DeleteSubKey(@"*\shellex\ContextMenuHandlers\Hoo Windows Shell", false);
root.DeleteSubKey(@"Directory\shellex\ContextMenuHandlers\Hoo Windows Shell", false);
root.DeleteSubKey(@"Directory\Background\shellex\ContextMenuHandlers\Hoo Windows Shell", false);
} catch (Exception e) {
System.Console.WriteLine(e.ToString());
}
}
}


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-355586-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...