0

经过多次尝试和磨难,我终于能够从一个 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 处理程序并使用它而不是声明一个新变量。不幸的是,如果是这样的话,我不知道该怎么做。

4

2 回答 2

0

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

于 2013-08-01T06:52:29.070 回答
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

于 2013-08-02T02:13:06.180 回答