最小化后无法最大化应用

时间:2013-08-01 03:53:34

标签: vb.net

经过多次尝试和磨难后,我终于能够从winform按钮最小化我的应用程序。代码完美无缺:

    Private Sub btnMinWindow_Click(sender As Object, e As EventArgs) Handles btnMinWindow.Click

    'This procedure runs when the btnMinWindow is clicked.
    'The procedure minizmizes the form and Excel.


    Dim WB As Excel.Workbook = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook)

    Me.WindowState = FormWindowState.Minimized
    WB.Application.WindowState = XlWindowState.xlMinimized

End Sub

当然,什么是最小化最终需要最大化。所以我假设我所要做的只是使用xlMaximized相同的代码。当然,我的假设是错误的。这是我的代码:

    Private Sub btnMinEmployeeDashboard_Click(sender As Object, e As EventArgs) Handles btnMinEmployeeDashboard.Click

    'This procedure runs when the btnEmployeeDashboard button is clicked.

    Dim WB As Excel.Workbook = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook)
    WB.Application.WindowState = XlWindowState.xlMaximized
    WB.Sheets("employeeBoard").Select()

    Me.Close()


End Sub

代码关闭表单,选择工作表,但它不会最大化我的窗口。我认为我要做的就是使“已经最小化”的变量(在范围内)可用于按钮_Click处理程序并使用它而不是声明一个新变量。不幸的是,如果是这样的话,我不知道该怎么做。

2 个答案:

答案 0 :(得分:0)

尝试将Excel对象实例化为类/模块/全局变量,然后使用此变量在Excel文件保持打开状态时对其执行操作。

答案 1 :(得分:0)

经过一番研究后,我找到了能够让它发挥作用的代码。这是:

 Public Class Form1

    'Declare functions and constants
    Private Declare Function ShowWindow Lib "user32" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer
    Private Const SW_SHOWMAXIMIZED As Integer = 3


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Specify part of the window title you want. Get its window handle:
        Dim hwnd As String = findPartialTitle("Notepad")

        'Send hwnd to showwindow function
        ShowWindow(hwnd, SW_SHOWMAXIMIZED)

    End Sub

    'FUNCTION: Find handle by partial window title
    Private Function findPartialTitle(ByVal partialTitle As String) As IntPtr
        For Each p As Process In Process.GetProcesses()
            If p.MainWindowTitle.IndexOf(partialTitle, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
                Return p.MainWindowHandle
            End If
        Next
        Return IntPtr.Zero
    End Function

End Class

http://www.techusers.net/60/how-maximize-external-application-window-title-vbnet