VB [TUTORIAL]Lavorare in BackGround

Stato
Discussione chiusa ad ulteriori risposte.

T3CkToNiCk

Utente Diamond
21 Luglio 2010
2,914
330
898
1,926
Ultima modifica:
Lavorare in background,non è difficile (Fino a prima si,adesso che speedjack me lo ha spiegato no :\).

Allora creare un nuovo progetto,Windows Form e doppio click sulla Form.

Prima di Form1_Load inserite questo codice:

Codice:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String) As IntPtr
End Function

Adesso,sopra Public Class Form1 inserite:
Codice:
Imports System.Runtime.InteropServices

Inserite un Button ---> Doppio Click --->

Codice:
SendMessage(FindWindow(Nothing, "NomeDellaFinestra"), &H100, &H70, 15)

SendMessage = Manda un messaggio ben preciso,ad esempio quello di premere un tasto

NomeDellaFinestra = Il nome della finestra da trovare

&H75 = Scan code dei tasti { 70 = F1 }

Ad esempio se volete che venga premuto il tasto f2,mettete &H72

VK_LBUTTON0x01Left mouse button
VK_RBUTTON0x02Right mouse button
VK_CANCEL0x03Control-break processing
VK_MBUTTON0x04Middle mouse button (three-button mouse)
VK_XBUTTON10x05X1 mouse button
VK_XBUTTON20x06X2 mouse button
-0x07Undefined
VK_BACK0x08BACKSPACE key
VK_TAB0x09TAB key
-0x0A-0BReserved
VK_CLEAR0x0CCLEAR key
VK_RETURN0x0DENTER key
-0x0E-0FUndefined
VK_SHIFT0x10SHIFT key
VK_CONTROL0x11CTRL key
VK_MENU0x12ALT key
VK_PAUSE0x13PAUSE key
VK_CAPITAL0x14CAPS LOCK key
VK_KANA0x15IME Kana mode
VK_HANGUEL0x15IME Hanguel mode (maintained for compatibility; use VK_HANGUL)
VK_HANGUL0x15IME Hangul mode
-0x16Undefined
VK_JUNJA0x17IME Junja mode
VK_FINAL0x18IME final mode
VK_HANJA0x19IME Hanja mode
VK_KANJI0x19IME Kanji mode
-0x1AUndefined
VK_ESCAPE0x1BESC key
VK_CONVERT0x1CIME convert
VK_NONCONVERT0x1DIME nonconvert
VK_ACCEPT0x1EIME accept
VK_MODECHANGE0x1FIME mode change request
VK_SPACE0x20SPACEBAR
VK_PRIOR0x21PAGE UP key
VK_NEXT0x22PAGE DOWN key
VK_END0x23END key
VK_HOME0x24HOME key
VK_LEFT0x25LEFT ARROW key
VK_UP0x26UP ARROW key
VK_RIGHT0x27RIGHT ARROW key
VK_DOWN0x28DOWN ARROW key
VK_SELECT0x29SELECT key
VK_PRINT0x2APRINT key
VK_EXECUTE0x2BEXECUTE key
VK_SNAPSHOT0x2CPRINT SCREEN key
VK_INSERT0x2DINS key
VK_DELETE0x2EDEL key
VK_HELP0x2FHELP key
0x300 key
0x311 key
0x322 key
0x333 key
0x344 key
0x355 key
0x366 key
0x377 key
0x388 key
0x399 key
-0x3A-40Undefined
0x41A key
0x42B key
0x43C key
0x44D key
0x45E key
0x46F key
0x47G key
0x48H key
0x49I key
0x4AJ key
0x4BK key
0x4CL key
0x4DM key
0x4EN key
0x4FO key
0x50P key
0x51Q key
0x52R key
0x53S key
0x54T key
0x55U key
0x56V key
0x57W key
0x58X key
0x59Y key
0x5AZ key
VK_LWIN0x5BLeft Windows key (Natural keyboard)
VK_RWIN0x5CRight Windows key (Natural keyboard)
VK_APPS0x5DApplications key (Natural keyboard)
-0x5EReserved
VK_SLEEP0x5FComputer Sleep key
VK_NUMPAD00x60Numeric keypad 0 key
VK_NUMPAD10x61Numeric keypad 1 key
VK_NUMPAD20x62Numeric keypad 2 key
VK_NUMPAD30x63Numeric keypad 3 key
VK_NUMPAD40x64Numeric keypad 4 key
VK_NUMPAD50x65Numeric keypad 5 key
VK_NUMPAD60x66Numeric keypad 6 key
VK_NUMPAD70x67Numeric keypad 7 key
VK_NUMPAD80x68Numeric keypad 8 key
VK_NUMPAD90x69Numeric keypad 9 key
VK_MULTIPLY0x6AMultiply key
VK_ADD0x6BAdd key
VK_SEPARATOR0x6CSeparator key
VK_SUBTRACT0x6DSubtract key
VK_DECIMAL0x6EDecimal key
VK_DIVIDE0x6FDivide key
VK_F10x70F1 key
VK_F20x71F2 key
VK_F30x72F3 key
VK_F40x73F4 key
VK_F50x74F5 key
VK_F60x75F6 key
VK_F70x76F7 key
VK_F80x77F8 key
VK_F90x78F9 key
VK_F100x79F10 key
VK_F110x7AF11 key
VK_F120x7BF12 key
VK_F130x7CF13 key
VK_F140x7DF14 key
VK_F150x7EF15 key
VK_F160x7FF16 key
VK_F170x80F17 key
VK_F180x81F18 key
VK_F190x82F19 key
VK_F200x83F20 key
VK_F210x84F21 key
VK_F220x85F22 key
VK_F230x86F23 key
VK_F240x87F24 key
-0x88-8FUnassigned
VK_NUMLOCK0x90NUM LOCK key
VK_SCROLL0x91SCROLL LOCK key
0x92-96OEM specific
-0x97-9FUnassigned
VK_LSHIFT0xA0Left SHIFT key
VK_RSHIFT0xA1Right SHIFT key
VK_LCONTROL0xA2Left CONTROL key
VK_RCONTROL0xA3Right CONTROL key
VK_LMENU0xA4Left MENU key
VK_RMENU0xA5Right MENU key
VK_BROWSER_BACK0xA6Browser Back key
VK_BROWSER_FORWARD0xA7Browser Forward key
VK_BROWSER_REFRESH0xA8Browser Refresh key
VK_BROWSER_STOP0xA9Browser Stop key
VK_BROWSER_SEARCH0xAABrowser Search key
VK_BROWSER_FAVORITES0xABBrowser Favorites key
VK_BROWSER_HOME0xACBrowser Start and Home key
VK_VOLUME_MUTE0xADVolume Mute key
VK_VOLUME_DOWN0xAEVolume Down key
VK_VOLUME_UP0xAFVolume Up key
VK_MEDIA_NEXT_TRACK0xB0Next Track key
VK_MEDIA_PREV_TRACK0xB1Previous Track key
VK_MEDIA_STOP0xB2Stop Media key
VK_MEDIA_PLAY_PAUSE0xB3Play/Pause Media key
VK_LAUNCH_MAIL0xB4Start Mail key
VK_LAUNCH_MEDIA_SELECT0xB5Select Media key
VK_LAUNCH_APP10xB6Start Application 1 key
VK_LAUNCH_APP20xB7Start Application 2 key
-0xB8-B9Reserved
VK_OEM_10xBAUsed for miscellaneous characters; it can vary by keyboard.
For the US standard keyboard, the ';:' key
VK_OEM_PLUS0xBBFor any country/region, the '+' key
VK_OEM_COMMA0xBCFor any country/region, the ',' key
VK_OEM_MINUS0xBDFor any country/region, the '-' key
VK_OEM_PERIOD0xBEFor any country/region, the '.' key
VK_OEM_20xBFUsed for miscellaneous characters; it can vary by keyboard.
For the US standard keyboard, the '/?' key
VK_OEM_30xC0Used for miscellaneous characters; it can vary by keyboard.
For the US standard keyboard, the '`~' key
-0xC1-D7Reserved
-0xD8-DAUnassigned
VK_OEM_40xDBUsed for miscellaneous characters; it can vary by keyboard.
For the US standard keyboard, the '[{' key
VK_OEM_50xDCUsed for miscellaneous characters; it can vary by keyboard.
For the US standard keyboard, the '\|' key
VK_OEM_60xDDUsed for miscellaneous characters; it can vary by keyboard.
For the US standard keyboard, the ']}' key
VK_OEM_70xDEUsed for miscellaneous characters; it can vary by keyboard.
For the US standard keyboard, the 'single-quote/double-quote' key
VK_OEM_80xDFUsed for miscellaneous characters; it can vary by keyboard.
-0xE0Reserved
0xE1OEM specific
VK_OEM_1020xE2Either the angle bracket key or the backslash key on the RT 102-key keyboard
0xE3-E4OEM specific
VK_PROCESSKEY0xE5IME PROCESS key
0xE6OEM specific
VK_PACKET0xE7Used to pass Unicode characters as if they were keystrokes. The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, see Remark inKEYBDINPUT, SendInput, WM_KEYDOWN, and WM_KEYUP
-0xE8Unassigned
0xE9-F5OEM specific
VK_ATTN0xF6Attn key
VK_CRSEL0xF7CrSel key
VK_EXSEL0xF8ExSel key
VK_EREOF0xF9Erase EOF key
VK_PLAY0xFAPlay key
VK_ZOOM0xFBZoom key
VK_NONAME0xFCReserved
VK_PA10xFDPA1 key

