将用户控件嵌入到 Excel 窗口中

 private MyUserControl myUserControl;
 private IntPtr excelWindowHandle;

 private void ThisAddIn_Startup(object sender, EventArgs e)
 {
     // 创建用户控件实例
     myUserControl = new MyUserControl();

     // 获取 Excel 主窗口句柄
     excelWindowHandle = FindWindow("XLMAIN", null);

     // 将用户控件嵌入到 Excel 窗口中
     IntPtr userControlHandle = myUserControl.Handle;
     SetParent(userControlHandle, excelWindowHandle);

     // 调整用户控件的位置和大小
     MoveWindow(userControlHandle, 100, 100, 300, 200, true);
 }

 private void ThisAddIn_Shutdown(object sender, EventArgs e)
 {
     // 清理资源
     myUserControl.Dispose();
 }

 // Windows API 函数声明
 [DllImport("user32.dll", SetLastError = true)]
 static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

 [DllImport("user32.dll", SetLastError = true)]
 static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

 [DllImport("user32.dll", SetLastError = true)]
 static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);


        

相关推荐

  1. 用户嵌入 Excel 窗口

    2024-07-22 16:38:10       16 阅读
  2. Qt 多进程编程-子程序嵌入窗口

    2024-07-22 16:38:10       46 阅读
  3. C#: 导入excel文件 dataGridView

    2024-07-22 16:38:10       54 阅读
  4. 一个excel的数据分发多个excel文件

    2024-07-22 16:38:10       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 16:38:10       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 16:38:10       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 16:38:10       45 阅读
  4. Python语言-面向对象

    2024-07-22 16:38:10       55 阅读

热门阅读

  1. 数据仓库中的数据治理方法

    2024-07-22 16:38:10       17 阅读
  2. GFS分布式文件系统

    2024-07-22 16:38:10       15 阅读
  3. linux环境下使用logrotate切分nginx日志

    2024-07-22 16:38:10       18 阅读
  4. css中浮动的原理

    2024-07-22 16:38:10       15 阅读
  5. c# 索引器

    2024-07-22 16:38:10       17 阅读
  6. 初入C语言的主要难点

    2024-07-22 16:38:10       17 阅读
  7. PostgreSQL 慢 SQL 排查

    2024-07-22 16:38:10       19 阅读
  8. YARA:第十六章-libyara之C API手册(威胁检测)

    2024-07-22 16:38:10       15 阅读
  9. ipython 的使用技巧的整理

    2024-07-22 16:38:10       17 阅读