|
|
|
|
|
Dicas
|
|
Visual Basic (Miscelâneas)
|
|
|
Título da Dica: Controlar as janelas de uma aplicação qualquer
|
|
|
|
Postada em 2/1/2004 por geronimo
controlar as janelas de uma aplicação qualquer de dentro do nosso programa VB como fechar, minimizar, etc.... Fonte: www.codex.com.br Desenhe um Listbox e 8 CommandButtons com o nome Command1 e a propriedade Index de 0 a 7, assim eles formam um control array. Então cole o código abaixo:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Sub SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Const SW_HIDE = 0 Private Const SW_SHOW = 5 Private Const SW_NORMAL = 1 Private Const SW_MINIMIZE = 6 Private Const SW_MAXIMIZE = 3 Private Const WM_CLOSE = &H10
Dim CL As New Collection Dim AppHwnd As Long
Private Sub TrataJanela(FN As Integer)
If AppHwnd > 0 Then Select Case FN Case 0 ' No Topo SetWindowPos AppHwnd, -1, 0, 0, 0, 0, &H2 Or &H1 Case 1 ' Tras para Frente SetForegroundWindow AppHwnd Case 2 ' Minimiza SetWindowPos AppHwnd, -2, 0, 0, 0, 0, &H2 Or &H1 ShowWindow AppHwnd, SW_MINIMIZE Case 3 ' Normaliza ShowWindow AppHwnd, SW_NORMAL Case 4 ' Maximiza SetWindowPos AppHwnd, -2, 0, 0, 0, 0, &H2 Or &H1 ShowWindow AppHwnd, SW_MAXIMIZE Case 5 ' Esconde SetWindowPos AppHwnd, -2, 0, 0, 0, 0, &H2 Or &H1 ShowWindow AppHwnd, SW_HIDE Case 6 ' Mostra ShowWindow AppHwnd, SW_SHOW Case 7 ' Fecha SetWindowPos AppHwnd, -2, 0, 0, 0, 0, &H2 Or &H1 PostMessage AppHwnd, WM_CLOSE, 0&, 0& End Select End If
End Sub
Private Sub Command1_Click(Index As Integer)
' Manda executar a tarefa de acordo com o indice do botão pressionado TrataJanela Index
End Sub
Private Sub Form_Load()
Dim I As Integer
Me.Caption = "Controle de Aplicativos"
Command1(0).Caption = "Poe no topo" Command1(1).Caption = "Tras p/ Frente" Command1(2).Caption = "Minimiza" Command1(3).Caption = "Normaliza" Command1(4).Caption = "Maximiza" Command1(5).Caption = "Esconde" Command1(6).Caption = "Mostra" Command1(7).Caption = "Fecha"
' Desabilita os botões For I = 0 To Command1.UBound Command1(I).Enabled = False Next
' Guarda as classes numa collection CL.Add "OpusApp", "MS Word" CL.Add "OMain", "MS Access" CL.Add "XLMAIN", "MS Excel" CL.Add "wndclass_desked_gsk", "MS Visual Basic" CL.Add "Notepad", "Notepad" CL.Add "IEFrame", "Internet Explorer" CL.Add "SciCalc", "Calculadora" CL.Add "ConsoleWindowClass", "Prompt do DOS" CL.Add "CabinetWClass", "Meu Computador"
' Enche o Listbox com as aplicações With List1 .AddItem "MS Word" .AddItem "MS Excel" .AddItem "MS Access" .AddItem "MS Visual Basic" .AddItem "Notepad" .AddItem "Internet Explorer" .AddItem "Calculadora" .AddItem "Prompt do DOS" .AddItem "Meu Computador" End With
End Sub
Private Sub List1_Click()
Dim I As Integer, R As Boolean
' Recupera o handle da janela escolhida no listbox AppHwnd = FindWindow(CL.Item(List1.Text), vbNullString)
' Habilita os botões se o handle for válido R = (AppHwnd > 0) For I = 0 To Command1.UBound Command1(I).Enabled = R Next
End Sub
|
|
|
|
|