当前位置: 首页 > news >正文

[CodeSnippet] 预览的代码.cs

文件:预览的代码.cs

归档时间:2025-12-27 21:20
原始文件:预览的代码.cs

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;
using Microsoft.Win32;
using Quicker.Public;public class UniversalPreviewer
{public static void Exec(IStepContext context){try{// ================== 修复点 1 ==================// 原错误代码: Quicker.Public.Common.GetSelectedFiles();// 原因: Quicker.Public 命名空间下没有 Common 类。// 修正: 从动作上下文(context)中读取上一步“获取选中的文件”得到的变量。// 请确保你在 C# 脚本前添加了“获取选中的文件”步骤,并将变量名设为 "files"// =============================================var files = context.GetVarValue("files") as List<string>;// ================== 修复点 2 ==================// 原错误: operator '==' cannot be applied to 'method group'// 原因: 之前的错误导致 files 类型不明,或者被误推断为 IEnumerable (需用 .Count())// 修正: 明确转换为 List<string> 后,.Count 属性即可正常使用。// =============================================if (files == null || files.Count == 0) {MessageBox.Show("请先在资源管理器中选中一个文件!");return;}string filePath = files[0];// 2. 创建宿主窗口PreviewForm form = new PreviewForm(filePath);form.ShowDialog(); // 模态显示}catch (Exception ex){MessageBox.Show("预览启动失败: " + ex.Message);}}
}
// ---------------------------------------------------------
// ️ 宿主窗口:假装自己是资源管理器的预览窗格
// ---------------------------------------------------------
public class PreviewForm : Form
{private object _currentHandler; // 保存 COM 对象防止被回收public PreviewForm(string filePath){this.Text = "Quicker 悬浮预览 - " + Path.GetFileName(filePath);this.Size = new Size(800, 600);this.StartPosition = FormStartPosition.CenterScreen;// 核心加载逻辑LoadPreviewHandler(filePath);}private void LoadPreviewHandler(string filename){try{// 1. 根据扩展名找 CLSID (注册表)string ext = Path.GetExtension(filename);if (string.IsNullOrEmpty(ext)) return;// 查找注册表: HKEY_CLASSES_ROOT\.ext\ShellEx\{8895b1c6-b41f-4c1c-a562-0d564250836f}string classId = GetPreviewHandlerCLSID(ext);if (string.IsNullOrEmpty(classId)){Label lbl = new Label() { Text = "该文件类型未注册预览器\n(Preview Handler Not Found)", Dock = DockStyle.Fill, TextAlign = ContentAlignment.MiddleCenter };this.Controls.Add(lbl);return;}// 2. 实例化 COM 对象Type comType = Type.GetTypeFromCLSID(new Guid(classId));_currentHandler = Activator.CreateInstance(comType);// 3. 初始化 (IInitializeWithFile)if (_currentHandler is IInitializeWithFile fileInit){fileInit.Initialize(filename, 0);}// 4. 设置窗口并绘制 (IPreviewHandler)if (_currentHandler is IPreviewHandler previewHandler){// 绑定到当前 FormpreviewHandler.SetWindow(this.Handle, new RECT(this.ClientRectangle));previewHandler.DoPreview(); //  开始绘制!// 响应窗口大小变化this.Resize += (s, e) => {previewHandler.SetRect(new RECT(this.ClientRectangle));};}}catch (Exception ex){Label lbl = new Label() { Text = "预览加载出错:\n" + ex.Message, Dock = DockStyle.Fill, TextAlign = ContentAlignment.MiddleCenter };this.Controls.Add(lbl);}}// 查注册表找 GUIDprivate string GetPreviewHandlerCLSID(string ext){using (var key = Registry.ClassesRoot.OpenSubKey(ext + @"\ShellEx\{8895b1c6-b41f-4c1c-a562-0d564250836f}")){if (key != null) return key.GetValue(null) as string;}return null;}// 清理资源protected override void OnFormClosing(FormClosingEventArgs e){if (_currentHandler != null && _currentHandler is IPreviewHandler ph){ph.Unload();}if (_currentHandler != null){Marshal.FinalReleaseComObject(_currentHandler);_currentHandler = null;}base.OnFormClosing(e);}
}// ---------------------------------------------------------
//  COM 接口定义 (这是最硬核的部分,照抄即可)
// ---------------------------------------------------------[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("8895b1c6-b41f-4c1c-a562-0d564250836f")]
internal interface IPreviewHandler
{void SetWindow(IntPtr hwnd, ref RECT rect);void SetRect(ref RECT rect);void DoPreview();void Unload();void SetFocus();void QueryFocus(out IntPtr phwnd);[PreserveSig] uint TranslateAccelerator(ref Message pmsg);
}[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("b7d14566-0509-4cce-a71f-0a554233bd9b")]
internal interface IInitializeWithFile
{void Initialize([MarshalAs(UnmanagedType.LPWStr)] string pszFilePath, uint grfMode);
}[StructLayout(LayoutKind.Sequential)]
internal struct RECT
{public int left, top, right, bottom;public RECT(Rectangle r) { left = r.Left; top = r.Top; right = r.Right; bottom = r.Bottom; }
}// 简单的 Message 结构模拟
[StructLayout(LayoutKind.Sequential)]
public struct Message
{public IntPtr hWnd;public uint msg;public IntPtr wParam;public IntPtr lParam;public uint time;public Point p;
}
http://www.jsqmd.com/news/150119/

相关文章:

  • Java毕业设计:导师模棱两可修改建议「精准解读+落地方案」
  • S盒的扩散特性和雪崩准则
  • S盒的代数免疫度
  • 使用TensorRT优化微软Phi-2模型推理表现
  • 2026年GEO优化源码搭建推荐排行榜哪家好 - 源码云科技
  • 对称密码复习要点
  • 哪吒监控 V1的搭建与美化
  • LabVIEW与西门子PLC的S7通信源码揭秘:稳定通信的利器
  • 2025年商业美陈设计公司推荐:东莞市共创广告有限公司,创意美陈与IP场景定制专家,商场节日美陈实力品牌深度解析 - 品牌企业推荐师(官方)
  • 2025年净化门厂家推荐:江苏言信环境科技领衔,手术室/实验室/无尘室等十大高等级净化门品牌实力深度解析与选购指南 - 品牌企业推荐师(官方)
  • HarmonyOS 全局取色功能(Pen Image Feature Picker C)开发指南
  • 2026年GEO优化源码搭建口碑推荐哪家好 - 源码云科技
  • 【顶级EI复现】不完全信息下计及环境成本的多能源集线器博弈优化调度附Matlab代码
  • HarmonyOS 手写笔报点预测 C API 开发指南
  • 《程序员修炼之道》阅读笔记9
  • 2025年洁净窗行业深度解析:江苏言信环境科技领衔,揭秘高等级气密洁净窗与模块化洁净窗的十大技术标杆与选购权威指南 - 品牌企业推荐师(官方)
  • AI coding Agent日常记录
  • 2025年喷丸加工厂家推荐:南通汉科新能源等六家技术领航企业的核心工艺与竞争优势深度解析 - 品牌企业推荐师(官方)
  • 2025年东莞腊味品牌实力解析:肥仔秋食品领衔,六家本土实力厂家深度剖析与选购指南 - 品牌企业推荐师(官方)
  • 使用TensorRT优化通义千问推理性能实测报告
  • 2025套丝机厂家推荐榜/套丝机品牌前十 - 栗子测评
  • CodeCombat 容器部署笔记
  • 2025最新!专科生必看8个AI论文工具测评,开题报告轻松搞定
  • 使用 Ansible 自动化部署 OpenStack 私有云平台
  • 推理吞吐量提升4倍的秘密武器:TensorRT层融合技术
  • 2026年GEO优化源码搭建推荐榜单哪家好 - 源码云科技
  • TensorRT与ONNX协同工作流程最佳实践
  • TensorRT Builder优化策略选择指南
  • 2025年金属热处理厂家实力推荐:南通汉科新能源领衔,渗碳、真空等十大工艺顶尖企业深度解析与权威排名 - 品牌企业推荐师(官方)
  • Myvatis 动态查询及关联查询