让外部应用程序最小化并最大化事件 [英] Getting external application minimize and maximize events

查看:77
本文介绍了让外部应用程序最小化并最大化事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好:



以下过程允许我检测应用程序何时最大化或最小化。我写了它,因为我的工作簿上有一个winform打开了工作簿,但是每当工作簿最小化时,我希望表单最小化,同样当它最大化时表单应该设置为不正常。



这是我到目前为止检测到的,工作正常:



Hello:

The following procedure allows me to detect when the application has been maximized or minimized. I wrote it, because there is a winform on my workbook that opens up with the workbook, but whenever the workbook is minimized, I want the form to minimize as well, the same when it is maximized then the form should be set not normal.

Here is what I have so far to detect, which works fine:

Public Class frmNavigation

Private Const SW_SHOWMAXIMIZED As Integer = 3
    Private Const SW_SHOWMINIMIZED As Integer = 2
    Private Const SW_SHOWNORMAL As Integer = 1

    Private Structure POINTAPI
        Public x As Integer
        Public y As Integer
    End Structure

    Private Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure

    Private Structure WINDOWPLACEMENT
        Public Length As Integer
        Public flags As Integer
        Public showCmd As Integer
        Public ptMinPosition As POINTAPI
        Public ptMaxPosition As POINTAPI
        Public rcNormalPosition As RECT
    End Structure

    Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As IntPtr, ByRef lpwndpl As WINDOWPLACEMENT) As Integer
    Private Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As IntPtr

    Private Sub GetWindowStats(handle As IntPtr)
        Dim wp As WINDOWPLACEMENT
        wp.Length = System.Runtime.InteropServices.Marshal.SizeOf(wp)
        GetWindowPlacement(handle, wp)
        If wp.showCmd = SW_SHOWMAXIMIZED Then ' is window maximized?
            If handle = GetForegroundWindow Then ' is the window foreground?

                Me.WindowState = FormWindowState.Normal

            End If
        ElseIf wp.showCmd = SW_SHOWNORMAL Then
            If handle = GetForegroundWindow Then
                Me.WindowState = FormWindowState.Normal

            End If
        ElseIf wp.showCmd = SW_SHOWMINIMIZED Then
            Me.WindowState = FormWindowState.Minimized
        End If
    End Sub


End Class





以下是检测应用程序的步骤:





Here is the procedure to detect the application:

GetWindowStats(Process.GetProcessesByName("Excel")(0).MainWindowHandle)





这是我遇到问题的地方。我可以使用哪种表格活动?如果我使用Load事件,它只能工作一次。这需要在应用程序最小化或最大化时随时工作,这可能在打开应用程序后多次,而不仅仅是Load事件。



Here is where I have the problem. Which form event can I use? If I used the Load event, it only works once. This needs to work anytime the application is minimized or maximized which could be multiple times after opening the application, not just the Load event.

推荐答案

这样做的最终方式事情是使用全局 Windows Hook 。请参阅我最近的回答:

Windows窗体和Word自动化 [ ^ ]。



但是,我建议你要小心。在大多数情况下,我建议完全避免使用外部流程,不包括为此目的专门设计的一些流程。



或者,如果外部流程基于 System.Windows.Forms ,您可以考虑自动化(但也要小心)。请参阅此文章: http://msdn.microsoft.com/en-us/library/ms996405.aspx [ ^ ]。< br $>


-SA
An ultimate way of doing such thing is using the global Windows Hook. Please see my recent answer:
Windows Forms and Word Automation[^].

However, I would advice your to be careful. In most cases, I would recommend to avoid working with external processes at all, excluding some specially designed for such purposes.

Alternatively, if the external process is based on System.Windows.Forms, you could consider Automation (but also be careful). Please see this article: http://msdn.microsoft.com/en-us/library/ms996405.aspx[^].

—SA


这篇关于让外部应用程序最小化并最大化事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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