none
O código abaixo consegue fechar a janela de outros programas externos, entretanto gostaria de implementa-lo com minimizar, normalizar e maximizar janelas externas. RRS feed

  • Pergunta

  • Imports System.Runtime.InteropServices
    Imports System.Text

    Public Class Form1

        Dim strTitle As String = String.Empty
        <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
        Private Shared Function GetForegroundWindow() As IntPtr
        End Function
        <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
        Private Shared Function GetWindowText(hWnd As IntPtr, text As StringBuilder, count As Integer) As Integer
        End Function
        <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
        Private Shared Function GetWindowTextLength(hWnd As IntPtr) As Integer
        End Function

        Private Sub GetCaptionOfActiveWindow()
            Dim handle As IntPtr = GetForegroundWindow()
            ' Obtain the length of the text   
            Dim intLength As Integer = GetWindowTextLength(handle) + 1
            Dim stringBuilder As New StringBuilder(intLength)
            If GetWindowText(handle, stringBuilder, intLength) > 0 Then
                strTitle = stringBuilder.ToString()
            End If
            Label1.Text = strTitle
        End Sub


        Private Declare Auto Function FindWindowEx Lib "user32" (ByVal parentHandle As Integer, _
        ByVal childAfter As Integer, _
        ByVal lclassName As String, _
        ByVal windowTitle As String) As Integer

        Private Declare Auto Function PostMessage Lib "user32" (ByVal hwnd As Integer, _
          ByVal message As UInteger, _
         ByVal wParam As Integer, _
           ByVal lParam As Integer) As Boolean

        Dim WM_CLOSE As UInteger = &H10

        '-----------------------------------------------------------------------

        '---------------------------------------------------------------------------
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            GetCaptionOfActiveWindow()
        End Sub

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'fecha janela com o nome
            If strTitle <> "Form1" Then
                Dim handle As Integer = FindWindowEx(0, 0, Nothing, Label1.Text)
                PostMessage(handle, WM_CLOSE, 0, 0)
            End If
        End Sub
    End Class
    quarta-feira, 21 de março de 2018 18:43

Respostas

  • Desta forma

    Public Class Form1
    
        Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer
        Private Const SW_SHOWNORMAL As Integer = 1
        Private Const SW_SHOWMINIMIZED As Integer = 2
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim iHwnd As IntPtr = FindWindow("notepad", vbNullString)
            ShowWindow(iHwnd, SW_SHOWMINIMIZED)
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim iHwnd As IntPtr = FindWindow("notepad", vbNullString)
            ShowWindow(iHwnd, SW_SHOWNORMAL)
        End Sub
    
    End Class

    quinta-feira, 22 de março de 2018 02:17