C# 显示隐藏窗口

2024-02-20

我正在开发一个 Excel 插件。在某些时候,我可以接收异步事件。如果 Excel 窗口在这些事件上隐藏,我需要能够显示该窗口。

我能够存储Hwnd属性,我相信它必须是一个不可变的 int/引用来标识我的 Excel 窗口。

有人可以详细说明这个 Hwnd 吗?并解释如何使用它显示 C# 中的隐藏窗口?

预先感谢大家;)

更新:很快,这就是解决我的问题的代码:

    /// <summary>Enumeration of the different ways of showing a window using
    /// ShowWindow</summary>
    private enum WindowShowStyle : uint
    {
        /// <summary>Hides the window and activates another window.</summary>
        /// <remarks>See SW_HIDE</remarks>
        Hide = 0,
        /// <summary>Activates and displays a window. If the window is minimized
        /// or maximized, the system restores it to its original size and
        /// position. An application should specify this flag when displaying
        /// the window for the first time.</summary>
        /// <remarks>See SW_SHOWNORMAL</remarks>
        ShowNormal = 1,
        /// <summary>Activates the window and displays it as a minimized window.</summary>
        /// <remarks>See SW_SHOWMINIMIZED</remarks>
        ShowMinimized = 2,
        /// <summary>Activates the window and displays it as a maximized window.</summary>
        /// <remarks>See SW_SHOWMAXIMIZED</remarks>
        ShowMaximized = 3,
        /// <summary>Maximizes the specified window.</summary>
        /// <remarks>See SW_MAXIMIZE</remarks>
        Maximize = 3,
        /// <summary>Displays a window in its most recent size and position.
        /// This value is similar to "ShowNormal", except the window is not
        /// actived.</summary>
        /// <remarks>See SW_SHOWNOACTIVATE</remarks>
        ShowNormalNoActivate = 4,
        /// <summary>Activates the window and displays it in its current size
        /// and position.</summary>
        /// <remarks>See SW_SHOW</remarks>
        Show = 5,
        /// <summary>Minimizes the specified window and activates the next
        /// top-level window in the Z order.</summary>
        /// <remarks>See SW_MINIMIZE</remarks>
        Minimize = 6,
        /// <summary>Displays the window as a minimized window. This value is
        /// similar to "ShowMinimized", except the window is not activated.</summary>
        /// <remarks>See SW_SHOWMINNOACTIVE</remarks>
        ShowMinNoActivate = 7,
        /// <summary>Displays the window in its current size and position. This
        /// value is similar to "Show", except the window is not activated.</summary>
        /// <remarks>See SW_SHOWNA</remarks>
        ShowNoActivate = 8,
        /// <summary>Activates and displays the window. If the window is
        /// minimized or maximized, the system restores it to its original size
        /// and position. An application should specify this flag when restoring
        /// a minimized window.</summary>
        /// <remarks>See SW_RESTORE</remarks>
        Restore = 9,
        /// <summary>Sets the show state based on the SW_ value specified in the
        /// STARTUPINFO structure passed to the CreateProcess function by the
        /// program that started the application.</summary>
        /// <remarks>See SW_SHOWDEFAULT</remarks>
        ShowDefault = 10,
        /// <summary>Windows 2000/XP: Minimizes a window, even if the thread
        /// that owns the window is hung. This flag should only be used when
        /// minimizing windows from a different thread.</summary>
        /// <remarks>See SW_FORCEMINIMIZE</remarks>
        ForceMinimized = 11
    }

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, WindowShowStyle nCmdShow);

    static void ContentClick(object obj, EventArgs ea)
    {
        Microsoft.Office.Interop.Excel.Application oExcelApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
        oExcelApp.Visible = true;
        ShowWindow( (System.IntPtr) Globals.ThisWorkbook.Application.Hwnd, WindowShowStyle.ShowMaximized);
    }

hWnd意思是窗口句柄。它是窗口实例的标识句柄。

至于显示它,你可以使用user32.ShowWindow http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548.aspxAPI。这是 P/Invoke 签名,由以下人员提供pinvoke.net http://www.pinvoke.net/default.aspx/user32.showwindow:

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);

这是ShowWindowCommands enum:

