当前位置:首页 » 程序代码 » 正文

用VB做一个输入法:发送字符到其它窗口 方法1

在做发送字符代码之前,先把让窗体无焦点的代码输入好:
Private Const GWL_STYLE = (-16)
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40
Private Const WS_EX_NOACTIVATE = &H8000000
Private Const GWL_EXSTYLE = (-20)
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Sub 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)

Private Sub Form_Load()
    Dim style     As Long
    style = GetWindowLong(Me.hwnd, GWL_EXSTYLE)
    style = style Or WS_EX_NOACTIVATE
    Call SetWindowLong(Me.hwnd, GWL_EXSTYLE, style)
    SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End Sub

然后,在窗口画一个标签(画标签,是因为标签控件没有焦点),标签的点击事件代码是:
Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    SendKeys "a"
End Sub

以上代码通过 SendKeys 方法来发送字符,当然你可以通过这种方式向别的程序的输入框发送字符串,如:
SendKeys "abc"
SendKeys "阿峰鼠标输入法"

你调用一个记事本来,编译好EXE后,运行上述程序,点击标签1后,你会发现,如果当时没有别的输入法,如中文输入法在调用,那么就可以成功发送字符到记事本里,如果有中文输入法,那么发送字符a或abc都不能成功。但是发送中文字符如“云峰工作室”是没有问题的,发送大写字母AB也是没有问题的。

用上述VB代码到字符发送到其它程序,其它窗口,只要有输入焦点的输入框中,都是没有问题,只要发送时,要关闭内置的中文输入法才好。(此文章来自wb86.com)

来源:济亨网

本文链接:http://wb98.com/post/39.html

    << 上一篇 下一篇 >>

    湘公网安备 43011102000514号 - 湘ICP备08100508号