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# 显示隐藏窗口 的相关文章

  • 通过 CMIS (dotCMIS) 连接到 SP2010:异常未经授权

    我正在使用 dotCMIS 并且想要简单连接到我的 SP2010 服务器 我尝试用 C 来做到这一点 如下所示http chemistry apache org dotnet getting started with dotcmis htm
  • 按成员序列化

    我已经实现了template
  • 在哪里可以找到列出 SSE 内在函数操作的官方参考资料?

    是否有官方参考列出了 GCC 的 SSE 内部函数的操作 即 头文件中的函数 除了 Intel 的 vol 2 PDF 手册外 还有一个在线内在指南 https www intel com content www us en docs in
  • 用于检查类是否具有运算符/成员的 C++ 类型特征[重复]

    这个问题在这里已经有答案了 可能的重复 是否可以编写一个 C 模板来检查函数是否存在 https stackoverflow com questions 257288 is it possible to write a c template
  • 嵌套接口:将 IDictionary> 转换为 IDictionary>?

    我认为投射一个相当简单IDictionary
  • 类模板参数推导 - clang 和 gcc 不同

    下面的代码使用 gcc 编译 但不使用 clang 编译 https godbolt org z ttqGuL template
  • HTTPWebResponse 响应字符串被截断

    应用程序正在与 REST 服务通信 Fiddler 显示作为 Apps 响应传入的完整良好 XML 响应 该应用程序位于法属波利尼西亚 在新西兰也有一个相同的副本 因此主要嫌疑人似乎在编码 但我们已经检查过 但空手而归 查看流读取器的输出字
  • OleDbDataAdapter 未填充所有行

    嘿 我正在使用 DataAdapter 读取 Excel 文件并用该数据填充数据表 这是我的查询和连接字符串 private string Query SELECT FROM Sheet1 private string ConnectStr
  • 将 VSIX 功能添加到 C# 类库

    我有一个现有的单文件生成器 位于 C 类库中 如何将 VSIX 项目级功能添加到此项目 最终目标是编译我的类库项目并获得 VSIX 我实际上是在回答我自己的问题 这与Visual Studio 2017 中的单文件生成器更改 https s
  • C++ OpenSSL 导出私钥

    到目前为止 我成功地使用了 SSL 但遇到了令人困惑的障碍 我生成了 RSA 密钥对 之前使用 PEM write bio RSAPrivateKey 来导出它们 然而 手册页声称该格式已经过时 实际上它看起来与通常的 PEM 格式不同 相
  • 创建链表而不将节点声明为指针

    我已经在谷歌和一些教科书上搜索了很长一段时间 我似乎无法理解为什么在构建链表时 节点需要是指针 例如 如果我有一个节点定义为 typedef struct Node int value struct Node next Node 为什么为了
  • 将多个表映射到实体框架中的单个实体类

    我正在开发一个旧数据库 该数据库有 2 个具有 1 1 关系的表 目前 我为每个定义的表定义了一种类型 1Test 1Result 我想将这些特定的表合并到一个类中 当前的类型如下所示 public class Result public
  • 重载<<的返回值

    include
  • Windows C++ 中的键盘钩子还是什么?

    我希望构建自己的应用程序 它可以将键盘命令 消息 发送到 Windows 操作系统 例如 当我按下组合键 ctrl shift n 时 我希望启动 notepad exe 我怎样才能做到这一点 您对所使用的概念有什么建议吗 我读过 何时使用
  • 如何查看网络连接状态是否发生变化?

    我正在编写一个应用程序 用于检查计算机是否连接到某个特定网络 并为我们的用户带来一些魔力 该应用程序将在后台运行并执行检查是否用户请求 托盘中的菜单 我还希望应用程序能够自动检查用户是否从有线更改为无线 或者断开连接并连接到新网络 并执行魔
  • 这些作业之间是否存在顺序点?

    以下代码中的两个赋值之间是否存在序列点 f f x 1 1 x 2 不 没有 在这种情况下 标准确实是含糊不清的 如果你想确认这一点 gcc 有这个非常酷的选项 Wsequence point在这种情况下 它会警告您该操作可能未定义
  • WPF/C# 将自定义对象列表数据绑定到列表框?

    我在将自定义对象列表的数据绑定到ListBox in WPF 这是自定义对象 public class FileItem public string Name get set public string Path get set 这是列表
  • 将控制台重定向到 .NET 程序中的字符串

    如何重定向写入控制台的任何内容以写入字符串 对于您自己的流程 Console SetOut http msdn microsoft com en us library system console setout aspx并将其重定向到构建在
  • 基于 OpenCV 边缘的物体检测 C++

    我有一个应用程序 我必须检测场景中某些项目的存在 这些项目可以旋转并稍微缩放 更大或更小 我尝试过使用关键点检测器 但它们不够快且不够准确 因此 我决定首先使用 Canny 或更快的边缘检测算法 检测模板和搜索区域中的边缘 然后匹配边缘以查
  • C++ 标准是否指定了编译器的 STL 实现细节?

    在写答案时this https stackoverflow com questions 30909296 can you put a pimpl class inside a vector我遇到了一个有趣的情况 这个问题演示了这样一种情况

随机推荐

  • 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 窗口 有人可以详细说明这