/// <summary>Enumeration of the different ways of showing a window using
/// ShowWindow</summary>
private enum WindowShowStyle : uint
{
    /// <summary>Hides the window and activates another window.</summary>
    /// <remarks>See SW_HIDE</remarks>
    Hide = 0,
    /// <summary>Activates and displays a window. If the window is minimized
    /// or maximized, the system restores it to its original size and
    /// position. An application should specify this flag when displaying
    /// the window for the first time.</summary>
    /// <remarks>See SW_SHOWNORMAL</remarks>
    ShowNormal = 1,
    /// <summary>Activates the window and displays it as a minimized window.</summary>
    /// <remarks>See SW_SHOWMINIMIZED</remarks>
    ShowMinimized = 2,
    /// <summary>Activates the window and displays it as a maximized window.</summary>
    /// <remarks>See SW_SHOWMAXIMIZED</remarks>
    ShowMaximized = 3,
    /// <summary>Maximizes the specified window.</summary>
    /// <remarks>See SW_MAXIMIZE</remarks>
    Maximize = 3,
    /// <summary>Displays a window in its most recent size and position.
    /// This value is similar to "ShowNormal", except the window is not
    /// actived.</summary>
    /// <remarks>See SW_SHOWNOACTIVATE</remarks>
    ShowNormalNoActivate = 4,
    /// <summary>Activates the window and displays it in its current size
    /// and position.</summary>
    /// <remarks>See SW_SHOW</remarks>
    Show = 5,
    /// <summary>Minimizes the specified window and activates the next
    /// top-level window in the Z order.</summary>
    /// <remarks>See SW_MINIMIZE</remarks>
    Minimize = 6,
      /// <summary>Displays the window as a minimized window. This value is
      /// similar to "ShowMinimized", except the window is not activated.</summary>
    /// <remarks>See SW_SHOWMINNOACTIVE</remarks>
    ShowMinNoActivate = 7,
    /// <summary>Displays the window in its current size and position. This
    /// value is similar to "Show", except the window is not activated.</summary>
    /// <remarks>See SW_SHOWNA</remarks>
    ShowNoActivate = 8,
    /// <summary>Activates and displays the window. If the window is
    /// minimized or maximized, the system restores it to its original size
    /// and position. An application should specify this flag when restoring
    /// a minimized window.</summary>
    /// <remarks>See SW_RESTORE</remarks>
    Restore = 9,
    /// <summary>Sets the show state based on the SW_ value specified in the
    /// STARTUPINFO structure passed to the CreateProcess function by the
    /// program that started the application.</summary>
    /// <remarks>See SW_SHOWDEFAULT</remarks>
    ShowDefault = 10,
    /// <summary>Windows 2000/XP: Minimizes a window, even if the thread
    /// that owns the window is hung. This flag should only be used when
    /// minimizing windows from a different thread.</summary>
    /// <remarks>See SW_FORCEMINIMIZE</remarks>
    ForceMinimized = 11
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# 显示隐藏窗口 的相关文章

  • 在 C# 中转换 VbScript 函数(Right、Len、IsNumeric、CInt)

    同样 我在 VbScript 中得到了以下代码 您能建议一下 C 中的等效代码吗 Function GetNavID Title getNavID UCase Left Title InStr Title 1 End Function 我已
  • 了解 VerQueryValue

    在 MSDN 上 我注意到 VerQueryValue 函数的以下内容 lplp缓冲区 输出 低电压空洞当此方法返回时 包含指向 pBlock 指向的缓冲区中所请求版本信息的指针的地址 当关联的 pBlock 内存被释放时 lplpBuff
  • C 语言中的套接字如何工作?

    我对 C 中的套接字编程有点困惑 You create a socket bind it to an interface and an IP address and get it to listen I found a couple of
  • 纹理映射 C++ OpenGL

    我已经阅读了相关内容 包括 Nehe 和此处的解决方案 但我找不到具体的答案 我正在尝试加载一张名为stars jpg 的照片 我想通过使用 uv 坐标映射它来使其成为场景的背景 方法是 glBegin GL QUADS glTexCoor
  • 如何通过实体键添加/删除与实体框架的多对多关系?

    I tried using Entities e new Entities EntityKey key new EntityKey Entities Users UserId 20 User user new User EntityKey
  • 如何让BackgroundWorker返回一个对象

    我需要做RunWorkerAsync 返回一个List
  • 如何正确实现带有 close 方法的处置模式(CA1063)

    框架设计指南 第二版 第 327 页 说 考虑提供方法Close 除了Dispose 如果接近 是该领域的标准术语 这样做时 重要的是使 Close 实现与Dispose并考虑实施IDisposable Dispose方法明确 因此 按照提
  • 为什么数组不可赋值? [复制]

    这个问题在这里已经有答案了 据我所知 C 标准禁止使用数组作为可修改的左值 即在赋值的左侧 int lhs 4 rhs 4 0 1 2 3 lhs rhs illegal 现在 我一直想知道为什么会这样 我可以看到上面的语句 以及写入数组的
  • 用于轻松动态反射的 C# 库

