Newsgroup: it.comp.lang.visual-basic
Titolo: Messaggio di errore

Durante l'esecuzione del mio programma in VB6 si apre una maschera di errore 
generato da una InternetDial che non riesce a creare una connessione.
Il messaggio di errore prevede i tasti "Riprova/Annulla/Help".
Come faccio ad intercettare la finestra e a fare in modo che venga 
automaticamente premuto il tasto "Annulla"?
Ciň mi consentirebbe di riprendere il controllo dell'applicazione.
Ho tentato con - Sendkeys "{ESC}" - ma l'output della tastiera sembra non 
essere indirizzato nella maschera di errore nonostante questa abbia il 
focus, ma viene inoltrato sul form dal quale ho lanciato la funzione API 
InternetDial.
Sono quasi disperato: Aiuto!!!!
Mood 

Sono quasi disperato: Aiuto!!!!
Puoi provare con SendInput,
Di seguito un esempio per mandare una combinazione di tasti ed un programma
(eDrawing)
____________________________
DefLng A-Z
'Add this code to a module
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal
lParam As Long) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd
As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA"
(ByVal hWnd As Long) As Long
Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal
hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long,
ByVal cy As Long, ByVal wFlags As Long) As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function SetActiveWindow Lib "user32.dll" (ByVal hWnd As Long) As Long
Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As Long) As Long
 
Declare Function GetWindowPlacement Lib "user32" (ByVal hWnd As Long, lpwndpl As
WINDOWPLACEMENT) As Long
Declare Function SetWindowPlacement Lib "user32" (ByVal hWnd As Long, lpwndpl As
WINDOWPLACEMENT) As Long
Const VK_CAPITAL = &H14
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_USED = VK_SCROLL
Public Type KeyboardBytes
    kbByte(0 To 255) As Byte
End Type
 
 Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Long
 Declare Function GetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As
Long
 Declare Function SetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As
Long
Public Type RECT
  Left    As Long
  Top     As Long
  Right   As Long
  Bottom  As Long
End Type
Public Type POINTAPI
  x       As Long
  y       As Long
End Type
Public Type WINDOWPLACEMENT
  Length            As Long
  flags             As Long
  showCmd           As Long
  ptMinPosition     As POINTAPI
  ptMaxPosition     As POINTAPI
  rcNormalPosition  As RECT
