C#显示隐藏的窗口 [英] C# show hidden window

查看:148
本文介绍了C#显示隐藏的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用于Excel加载英寸在某些时候,我可以接收异步事件。我需要能够显示,如果隐藏在这些事件的Excel窗口。



我能够存储 HWND 属性,我相信一定是一个不变的INT /参考,以确定我的Excel窗口。



有人能解释一下HWND? ?在此先人,并解释我如何使用它表明从C#隐藏的窗口



感谢;)



更新:不久,那是一段代码,整理我的问题:

  ///<总结>不同的枚举使用
///的ShowWindow<显示窗口的方式; /总结>
私人枚举WindowShowStyle:UINT
{
///<总结>隐藏窗口并激活另一个窗口< /总结>
///<&言论GT;看到SW_HIDE< /言论>
隐藏= 0,
///<总结>激活并显示一个窗口。如果窗口最小化
///或最大化,系统将它恢复到原来的大小和
///位置。显示
///窗口在第一次时,应用程序应该指定该标志< /总结>
///<&言论GT;看到SW_SHOWNORMAL< /言论>
ShowNormal = 1,
///<总结>激活窗口,并将其显示为最小化窗口< /总结>
///<&言论GT;看到SW_SHOWMINIMIZED< /言论>
ShowMinimized = 2,
///<总结>激活窗口,并将其显示为最大化窗口< /总结>
///<&言论GT;看到SW_SHOWMAXIMIZED< /言论>
ShowMaximized = 3,
///<总结>最大化指定窗口< /总结>
///<&言论GT;看到SW_MAXIMIZE< /言论>
最大化= 3,
///<总结>显示在其最近的大小和位置的窗口。
///此值是类似ShowNormal,除了窗口不是
///活化&下; /总结>
///<&言论GT;看到SW_SHOWNOACTIVATE< /言论>
ShowNormalNoActivate = 4,
///<总结>激活窗口,并显示在其当前大小
///和位置< /总结>
///<&言论GT;看到SW_SHOW< /言论>
展= 5,
///<总结>最小化指定的窗口,并按照Z顺序激活下一个
///顶层窗口< /总结>
///<&言论GT;看到SW_MINIMIZE< /言论>
最小化= 6,
///<总结>显示窗口为最小化窗口。这个值是
///类似ShowMinimized,但没有被激活的窗口< /总结>
///<&言论GT;看到SW_SHOWMINNOACTIVE< /言论>
ShowMinNoActivate = 7,
///&所述;总结>显示在其当前大小和位置的窗口。这种
///值是类似秀,除了窗户没有被激活< /总结>
///<&言论GT;看到SW_SHOWNA< /言论>
ShowNoActivate = 8,
///&所述;总结>激活并显示该窗口。如果窗口
///最小化或最大化,系统将它恢复到原来的大小
///和位置。恢复
///时最小化窗口的应用程序应该指定该标志< /总结>
///<&言论GT;看到SW_RESTORE< /言论>
恢复= 9,
///<总结>设置基于
指定的SW_值/// STARTUPINFO结构由$ B $传递给CreateProcess函数显示状态; b ///程序启动应用程序< /总结>
///<&言论GT;看到SW_SHOWDEFAULT< /言论>
ShowDefault = 10,
///&所述;总结>在Windows 2000 / XP:最小化的窗口,即使线程
///拥有窗口挂起。这个标志应该只用于在
///来自不同的线程最小化窗口< /总结>
///<&言论GT;看到SW_FORCEMINIMIZE< /言论>
ForceMinimized = 11
}

函数[DllImport(user32.dll中)]
静态外部布尔的ShowWindow(IntPtr的的HWND,WindowShowStyle的nCmdShow);

静态无效ContentClick(obj对象,EventArgs的EA)
{
Microsoft.Office.Interop.Excel.Application oExcelApp =(Microsoft.Office.Interop.Excel.Application)系统.Runtime.InteropServices.Marshal.GetActiveObject(Excel.Application);
oExcelApp.Visible = TRUE;
的ShowWindow((System.IntPtr)Globals.ThisWorkbook.Application.Hwnd,WindowShowStyle.ShowMaximized);
}


