当前位置: 首页 > 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.zskr.cn/news/165110.html

相关文章:

  • 使用TensorRT优化微软Phi-2模型推理表现
  • 2026年GEO优化源码搭建推荐排行榜哪家好 - 源码云科技
  • Linux定时任务cron完全指南:从写法到排错
  • 2025年净化门厂家推荐:江苏言信环境科技领衔,手术室/实验室/无尘室等十大高等级净化门品牌实力深度解析与选购指南 - 品牌企业推荐师(官方)
  • 2026年GEO优化源码搭建口碑推荐哪家好 - 源码云科技
  • 2025年洁净窗行业深度解析:江苏言信环境科技领衔,揭秘高等级气密洁净窗与模块化洁净窗的十大技术标杆与选购权威指南 - 品牌企业推荐师(官方)
  • S盒的代数免疫度
  • 2025年商业美陈设计公司推荐:东莞市共创广告有限公司,创意美陈与IP场景定制专家,商场节日美陈实力品牌深度解析 - 品牌企业推荐师(官方)
  • 2025年数码打印机厂家推荐:深圳易龙三维科技引领柔性印刷新浪潮,九大细分领域定制化解决方案权威解析 - 品牌企业推荐师(官方)
  • 2025年高温热油泵厂家权威推荐:河北兆宏机械泵业TAP/RYT/SRY系列节能型离心热油泵核心技术深度解析 - 品牌企业推荐师(官方)
  • openwrt路由器iptv设置
  • 2026年GEO优化源码搭建推荐排行哪家好 - 源码云科技
  • 【Week1_Day2】【软件测试学习记录与反思】【拆分知识点,形成思维导图,划分重点,优先级排序,集中80%精力攻克重点】
  • 为什么Transformer类模型特别适合TensorRT优化?
  • 2026年GEO优化源码搭建推荐榜单哪家好 - 源码云科技
  • TensorRT Builder优化策略选择指南
  • Excel如何在全校成绩册中,根据班级和总分求最高分、最低分呢?
  • 专业的企业信用服务排名
  • 【开题答辩全过程】以 基于大数据的健康评估管理系统的设计与实现为例,包含答辩的问题和答案
  • 【接口测试】3_PyMySQL模块 _连接数据库
  • 基于TensorRT镜像的大模型部署全流程指南
  • 深度探索.NET 中 IAsyncEnumerable:异步迭代的底层奥秘与高效实践
  • 2025年上海智慧招劳务派遣公司深度解析:劳务中介服务十大实力品牌排行,企业用工外包与灵活派遣权威指南 - 品牌企业推荐师(官方)
  • 2025最新!专科生必看8个AI论文工具测评,开题报告轻松搞定
  • 基于大数据的图书管理分析及可视化系统(毕设源码+文档)
  • 大模型Token成本太高?用TensorRT降低推理资源消耗
  • 云服务商为何偏爱TensorRT?背后的技术逻辑揭秘
  • Transformer 中为什么用LayerNorm而不用BatchNorm?
  • 告别高延迟:使用TensorRT优化大模型生成速度实战
  • Myvatis 动态查询及关联查询