Office中国论坛/Access中国论坛

标题: 如何获取当前焦点的控件的句柄 [打印本页]

作者: JosephTan    时间: 2012-1-7 13:09
标题: 如何获取当前焦点的控件的句柄
我想用user32的postmessage对一个combo box(之前已经把焦点设给这个combo box)模拟键盘输入一些数据形如:
PostMessage hwnd, WM_KEYDOWN, VK_END, MakeKeyLparam(VK_END, WM_KEYDOWN)
现在我就是没法找出这个combo box的句柄hwnd。求助!

作者: 风中漫步    时间: 2012-1-7 13:49
那个内部控件没有句柄
作者: JosephTan    时间: 2012-1-7 14:57
哦,原来没有句柄。那么有没有什么办法可以让combo box跳到我想给他赋值的那个位置?我原来是想模拟键盘输入的。现在不行了。
我在那个combo box的after update用了这么一句:DoCmd.GoToRecord acDataForm, "发票", acGoTo, Me.Combo51
这个只有在手动输入combo51的内容然后按enter才能跑到我想要的记录,我给combo51赋值的话是没法跳的,不知用什么办法赋值给combo box,然后触发after update事件。
作者: JosephTan    时间: 2012-1-7 20:23
啊,最后那个问题我很想得到答案。真的没有人屑于回答吗?最后顶一次。不然就结束了。
作者: 红尘如烟    时间: 2012-1-7 20:31
用API函数中的GotFocus就能获取焦点控件的句柄
作者: JosephTan    时间: 2012-1-8 05:17
gotfocus我用了,不行的。
Option Compare Database
Option Explicit

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
'Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_CHAR = &H102
Public Const VK_A = &H41
Public Const VK_RIGHT = &H27
Public Const VK_RETURN = &HD
Public Const VK_END = &H23
Public Const VK_0 = &H30
Public Const VK_1 = &H31
Public Const VK_2 = &H32
Public Const VK_3 = &H33
Public Const VK_4 = &H34
Public Const VK_5 = &H35
Public Const VK_6 = &H36
Public Const VK_7 = &H37
Public Const VK_8 = &H38
Public Const VK_9 = &H39

Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal Hwnd As Long, lpdwProcessId As Long) As Long


Public Function GetHwnd() As Long
'    Dim Hwnd As Long
'    Dim PID As Long
'    Dim TID As Long
'    Dim hWndFocus As Long
'
'    Hwnd = GetForegroundWindow
'
'    If Hwnd Then
'TID = GetWindowThreadProcessId(Hwnd, PID)
'AttachThreadInput App.ThreadID, TID, True
'
        GetHwnd = GetFocus
'AttachThreadInput App.ThreadID, TID, False
'    End If
'
End Function

Function MakeKeyLparam(ByVal VirtualKey As Long, ByVal flag As Long) As Long
'参数VirtualKey表示按键虚拟码,flag表示是按下键还是释放键,用WM_KEYDOWN和WM_KEYUP这两个常数表示
  Dim s As String
  Dim Firstbyte     As String           'lparam参数的24-31位
   If flag = WM_KEYDOWN Then             '如果是按下键
   Firstbyte = "00 "
   Else
   Firstbyte = "C0 "                   '如果是释放键
   End If
  Dim Scancode     As Long '获得键的扫描码
      Scancode = MapVirtualKey(VirtualKey, 0)
  Dim Secondbyte     As String         'lparam参数的16-23位,即虚拟键扫描码
      Secondbyte = Right("00 " & Hex(Scancode), 2)
      s = Firstbyte & Secondbyte & "0001 "                 '0001为lparam参数的0-15位,即发送次数和其它扩展信息
      MakeKeyLparam = Val("&H " & s)
End Function

作者: JosephTan    时间: 2012-1-8 12:39
想复杂了。用了个简单方法把所有的东西绕过了。原来有个Sendkeys的命令:
根本就不需要抓句柄。
SendKeys "^x", True
DoEvents
SendKeys "^v", True
'DoEvents
SendKeys "{ENTER}", True
就搞定了。




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3