    是否有任何库 例如开源项目等 可以更轻松地使用复杂的反射 例如动态创建对象或类 检查实例等 Thanks 有一个LinFu http www codeproject com KB cs LinFuPart1 aspx可用的库除了反射之外还可
  • 如何使用 Linq to Sql 修剪值?

    在数据库中 我有一个名为 联系人 的表 名字和其他此类字符串字段设计为使用 Char 数据类型 不是我的数据库设计 我的对象 Contact 映射到属性中的字符串类型 如果我想做一个简单的测试 通过 id 检索 Contact 对象 我会这
  • 为什么这段代码不会产生编译错误?

    template
  • 扩展一个类

    编辑回答 虽然我最初的问题并没有完全按照康拉德 鲁道夫提供的答案所解决的方式解释我的需求 但他 无意或有意 基本上为我写了我想写的内容 类本身不会被扩展 但通过使类了解新函数来扩展其功能 这些新函数允许它 类 处理更广泛的问题 我非常感谢您
  • 当一种语言是另一种语言的平行超集时,这意味着什么?

    我正在阅读关于实时并发 C 的期刊文章 http link springer com article 10 1007 2FBF00365999 并且它在摘要中提到 因此你们中的任何人都可以通过该链接查看上下文 Concurrent C 是
  • 为什么 g++ 在编译的二进制文件中存储类名?

    我注意到如果我跑strings在我编译的程序上g 输出包含它使用的各种类的名称 该程序是用 O3并且没有 g or p 并且当我剥离二进制文件时 类名仍然存在 我想知道为什么有必要g 将此信息存储在二进制文件中 出现的类名似乎都是使用虚函数
  • 使用客户端 hello 消息进行 TLS 协议检测

    我需要检测网络流量中的 https 数据包 到目前为止 我将所有 443 标记为 https 但我不想再在这种情况下使用端口信息 检查客户端问候消息是否足够 Check 22 and version info 0300 0301 or 03
  • 如何获取数字列的确切类型,包括。规模和精度?

    有没有办法知道列中列的确切类型DataTable 现在我正在这样做 DataTable st dataReader GetSchemaTable foreach DataColumn col in st Columns var type c
  • 有没有办法将复选框列表绑定到 asp.net mvc 中的模型

    我在这里寻找一种快速简便的方法来在模型中发生回发时绑定复选框列表项的列表 显然现在常见的做法似乎是这样的form GetValues checkboxList 0 Contains true 这看起来很痛苦而且不太安全 有没有一种方法可以绑
  • Opencv 对象检测:ORB GPU 检测器和 SURF GPU 描述符提取器

    我只是做了一个小实验来尝试不同的检测器 描述符组合 我的代码使用 ORB GPU 检测器来检测特征 并使用 SURF GPU 描述符来计算描述符 我使用 BruteForceMatcher GPU 来匹配描述符 并使用 knnMatch 方
  • 在 C# 中设置风扇速度

    我知道以前有人问过这个问题 但我似乎无法让它发挥作用 我已调用以下内容 using System Management using System Management Instrumentation using System Runtime
  • 如何在 C# 中将 json 转换为平面结构

    我正在尝试用 C 编写函数 将 JSON 转换为键 值对 它应该支持数组 例如下面的 JSON title title value components component id id1 menu title menu title1 tit

随机推荐

  • React 卸载并重新挂载子组件

    我有一个 npm 导入的组件 CKEditor 它只关心其父组件在安装时的状态 也就是说 无论父组件的状态发生什么变化 如果父组件已经挂载 CKEditor 都不会反映这些变化 这对我来说是一个问题 因为当父组件更改其语言属性时 我需要 C
  • 如何在屏幕退出时隐藏有条件的自定义字段?

    我的任务是自定义标题详细信息屏幕ME33K交易 目标是添加一个包含新字段的框 仅当协议类型是我使用交易定义的类型时才应显示该框SPRO 例如 协议类型ABC 我开始使用CMOD交易中 我创建了一个虚拟框和带有一些硬编码输入值的字段 并且工作
  • Scala 相等与类型检查?

    是否有统一的方法来执行类型检查的相等性 很遗憾 val objectA String test val objectB Int 2 objectA objectB 如果 objectB 是 Int 而 objectA 是 String 则相
  • 具有多个 CDVViewController 的 Phonegap/Cordova

    我的想法是使用 Phonegap 作为我的应用程序的业务逻辑 但使用本机转换 所以我在每个 UIViewController 中都需要 CDVWebView 这对于普通的 UIWebviews 来说工作得很好 但是如果我使用多个 CDVVi
  • 在android中设计一个如下图所示的带有图像按钮的布局

    Hi 我想设计像上图这样的按钮 八个按钮以圆形方式 我很困惑如何将它们设计成圆形的板球标志 我已经尝试使用相对布局和线性布局 但不能这样做 请帮助我使这个观点得以实施 查看车轮组件 http developer digitalaria co
  • 选择 Hive 中的前 2 行

