VB6:控制台获取鼠标位置读取输入缓冲区事件

时间:2015-09-29 20:26:15

标签: events input vb6 console buffer

任何人都可以帮我在VB6中编写从控制台窗口读取输入缓冲区事件的代码吗? 最重要的部分是单元格和行中的鼠标光标位置。

我在VB6中使用控制台作为主gui(或者我应该说cui)制作一个程序,我在vb代码中找不到使用“输入缓冲事件”的示例。

这个示例代码正是我想要的,但它是在c ++中我使用的是VB6:     https://msdn.microsoft.com/en-us/library/ms685035(v=vs.85).aspx

我在转换代码时遇到了很多麻烦,因为我对c ++知之甚少。 以下是一些其他链接已经让我知道如何开始:

这是我的模块,我的全部内容:

Private Const ENABLE_MOUSE_INPUT = &H10
Private Const ENABLE_WINDOW_INPUT = &H8
Private Const ENABLE_EXTENDED_FLAGS = &H80

Private Const STD_INPUT_HANDLE = -10&
Private Const STD_OUTPUT_HANDLE = -11&

Private Const MOUSE_MOVED = &H1

Private Type COORD
    x As Integer
    y As Integer
End Type

Private Type SMALL_RECT
    Left As Integer
    Top As Integer
    Right As Integer
    Bottom As Integer
End Type

Type INPUT_RECORD
    iEventType As Integer
    abData(0 To 15) As Byte
End Type

Private Type CONSOLE_SCREEN_BUFFER_INFO
    dwSize As COORD
    dwCursorPosition As COORD
    wAttributes As Integer
    srWindow As SMALL_RECT
    dwMaximumWindowSize As COORD
End Type

Type KEY_EVENT_RECORD
    lKeyDown As Long
    iRepeatCount As Integer
    iVirtualKeyCode As Integer
    iVirtualScanCode As Integer
    iChar As Integer
    lControlKeyState As Long
End Type

Type MOUSE_EVENT_RECORD
    CDMOUSEPOSITION As COORD
    lButtonState As Long
    lControlKeyState As Long
    lEventFlags As Long
End Type

Type FOCUS_EVENT_RECORD
    lSetFocus As Long
End Type

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDestination As Any, pSource As Any, ByVal lByteCount As Long)

Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long
Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
Private Declare Function SetConsoleMode Lib "kernel32" (ByVal hConsoleOutput As Long, dwMode As Long) As Long
Private Declare Function ReadConsoleInput Lib "kernel32" Alias "ReadConsoleInputA" (ByVal hConsoleInput As Long, lpBuffer As Any, ByVal nLength As Long, lpNumberOfEventsRead As Long) As Long

'##################################################################################################

Private Sub Main()
    AllocConsole

        Dim hConsoleIn As Long
        Dim hConsoleOut As Long

        Dim IR As INPUT_RECORD
        Dim MER As MOUSE_EVENT_RECORD
        Dim ret As Long

        hConsoleIn = GetStdHandle(STD_INPUT_HANDLE)
        hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)

        Call SetConsoleMode(hConsoleOut, ENABLE_WINDOW_INPUT Or ENABLE_MOUSE_INPUT Or ENABLE_EXTENDED_FLAGS)

        Dim x As Long
        Dim y As Long

        Do
            ReadConsoleInput hConsoleIn, MER, 1, ret

            Select Case IR.iEventType
                Case MOUSE_EVENT
                    Call CopyMemory(MER, IR.abData(0), Len(MER))
                    x = MER.CDMOUSEPOSITION.x
                    y = MER.CDMOUSEPOSITION.y

                    If MsgBox(x & "|" & y, vbOKCancel) = vbCancel Then Exit Do
            End Select

        Loop

    FreeConsole
End Sub

有人可以帮帮我吗? 亲切的问候!

0 个答案:

没有答案