【问题标题】:PostMessage send ctrl + key in backgroundPostMessage 在后台发送 ctrl + 键
【发布时间】:2020-09-01 14:09:31
【问题描述】:

我有这个代码,但我不知道为什么它不起作用,谁能帮助我?我需要在后台发送 ctrl + f 到另一个应用程序

Imports System.Runtime.InteropServices

Public Class Form2

    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Private Const WM_KEYDOWN = &H100
    Private Const WM_KEYUP = &H101
    Private Const WM_CHAR = &H102
    Private Const WM_SYSKEYDOWN = &H104
    Private Const WM_SYSKEYUP = &H105
    Private Const VK_SHIFT = &H10
    Private Const VK_CONTROL = &H11
    Private Const KEYEVENTF_EXTENDEDKEY = &H1
    Private Const KEYEVENTF_KEYUP = &H2
    Private Const KEYEVENTF_KEYDOWN = &H0
    Private Const VK_F = &H46
    Private Const WM_SETTEXT = &HC
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
    End Function
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

    Public Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Integer, ByVal wMapType As Integer) As Byte


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim hPadre As IntPtr = FindWindowEx(IntPtr.Zero, hPadre, "Notepad", "Sin título: Bloc de notas")
        Dim hCarpetas As IntPtr = FindWindowEx(hPadre, hCarpetas, "Edit", vbNullString)


        keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYDOWN, 0)
        PostMessage(hPadre, WM_KEYDOWN, VK_F, 0)
        keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0)
    End Sub

【问题讨论】:

    标签: vb.net background key postmessage ctrl


    【解决方案1】:

    我也有这个例子,但它不起作用:(

    导入 System.Runtime.InteropServices 公开课形式1

    Private Const VK_CONTROL As Integer = 17
    Private Const KEYEVENTF_KEYUP As Integer = &H2
    Private Const INPUT_MOUSE As Integer = 0
    Private Const INPUT_KEYBOARD As Integer = 1
    Private Const INPUT_HARDWARE As Integer = 2
    Private Const WM_KEYDOWN As Integer = &H100 
    
    Private Structure MOUSEINPUT
        Public dx As Integer
        Public dy As Integer
        Public mouseData As Integer
        Public dwFlags As Integer
        Public time As Integer
        Public dwExtraInfo As IntPtr
    End Structure
    
    Private Structure KEYBDINPUT
        Public wVk As Integer
        Public wScan As Integer
        Public dwFlags As Integer
        Public time As Integer
        Public dwExtraInfo As IntPtr
    End Structure
    
    Private Structure HARDWAREINPUT
        Public uMsg As Integer
        Public wParamL As Integer
        Public wParamH As Integer
    End Structure
    
    <StructLayout(LayoutKind.Explicit)>
    Private Structure INPUT
        <FieldOffset(0)>
        Public type As Integer
        <FieldOffset(4)>
        Public mi As MOUSEINPUT
        <FieldOffset(4)>
        Public ki As KEYBDINPUT
        <FieldOffset(4)>
        Public hi As HARDWAREINPUT
    End Structure
    
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
    End Function ' Dany
    
    Private Declare Function SendInput Lib "user32" (ByVal nInputs As Integer, ByVal pInputs() As INPUT, ByVal cbSize As Integer) As Integer
    
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As IntPtr, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr 
    
    
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Integer, ByVal bScan As Byte, ByVal dwFlags As IntPtr, ByVal dwExtraInfo As IntPtr) 
    
    
    Private Sub wait(ByVal interval As Integer)
        Dim sw As New Stopwatch
        sw.Start()
        Do While sw.ElapsedMilliseconds < interval
            ' Allows UI to remain responsive
            Application.DoEvents()
        Loop
        sw.Stop()
    
    End Sub
    Private Sub SendKey(ByVal bKey As Integer)
        Dim GInput(0) As INPUT
    
        ' press the key
        GInput(0).type = INPUT_KEYBOARD
        GInput(0).ki.wVk = bKey
        GInput(0).ki.dwFlags = 0
        SendInput(2, GInput, Marshal.SizeOf(GetType(INPUT)))
    End Sub
    
    Sub Press_Control()
        wait(2000)
        Dim b As Integer
        b = VK_CONTROL
        SendKey(b)
    End Sub
    
    
    
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim hPadre As IntPtr = FindWindowEx(IntPtr.Zero, hPadre, "Notepad", "Sin título: Bloc de notas")
        Dim hCarpetas As IntPtr = FindWindowEx(hPadre, hCarpetas, "Edit", vbNullString)
    
        Press_Control()
        SendMessage(hPadre, WM_KEYDOWN, 70, 0)
        wait(1000)
        keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 2015-10-18
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多