15 = Numero di ripetizioni del tasto.

&H100 = WM_KEYDOWN

Ad esempio,se si vuole che in una finestra chiamata Prova venga premuto il bottone F1 il codice sarà simile a questo:

Codice:
Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindow( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String) As IntPtr
    End Function
     
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SendMessage(FindWindow(Nothing, "NomeDellaFinestra"), &H100, &H70, 15)
    End Sub
End Class

SpeedJack dice:

Come funziona il trattamento dei messaggi per le finestre sotto Windows (almeno per la pressione di un tasto):




quando premi enter, il sistema operativo fa questo:
1- invia il messaggio WM_KEYDOWN con lo scan-code (0x0D) del tasto enter
2- invia il messaggio WM_CHAR con il codice ASCII corrispondente a quel tasto (questo codice può variare da sistema a sistema, dipende da come è configurato...solitamente varia da nazione a nazione). per quanto riguarda enter, Windows invia due messaggi WM_CHAR: 1 con il codice ASCII del carriage-return, e dopo uno con il codice ASCII del line-feed
3- invia il messaggio WM_KEYUP con lo scan-code del tasto
 
ma che centra "Lavorare in background" con il codice?
Intende che non fa muovere il mouse per esempio per cliccare in un certo punto dello schermo per esempio, ma lo fa tramite codice e coordinate così l'utete che usail bot è libero di fare quello che vuole anche con altri programmi (esempio del bottone ma è lo stesso per i tasti)
Comunque ottima mi serviva!
 
  • Mi piace