End Type
Public Const SW_SHOWNORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_SHOWNOACTIVATE = 4
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA"
(ByVal hWnd As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal
hWndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String,
ByVal lpszWindow As String) As Long
Private Const WM_SETTEXT = 12
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Type GENERALINPUT
dwType As Long
xi(0 To 23) As Byte
End Type
Private Type KEYBDINPUT
wVk As Integer
wScan As Integer
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type
Private Const INPUT_KEYBOARD As Long = 1
Private Const KEYEVENTF_KEYUP As Long = 2
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As
Any, pSrc As Any, ByVal ByteLen As Long)
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long,
pInputs As GENERALINPUT, ByVal cbSize As Long) As Long
Dim Txsrc As String, HnSr As Long, kbOld As KeyboardBytes, kbArray As
KeyboardBytes
Sub Send_CtrlP(hwd As Long)
Dim GInput(2) As GENERALINPUT
Dim KInput As KEYBDINPUT
'hwndNotepad = FindWindowEx(0, 0, "Notepad", vbNullString)
'Debug.Assert hwndNotepad <> 0
'Press Ctrl
KInput.wVk = vbKeyControl
KInput.dwFlags = 0
GInput(0).dwType = INPUT_KEYBOARD
CopyMemory GInput(0).xi(0), KInput, Len(KInput)
'Press P
KInput.wVk = vbKeyP
KInput.dwFlags = 0
GInput(1).dwType = INPUT_KEYBOARD
CopyMemory GInput(1).xi(0), KInput, Len(KInput)
'Release P
KInput.wVk = vbKeyP
KInput.dwFlags = KEYEVENTF_KEYUP
GInput(2).dwType = INPUT_KEYBOARD
CopyMemory GInput(2).xi(0), KInput, Len(KInput)
'Release Ctrl
KInput.wVk = vbKeyControl
KInput.dwFlags = KEYEVENTF_KEYUP
GInput(2).dwType = INPUT_KEYBOARD
CopyMemory GInput(2).xi(0), KInput, Len(KInput)
'Now, finally send the keystrokes
SetForegroundWindow hwd
SendInput 4, GInput(0), Len(GInput(0))
End Sub
Sub Send_Enter(hwd As Long)
Dim GInput(2) As GENERALINPUT
Dim KInput As KEYBDINPUT
'hwndNotepad = FindWindowEx(0, 0, "Notepad", vbNullString)
'Debug.Assert hwndNotepad <> 0
'Press Return
KInput.wVk = vbKeyReturn
KInput.dwFlags = 0
GInput(0).dwType = INPUT_KEYBOARD
CopyMemory GInput(0).xi(0), KInput, Len(KInput)
'Release Return
KInput.wVk = vbKeyReturn
KInput.dwFlags = KEYEVENTF_KEYUP
GInput(1).dwType = INPUT_KEYBOARD
CopyMemory GInput(1).xi(0), KInput, Len(KInput)
'Now, finally send the keystrokes
SetForegroundWindow hwd
SendInput 2, GInput(0), Len(GInput(0))
End Sub
 
 
Public Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As
Boolean
    Dim sSave As String, Ret As Long
    Dim currWinP As WINDOWPLACEMENT
 
    Ret = GetWindowTextLength(hWnd)
    sSave = Space(Ret)
    GetWindowText hWnd, sSave, Ret + 1
 
  If InStr(sSave, Txsrc) > 0 Then
     HnSr = hWnd
 
    'prepare the WINDOWPLACEMENT type
      currWinP.Length = Len(currWinP)
 
      If GetWindowPlacement(hWnd, currWinP) > 0 Then
 
        'determine the window state
         If currWinP.showCmd = SW_SHOWMINIMIZED Then
 
           'minimized, so restore
            currWinP.Length = Len(currWinP)
            currWinP.flags = 0&
            currWinP.showCmd = SW_SHOWNORMAL
            Call SetWindowPlacement(hWnd, currWinP)
 
         Else
 
           'on screen, so assure visible
    retval& = SetForegroundWindow(HnSr)
 
         End If
 
      End If
 '   GetKeyboardState kbOld
    retval& = SetActiveWindow(HnSr)
    retval& = BringWindowToTop(HnSr)
    Sleep 250
Send_CtrlP (HnSr)
'    SendKeys "%F", True
'    Sleep 100
'    SendKeys "p", True
'    Sleep 100
'    SendKeys "p", True
'    Sleep 100
'    SendKeys "{ENTER}", True
    Sleep 100
'    SendKeys "{ENTER}", True
   h = FindWindow(vbNullString, "Stampa")
   Debug.Print h
Send_Enter (h)
    Txsrc = ""
    'SetKeyboardState kbOld
 '   TurnOn VK_CAPITAL
 '   TurnOn VK_NUMLOCK
 
    Exit Function
 
  End If
    'continue enumeration
    EnumWindowsProc = True
End Function
Sub TurnOn(vkKey As Long)
    'Get the keyboard state
    GetKeyboardState kbArray
    'Change a key
    kbArray.kbByte(vkKey) = 1
    'Set the keyboard state
    SetKeyboardState kbArray
End Sub
Sub Main()
 Txsrc = "eDrawings"
     'Get the current keyboardstate
    EnumWindows AddressOf EnumWindowsProc, ByVal 0&
  If Len(Txsrc) > 0 Then MsgBox Txsrc & " NOT FOUND", vbCritical
 
End Sub
--
ciao
  Stefano