VB2015 winform Sendinput鼠标点击Excel,无法正常工作

时间:2017-05-28 05:39:07

标签: excel visual-studio-2015 mouseevent sendinput

我订阅了受密码保护的Excel工作簿。工作簿必须每天运行。所以我有代码用密码打开工作簿,然后我想点击特定工作表上的两个按钮 因此,我正在研究如何让sendinput将鼠标点击发送到工作表。工作表打开和关闭,但未发送鼠标单击事件。我每天都有四张这样的床单,所以我只是打算将程序“clickOIS”调用几次。

这是我的代码(工作簿的密码已更改)

Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        clickOIS("SPX")

    End Sub

    Private Sub clickOIS(ByVal strOIS1 As String)
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim strany As String

        strany = "c:\eev\ois\" & strOIS1 & "\OIS Universal Filter.xlsb"

        xlApp = New Excel.Application
        xlApp.Visible = True
        xlApp.DisplayFullScreen = True



        xlWorkBook = xlApp.Workbooks.Open(Filename:=strany, Password:="xcsx")
        xlWorkSheet = xlWorkBook.Worksheets("OIS Score")
        xlWorkSheet.Activate()
        xlApp.WindowState = Excel.XlWindowState.xlMaximized
        xlApp.ActiveWindow.WindowState = Excel.XlWindowState.xlMaximized


        MouseMove1(795, 68)
        Application.DoEvents()
        MouseLeftClick()


        MouseMove1(825, 133)
        Application.DoEvents()
        MouseLeftClick()



        xlWorkBook.Save()
        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlWorkSheet)
        releaseObject(xlWorkBook)
        releaseObject(xlApp)

    End Sub



    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub


    Private Sub Button1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick
        If e.Button = System.Windows.Forms.MouseButtons.Left AndAlso e.Clicks = 2 Then
            'Left double click
        End If
    End Sub
    Public Shared Sub MouseMove1(ByVal x As Integer, ByVal y As Integer)
        Dim fx = x * (65535.0# / (GetSystemMetrics(SystemMetric.SM_CXSCREEN) - 1))
        Dim fy = y * (65535.0# / (GetSystemMetrics(SystemMetric.SM_CYSCREEN) - 1))
        Dim it As New INPUT
        it.dwType = INPUT_MOUSE
        it.mkhi.mi.dwFlags = MOUSEEVENTF_MOVE Or MOUSEEVENTF_ABSOLUTE
        it.mkhi.mi.dx = fx
        it.mkhi.mi.dy = fy
        SendInput(1, it, Marshal.SizeOf(it))
    End Sub

    Public Shared Sub MouseLeftClick()
        Dim it As New INPUT
        it.dwType = INPUT_MOUSE
        it.mkhi.mi.dwFlags = MOUSEEVENTF_LEFTDOWN
        SendInput(1, it, Marshal.SizeOf(it))
        Application.DoEvents()
        it = New INPUT
        it.dwType = INPUT_MOUSE
        it.mkhi.mi.dwFlags = MOUSEEVENTF_LEFTUP
        SendInput(1, it, Marshal.SizeOf(it))
    End Sub

    Private Structure INPUT
        Dim dwType As Integer
        Dim mkhi As MOUSEKEYBDHARDWAREINPUT
    End Structure

    <Runtime.InteropServices.StructLayout(LayoutKind.Explicit)>
    Private Structure MOUSEKEYBDHARDWAREINPUT
        <FieldOffset(0)> Public mi As MOUSEINPUT
        <FieldOffset(0)> Public ki As KEYBDINPUT
        <FieldOffset(0)> Public hi As HARDWAREINPUT
    End Structure

    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 Integer
    End Structure

    Private Structure KEYBDINPUT
        Public wVk As Short
        Public wScan As Short
        Public dwFlags As Integer
        Public time As Integer
        Public dwExtraInfo As Integer
    End Structure

    Private Structure HARDWAREINPUT
        Public uMsg As Integer
        Public wParamL As Short
        Public wParamH As Short
    End Structure

    Const INPUT_MOUSE As UInt32 = 0
    Const INPUT_KEYBOARD As Integer = 1
    Const INPUT_HARDWARE As Integer = 2
    Const XBUTTON1 As UInt32 = &H1
    Const XBUTTON2 As UInt32 = &H2
    Const MOUSEEVENTF_MOVE As UInt32 = &H1
    Const MOUSEEVENTF_LEFTDOWN As UInt32 = &H2
    Const MOUSEEVENTF_LEFTUP As UInt32 = &H4
    Const MOUSEEVENTF_RIGHTDOWN As UInt32 = &H8
    Const MOUSEEVENTF_RIGHTUP As UInt32 = &H10
    Const MOUSEEVENTF_MIDDLEDOWN As UInt32 = &H20
    Const MOUSEEVENTF_MIDDLEUP As UInt32 = &H40
    Const MOUSEEVENTF_XDOWN As UInt32 = &H80
    Const MOUSEEVENTF_XUP As UInt32 = &H100
    Const MOUSEEVENTF_WHEEL As UInt32 = &H800
    Const MOUSEEVENTF_VIRTUALDESK As UInt32 = &H4000
    Const MOUSEEVENTF_ABSOLUTE As UInt32 = &H8000

    <Runtime.InteropServices.DllImport("user32.dll")> Private Shared Function GetSystemMetrics(ByVal smIndex As Integer) As Integer
    End Function
    <Runtime.InteropServices.DllImport("user32.dll")> Private Shared Function SendInput(ByVal nInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer

    End Function

    Public Enum SystemMetric As Integer
        ''' <summary>
        '''  Width of the screen of the primary display monitor in pixels. This is the same values obtained by calling GetDeviceCaps as follows: GetDeviceCaps( hdcPrimaryMonitor HORZRES).
        ''' </summary>
        SM_CXSCREEN = 0
        ''' <summary>
        ''' Height of the screen of the primary display monitor in pixels. This is the same values obtained by calling GetDeviceCaps as follows: GetDeviceCaps( hdcPrimaryMonitor VERTRES).
        ''' </summary>
        SM_CYSCREEN = 1
    End Enum

End Class

0 个答案:

没有答案