解决方案

的hWnd 表示窗口句柄。这对窗口实例的标识句柄。



至于显示它,你可以使用的 user32.ShowWindow API。这里的的P / Invoke签名,礼貌 pinvoke.net



 函数[DllImport(user32.dll中)] 
静态外部布尔的ShowWindow(IntPtr的的HWND,ShowWindowCommands的nCmdShow);

和这里的 ShowWindowCommands 枚举:

  ///<总结> /总结>使用
///的ShowWindow<显示窗口的不同方式枚举;
私人枚举WindowShowStyle:UINT
{
///<总结>隐藏窗口并激活另一个窗口< /总结>
///<&言论GT;看到SW_HIDE< /言论>
隐藏= 0,
///<总结>激活并显示一个窗口。如果窗口最小化
///或最大化,系统将它恢复到原来的大小和
///位置。显示
///窗口在第一次时,应用程序应该指定该标志< /总结>
///<&言论GT;看到SW_SHOWNORMAL< /言论>
ShowNormal = 1,
///<总结>激活窗口,并将其显示为最小化窗口< /总结>
///<&言论GT;看到SW_SHOWMINIMIZED< /言论>
ShowMinimized = 2,
///<总结>激活窗口,并将其显示为最大化窗口< /总结>
///<&言论GT;看到SW_SHOWMAXIMIZED< /言论>
ShowMaximized = 3,
///<总结>最大化指定窗口< /总结>
///<&言论GT;看到SW_MAXIMIZE< /言论>
最大化= 3,
///<总结>显示在其最近的大小和位置的窗口。
///此值是类似ShowNormal,除了窗口不是
///活化&下; /总结>
///<&言论GT;看到SW_SHOWNOACTIVATE< /言论>
ShowNormalNoActivate = 4,
///<总结>激活窗口,并显示在其当前大小
///和位置< /总结>
///<&言论GT;看到SW_SHOW< /言论>
展= 5,
///<总结>最小化指定的窗口,并按照Z顺序激活下一个
///顶层窗口< /总结>
///<&言论GT;看到SW_MINIMIZE< /言论>
最小化= 6,
///<总结>显示窗口为最小化窗口。这个值是
///类似ShowMinimized,但没有被激活的窗口< /总结>
///<&言论GT;看到SW_SHOWMINNOACTIVE< /言论>
ShowMinNoActivate = 7,
///&所述;总结>显示在其当前大小和位置的窗口。这种
///值是类似秀,除了窗户没有被激活< /总结>
///<&言论GT;看到SW_SHOWNA< /言论>
ShowNoActivate = 8,
///&所述;总结>激活并显示该窗口。如果窗口
///最小化或最大化,系统将它恢复到原来的大小
///和位置。恢复
///时最小化窗口的应用程序应该指定该标志< /总结>
///<&言论GT;看到SW_RESTORE< /言论>
恢复= 9,
///<总结>设置基于
指定的SW_值/// STARTUPINFO结构由$ B $传递给CreateProcess函数显示状态; b ///程序启动应用程序< /总结>
///<&言论GT;看到SW_SHOWDEFAULT< /言论>
ShowDefault = 10,
///&所述;总结>在Windows 2000 / XP:最小化的窗口,即使线程
///拥有窗口挂起。这个标志应该只用于在
///来自不同的线程最小化窗口< /总结>
///<&言论GT;看到SW_FORCEMINIMIZE< /言论>
ForceMinimized = 11
}


I am developing an add in for excel. At some point, I can receive async events. I need to be able to show the Excel window if hidden on these events.

I am able to store the Hwnd property, which I believe must be an immutable int/reference to identify my Excel window.

Can someone elaborate on this Hwnd ? and explain how I can show a hidden window from C# using it ?

Thanks in advance folks ;)

UPDATE : shortly, that was the piece of code that sorted my problems :

    /// <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 means Window Handle. It's an identifying handle for the window instance.

As for showing it, you could use the user32.ShowWindow API. Here's the P/Invoke signature, courtesy of pinvoke.net:

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

And here's the 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
}

这篇关于C#显示隐藏的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