    我正在尝试根据配置单元 版本 0 11 中的工资从我的员工列表中检索前 2 个表 由于它不支持TOP功能 有什么替代方案吗 或者我们有定义一个UDF吗 是的 在这里你可以使用LIMIT 您可以通过以下查询尝试一下 SELECT FROM e
  • LWJGL 3.2.0+ 字体

    我使用 lwjgl 一段时间了 最 近我决定从固定功能管道切换到着色器 因此 当我启动程序时 我首先设置 ContextAttrib 3 2 这样我将使用 GL 3 2 问题是 当我打开更高版本的 GL 时 很多功能都不受支持 在切换到更高
  • Python:使用图表创建 Excel 工作表

    是否有任何模块可以在 Python 中创建带有嵌入图表的 Excel 图表 这个问题中提到的模块 https stackoverflow com questions 553019 python excel making reports好像没
  • 使用 CMake 设置应用程序图标

    有没有跨平台的方法来使用 CMake 设置应用程序图标 我正在使用 Cmake 3 0 2 和 Qt 5 4 在Qt中文档 http doc qt io qt 5 appicon html显示了一种方法 但它不是跨平台的 CMake 不会为
  • Heroku 上 React 应用程序的简单密码保护

    我有一个简单的 React 应用程序 是用 create react app 创建的 我想将其部署到 Heroku 或简单的地方 并进行密码保护 保护可以非常简单 只需一个密码就可以了 我开始研究 HTTP 基本身份验证 但没有找到简单的答
  • 为什么 PathPrefixStrip 可以工作而 PathPrefix 不能?

    我有一个 GatsbyJS 静态站点 prefix paths The pathPrefix被设定为 environment test in gatsby config js 它被部署到运行 Traefik 的 docker swarm 中
  • 本地目录路径

    一个相当简单的问题 如何找到我的exe所在的本地目录的路径 就像我有一个 exe 一样 在程序中我必须在 exe 所在的目录中创建一个 txt 文件 语言 C 因此 如果 exe 位于 C Temp 并从那里启动 我的 txt 应该在 C
  • 使用 R 删除列表中空的零长度行

    我有数据框列表 有些是空的 如何删除它们 S566X7221 1 V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23 V24
  • 如何从 Google Colab 访问我的 Google 云端硬盘文件?

    我使用以下网址从网址下载了图像urlretrieve urllib 在 Google Colab 中 但是 下载图像后 我无法找到图像 from google colab import drive drive mount content g
  • Vaadin:如何显示 MySQL 数据库中的数据?

    我正在开发 Vaadin Flow 版本 14 1 应用程序 但遇到了这个问题 我无法将其直接与 MySQL 数据库连接 我已经与 Maven 设置了 JDBC 连接 还创建了一个名为 Datasource 的单例类 在其中存储我的值和方法
  • 如何使用VBA在Excel中的所有行前面添加单引号

    我有一个电子表格 其中包含带有日期的第一列 我希望文本保留为 May 21 但采用字符串形式 单元格值应该是 May 21 但是当我尝试将其转换为字符串时 它使用 5 位数字 如果我将列保留为日期格式 它会首先自动选择 May 因此即使格式
  • 用于特定文本模式的 PHP 正则表达式

    在我的网站上 我在正文中插入了项目创建的年份 并将其替换为 六年前 或者无论有多长 所以在我的内容中我有 我们自 1998 年开始营业 并在 2011 年前制作了这种包装设计 我试图使用正则表达式将 2011 放入变量中以便稍后搜索和替换
  • 复制矩阵的一行或一列并将其插入到下一行/列中

    我想知道 MATLAB 中是否有一种简单的方法来执行以下操作 我想复制矩阵的行或列并将其插入到下一行 列中 例如 给定一个 3x3 矩阵 1 2 3 4 5 6 7 8 9 我想复制第一行并将其作为第二行插入 1 2 3 1 2 3 4 5
  • 提取模式两端由标识符括起来的多行

    我正在尝试从日志文件中提取特定事件的跟踪 为了查找相关事件 我查找 PATTERN 为了提取事件的完整跟踪 我希望提取由 SEPARATOR 包围的模式两端的行 例如 如果日志文件的内容是 Line1 Line2 SEP Line3 Lin
  • C# 显示隐藏窗口

    我正在开发一个 Excel 插件 在某些时候 我可以接收异步事件 如果 Excel 窗口在这些事件上隐藏 我需要能够显示该窗口 我能够存储Hwnd属性 我相信它必须是一个不可变的 int 引用来标识我的 Excel 窗口 有人可以详细说明这