如何使用AppActivate(ProcessID)还原最小化的窗口

时间:2010-01-27 22:01:43

标签: .net winforms

AppActivate(ProcessID)的文档声明......

  

AppActivate功能改变了   专注于指定的应用程序或   窗口,但不影响是否   最大化或最小化。

不幸的是,它没有建议您如何在激活时从任务栏取消最小化应用程序。

我在Process对象上找不到类似SetWindowState的东西,所以假设我有一个ProcessID和/或一个Process对象,可以做些什么来使窗口进入Normal或Maximized状态?

3 个答案:

答案 0 :(得分:4)

除了互操作之外,我没有任何其他方式。

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
const UInt32 WM_SYSCOMMAND = 0x0112;
const UInt32 SC_RESTORE    = 0xF120;

if (Process.MainWindowHandle != IntPtr.Zero)
   SendMessage(Process.MainWindowHandle, WM_SYSCOMMAND, SC_RESTORE, 0);

如果你不需要知道它何时被恢复,你也可以使用PostMessage。

答案 1 :(得分:0)

由于未知原因,似乎从VB6应用程序返回的Process.MainWindowHandle值不是传递给ShowWindow的适当值,以便从最小化状态恢复应用程序。

在应用程序标题不是常量的情况下,FindWindow API调用无用。下面的代码提供了一个函数,它将根据窗口的标题STARTING以指定值返回正在运行的应用程序的句柄。

示例用法:识别ID ...

Dim hwnd As Integer
Dim iProcessID As Integer

iProcessID = Shell("SampleApp.exe", AppWinStyle.NormalFocus)
hwnd = API.GetFirstWindowhandle("Sample App")

......恢复申请......

AppActivate(iProcessID)

If API.IsMinimized(hwnd) Then
   API.ShowWindow(hwnd)
End If

......功能惯例......

Imports System.Runtime.InteropServices

Public Class API

   Private Declare Function apiGetTopWindow Lib "user32" Alias "GetTopWindow" (ByVal hwnd As Integer) As Integer
   Private Declare Function apiGetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Integer
   Private Declare Function apiGetWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
   Private Declare Function apiGetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Integer) As Integer
   Private Declare Function apiGetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
   Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer
   Private Declare Function apiIsIconic Lib "user32" Alias "IsIconic" (ByVal hwnd As IntPtr) As Boolean

   Private Const GW_HWNDNEXT As Integer = 2
   Private Const SW_NORMAL As Integer = 1

   Public Shared Function GetFirstWindowHandle(ByVal sStartingWith As String) As Integer

      Dim hwnd As Integer
      Dim sWindowName As String

      Dim iHandle As Integer = 0

      hwnd = apiGetTopWindow(apiGetDesktopWindow)

      Do While hwnd <> 0
         sWindowName = zGetWindowName(hwnd)
         If sWindowName.StartsWith(sStartingWith) Then
            iHandle = hwnd
            Exit Do
         End If
         hwnd = apiGetWindow(hwnd, GW_HWNDNEXT)
      Loop

      Return iHandle

   End Function

   Public Shared Function IsMinimized(ByVal hwnd As Integer) As Boolean

      Dim ip As New IntPtr(hwnd)

      Return apiIsIconic(ip)

   End Function

   Public Shared Sub ShowWindow(ByVal hwnd As Integer)

      Dim ip As New IntPtr(hwnd)

      apiShowWindow(ip, SW_NORMAL)

   End Sub

   Private Shared Function zGetWindowName(ByVal hWnd As Integer) As String

      Dim nBufferLength As Integer
      Dim nTextLength As Integer
      Dim sName As String

      nBufferLength = apiGetWindowTextLength(hWnd) + 4
      sName = New String(" "c, nBufferLength)

      nTextLength = apiGetWindowText(hWnd, sName, nBufferLength)
      sName = sName.Substring(0, nTextLength) 

      Return sName

   End Function

End Class

答案 2 :(得分:0)

AppActivate(ProcessID)

SendKeys "{Enter}"

这意味着您必须按Enter键才能激活最小化过程

但是如果流程没有最小化,那么输入密钥将被发送到流程并影响流程。