Reazioni: TIH
ma che centra "Lavorare in background" con il codice?
Per i bot solitamente i gamers usano SendKeys.Send() oppure AutoIT (se non addirittura ACTool). Sostanzialmente con SendMessage non devono mantenere il focus sulla finestra di gioco.
Gli ho spiegato su Skype un paio di giorni fa l'uso di SendMessage (voleva premere il tasto Enter, ecco perché quell'esempio).
 
  • Mi piace
Reazioni: T3CkToNiCk
Per i bot solitamente i gamers usano SendKeys.Send() oppure AutoIT (se non addirittura ACTool). Sostanzialmente con SendMessage non devono mantenere il focus sulla finestra di gioco.
Gli ho spiegato su Skype un paio di giorni fa l'uso di SendMessage (voleva premere il tasto Enter, ecco perché quell'esempio).
E' quello che volevo dire :) così il giocatore in tanto che il bot funziona può andare su internet ecc ;) molto comodo :D
 
A me non fa niente :(
Il funzionamento del sistema SendMessage dipende molto da come il thread della finestra alla quale invii il messaggio gestisce i messaggi di Windows: può essere necessario inviare WM_CHAR o WM_KEYDOWN, oppure può anche essere necessaria l'intera sequenza di messaggi, ecc.
Un debugger aiuta molto nel capire quali input il thread si aspetta dall'utente.
 
  • Mi piace
Reazioni: T3CkToNiCk
Il funzionamento del sistema SendMessage dipende molto da come il thread della finestra alla quale invii il messaggio gestisce i messaggi di Windows: può essere necessario inviare WM_CHAR o WM_KEYDOWN, oppure può anche essere necessaria l'intera sequenza di messaggi, ecc.
Un debugger aiuta molto nel capire quali input il thread si aspetta dall'utente.
Io ho creato due windows form classici... nel ricevente diciamo ho creato nell'evento form1_keypress una condizione dove se si preme un tasto esce una msgbox. Col secondo form invio il messaggio col tasto da usare, ma non esce la msgbox... dovrebbe uscire così vero?
 
Ultima modifica:
Io ho creato due windows form classici... nel ricevente diciamo ho creato nell'evento form1_keypress una condizione dove se si preme un tasto esce una msgbox. Col secondo form invio il messaggio col tasto da usare, ma non esce la msgbox... dovrebbe uscire così vero?
Che messaggio hai inviato? WM_CHAR? Per l'evento KeyPress immagino ci sia bisogno di WM_KEYDOWN.

Anzi, pensandoci bene, credo che per KeyPress ci voglia l'intera sequenza. Per il solo WM_KEYDOWN, ci vuole l'evento KeyDown.
 
Stato
Discussione chiusa ad ulteriori risposte.