Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#显示隐藏窗口_C#_Windows_Hwnd - Fatal编程技术网

C#显示隐藏窗口

C#显示隐藏窗口,c#,windows,hwnd,C#,Windows,Hwnd,我正在为excel开发一个外接程序。在某种程度上,我可以接收异步事件。如果在这些事件中隐藏,我需要能够显示Excel窗口 我能够存储Hwnd属性,我认为它必须是一个不可变的int/reference来标识我的Excel窗口 有人能详细说明一下这个Hwnd吗?并解释如何使用C#显示隐藏窗口 提前感谢各位;) 更新:很快,这就是解决我问题的代码: /// <summary>Enumeration of the different ways of showing a window

我正在为excel开发一个外接程序。在某种程度上,我可以接收异步事件。如果在这些事件中隐藏,我需要能够显示Excel窗口

我能够存储
Hwnd
属性,我认为它必须是一个不可变的int/reference来标识我的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);
    }
///使用
///橱窗
私有枚举窗口显示样式:uint
{
///隐藏窗口并激活另一个窗口。
///请参阅SW_HIDE
Hide=0,
///激活并显示窗口。如果窗口最小化
///或最大化,系统将其恢复到其原始大小和
///位置。应用程序在显示时应指定此标志
///这是第一次打开窗户。
///见SW_SHOWNORMAL
ShowNormal=1,
///激活窗口并将其显示为最小化窗口。
///见SW_-Show
show=2,
///激活窗口并将其显示为最大化窗口。
///见SW_showmized
显示最大化=3,
///最大化指定的窗口。
///见SW_
最大化=3,
///显示窗口的最新大小和位置。
///该值与“ShowNormal”类似,只是窗口不可用
///活跃的。
///请参阅SW_SHOWNOACTIVATE
ShowNormalNoActivate=4,
///激活窗口并以其当前大小显示它
///和位置。
///见SW_SHOW
Show=5,
///最小化指定的窗口并激活下一个窗口
///按Z顺序排列的顶级窗口。
///见SW_
最小化=6,
///将窗口显示为最小化窗口。此值为
///与“ShowMinimized”类似,只是窗口未激活。
///见SW_showminoactive
showminoactivate=7,
///以当前大小和位置显示窗口。此
///值与“显示”类似,只是窗口未激活。
///见SW_SHOWNA
ShowNoActivate=8,
///激活并显示窗口。如果窗口
///最小化或最大化后,系统会将其恢复为原始大小
///和位置。应用程序在还原时应指定此标志
///最小化的窗口。
///参见SW_恢复
恢复=9,
///根据中指定的SW_uu值设置显示状态
///由传递给CreateProcess函数的STARTUPINFO结构
///启动应用程序的程序。
///请参阅SW_SHOWDEFAULT
ShowDefault=10,
///Windows 2000/XP:最小化窗口,即使线程
///拥有该窗口的已挂起。仅当
///从不同线程最小化窗口。
///见SW_
力最小化=11
}
[DllImport(“user32.dll”)]
静态外部布尔显示窗口(IntPtr hWnd、WindowsShowStyle nCmdShow);
静态void ContentClick(对象对象对象、事件参数)
{
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.thishworkbook.Application.Hwnd,WindowShowStyle.ShowMaximized);
}

hWnd
表示窗口句柄。它是窗口实例的标识句柄

至于显示它,您可以使用API。这是P/Invoke签名,由以下人员提供:

下面是
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
}
///使用
///橱窗
私有枚举窗口显示样式:uint
{
///隐藏窗口并激活另一个窗口。
///请参阅SW_HIDE
Hide=0,
///激活并显示窗口。如果窗口最小化
///或最大化,系统将其恢复到其原始大小和
///位置。应用程序在显示时应指定此标志
///这是第一次打开窗户。
///见SW_SHOWNORMAL
ShowNormal=1,
///激活窗口并将其显示为最小化窗口。
///见SW_-Show
show=2,
///激活窗口并将其显示为最大化窗口。
///见SW_showmized
显示最大化=3,
///最大化指定的窗口。
///见SW_
最大化=3,
///显示窗口的最新大小和位置。
///该值与“ShowNormal”类似,只是窗口不可用
///活跃的。
///请参阅SW_SHOWNOACTIVATE
ShowNormalNoActivate=4,
///激活窗口并以其当前大小显示它
///和位置。
///见SW_SHOW
Show=5,
///最小化指定的窗口并激活下一个窗口
///按Z顺序排列的顶级窗口。
///见SW_
最小化=6,
///将窗口显示为最小化窗口。此值为
///与“ShowMinimized”类似,只是窗口未激活。
///见SW_showminoactive
showminoactivate=7,
///以当前大小和位置显示窗口。此
///值与“显示”类似,只是窗口未激活。
///见SW_SHOWNA
ShowNoActivate=8,
///激活并显示窗口。如果窗口
///最小化或最大化后,系统会将其恢复为原始大小
///和位置。应用程序在还原时应指定此标志
///最小化的窗口。
///参见SW_恢复
恢复=9,
///基于S设置显示状态
/// <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
}
public class WndWrapper : IWin32Window
{
    IntPtr m_Handle;
    public WndWrapper(long pHandle)
    {
        m_Handle = (IntPtr)pHandle;
    }

    #region IWin32Window Members

    public IntPtr Handle
    {
        get { return m_Handle; }
    }

    #endregion
}
WndWrapper oWnd = new WndWrapper(pHandle);
MyForm oDlg = new MyForm();
oDlg.Show(oWnd);