玩技术的,熟悉Windows编程的来看,帮帮我!

wumylove1234 2005-06-10 03:35:19
如何向TextBox控件用SendMessage发送Ctrl+Left消息?
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const VK_CONTROL = &H11
Private Const VK_LEFT = &H25

Private Sub Command1_Click()
Dim t As Long
SendMessage Me.Text1.hwnd, WM_KEYDOWN, VK_CONTROL, 0
t = t And &H40000001
SendMessage Me.Text1.hwnd, WM_KEYDOWN, VK_LEFT, t

End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyLeft Then
If Shift = 2 Then
MsgBox "Ctrl+Left"
End If
End If
End Sub

以前不好用.
MSDN中的消息说明:
The WM_KEYDOWN message is posted to the window with the keyboard focus when a nonsystem key is pressed. A nonsystem key is a key that is pressed when the alt key is not pressed.

WM_KEYDOWN
nVirtKey = (int) wParam; // virtual-key code
lKeyData = lParam; // key data

Parameters
nVirtKey
Value of wParam. Specifies the virtual-key code of the nonsystem key.
lKeyData
Value of lParam. Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table. Value Description
0–15 Specifies the repeat count for the current message. The value is the number of times the keystroke is auto-repeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.
16–23 Specifies the scan code. The value depends on the original equipment manufacturer (OEM).
24 Specifies whether the key is an extended key, such as the right-hand alt and ctrl keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
25–28 Reserved; do not use.
29 Specifies the context code. The value is always 0 for a WM_KEYDOWN message.
30 Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up.
31 Specifies the transition state. The value is always 0 for a WM_KEYDOWN message.

我想到的lParam的值为:
01000000 00000000 00000000 00000001
即&H40000001
不过好像不对,哪位大侠帮忙一下!!~~~~谢谢啦!
...全文
123 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
homezj 2005-06-11
  • 打赏
  • 举报
回复
呵呵,我写的不是原文,只是对其解释的理解,原文就在你那段原文的下面:

Remarks
Because of the autorepeat feature, more than one WM_KEYDOWN message may be posted before a WM_KEYUP message is posted. The previous key state (bit 30) can be used to determine whether the WM_KEYDOWN message indicates the first down transition or a repeated down transition.


wumylove1234 2005-06-11
  • 打赏
  • 举报
回复
小吉:

我看MSDN中对该位的解释是:
这一位的用处在按键自动重复时才有效,它是指当lParam低16位大于1时,对wParam中VK的每次重复,该位决定前一次是否已UP。


能不能把MSDN的原文告诉我?

同时谢谢 crycoming(瞎编),你的方法非常好用!

wumylove1234 2005-06-11
  • 打赏
  • 举报
回复
哦.那按照这种理解就明白了.原来针对的都是同一个键.

我同事用keybd_event这个发了模拟CTRL键也好用.呵呵.
homezj 2005-06-10
  • 打赏
  • 举报
回复
看楼主的意思是想让lParam第1、30位为1,可惜值算错了,应是&H20000001
30位为1是指前一键仍为按下状态,可惜这不会是前一次消息中的Ctrl键

我看MSDN中对该位的解释是:
这一位的用处在按键自动重复时才有效,它是指当lParam低16位大于1时,对wParam中VK的每次重复,该位决定前一次是否已UP。

说来说去,WM_KEYDOWN只能模似产生一个键的按下事件,VB中也可看出,你的代码产生了两次KeyDown事件,却不是组合键消息。键盘物理动作会产生键盘消息,但键盘消息并不会产生键盘物理动作。虽然前面发生了Ctrl按下事件,但并不代表Ctrl键按下了,后面的消息不会与其组合发生作用。

所以,我认为通过改变键盘状态,模似键盘物理动作,是产生组合键消息的一种办法,楼上的方法可以试试。
crycoming 2005-06-10
  • 打赏
  • 举报
回复
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const VK_CONTROL = &H11
Private Const VK_LEFT = &H25
Private Declare Function SetKeyboardState Lib "user32" (ByRef lppbKeyState As Byte) As Long



Private Sub Command1_Click()
Dim t As Long


Dim KeyStateArray(256) As Byte

KeyStateArray(VK_CONTROL) = &H80 '//使Ctrl键处于按下状态
SetKeyboardState KeyStateArray(0)

SendMessage Me.Text1.hwnd, WM_KEYDOWN, VK_LEFT, 0 ' //想窗口发送Ctrl + Enter 组合键消息

KeyStateArray(VK_LEFT) = &H0 '; //恢复原来的键盘缓冲区
SetKeyboardState KeyStateArray(0) ';


End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyLeft Then
If Shift = 2 Then
MsgBox "Ctrl+Left"
End If
End If
End Sub
liuyh7788 2005-06-10
  • 打赏
  • 举报
回复
我顶!我顶!我顶顶顶!!!!!!!!
wumylove1234 2005-06-10
  • 打赏
  • 举报
回复
顶都没有顶!!!

我好惨啊!:(

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