自学内容网 自学内容网

将用户控件嵌入到 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);


        


原文地址:https://blog.csdn.net/lijingguang/article/details/140542658

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!