还有向这个程序的一个文本框发键盘消息,能模拟字符串的输入,清空,和退格
问题全部解决,另开贴加分
8 个解决方案
#1
调用mouse_event()
#2
键盘的调用keybd_event()
#3
//模拟鼠标按下,然后松开
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
//模拟按键
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),0,0);
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),KEYEVENTF_KEYUP,0);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
//模拟按键
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),0,0);
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),KEYEVENTF_KEYUP,0);
#4
hyamw(林锋) 你写的这两个我大概知道,但不用获取窗口的句柄吗?我使用一个程序控制另一个程序,这个可以做到吗?那么如果想你说的可以,请把你写的两种用法大概说明一下,每输入一个字符,这两个东西要配套出现吗?
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),0,0);
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),KEYEVENTF_KEYUP,0);
还有具体是什么意思,你帮我节省时间,我给你高分
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),0,0);
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),KEYEVENTF_KEYUP,0);
还有具体是什么意思,你帮我节省时间,我给你高分
#5
如果当前被激活的窗口就是要控制的窗口,就可以用这两函数实现。
如果不是,就需要用PostMessage()或者SendMessage()来实现了
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),0,0);
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),KEYEVENTF_KEYUP,0);
这两个函数最好是同时出现。前面一个是模拟按下'+'键,后一个是模拟松开。
//keybd_event和mouse_event这两个函数在msdn里面有详细的解释
The keybd_event function synthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message. The keyboard driver's interrupt handler calls the keybd_event function.
Windows NT: This function has been superseded. Use SendInput instead.
VOID keybd_event(
BYTE bVk, // virtual-key code
BYTE bScan, // hardware scan code
DWORD dwFlags, // flags specifying various function options
DWORD dwExtraInfo // additional data associated with keystroke
);
Parameters
bVk
Specifies a virtual-key code. The code must be a value in the range 1 to 254.
bScan
Specifies a hardware scan code for the key.
dwFlags
A set of flag bits that specify various aspects of function operation. An application can use any combination of the following predefined constant values to set the flags. Value Meaning
KEYEVENTF_EXTENDEDKEY If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).
KEYEVENTF_KEYUP If specified, the key is being released. If not specified, the key is being depressed.
dwExtraInfo
Specifies an additional 32-bit value associated with the key stroke.
Return Values
This function has no return value.
The mouse_event function synthesizes mouse motion and button clicks.
Windows NT: This function has been superseded. Use SendInput instead.
VOID mouse_event(
DWORD dwFlags, // flags specifying various motion/click variants
DWORD dx, // horizontal mouse position or position change
DWORD dy, // vertical mouse position or position change
DWORD dwData, // amount of wheel movement
DWORD dwExtraInfo
// 32 bits of application-defined information
);
Parameters
dwFlags
A set of flag bits that specify various aspects of mouse motion and button clicking. The bits in this parameter can be any reasonable combination of the following values: Value Meaning
MOUSEEVENTF_ABSOLUTE Specifies that the dx and dy parameters contain normalized absolute coordinates. If not set, those parameters contain relative data: the change in position since the last reported position. This flag can be set, or not set, regardless of what kind of mouse or mouse-like device, if any, is connected to the system. For further information about relative mouse motion, see the following Remarks section.
MOUSEEVENTF_MOVE Specifies that movement occurred.
MOUSEEVENTF_LEFTDOWN Specifies that the left button is down.
MOUSEEVENTF_LEFTUP Specifies that the left button is up.
MOUSEEVENTF_RIGHTDOWN Specifies that the right button is down.
MOUSEEVENTF_RIGHTUP Specifies that the right button is up.
MOUSEEVENTF_MIDDLEDOWN Specifies that the middle button is down.
MOUSEEVENTF_MIDDLEUP Specifies that the middle button is up.
MOUSEEVENTF_WHEEL Windows NT: Specifies that the wheel has been moved, if the mouse has a wheel. The amount of movement is given in dwData
dx
Specifies the mouse's absolute position along the x-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual x-coordinate; relative data is given as the number of mickeys moved. A mickey is the amount that a mouse has to move for it to report that it has moved.
dy
Specifies the mouse's absolute position along the y-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual y-coordinate; relative data is given as the number of mickeys moved.
dwData
If dwFlags is MOUSEEVENTF_WHEEL, then dwData specifies the amount of wheel movement. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user. One wheel click is defined as WHEEL_DELTA, which is 120.
If dwFlags is not MOUSEEVENTF_WHEEL, then dwData should be zero.
dwExtraInfo
Specifies an additional 32-bit value associated with the mouse event. An application calls GetMessageExtraInfo to obtain this extra information.
Return Values
This function has no return value.
如果不是,就需要用PostMessage()或者SendMessage()来实现了
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),0,0);
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),KEYEVENTF_KEYUP,0);
这两个函数最好是同时出现。前面一个是模拟按下'+'键,后一个是模拟松开。
//keybd_event和mouse_event这两个函数在msdn里面有详细的解释
The keybd_event function synthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message. The keyboard driver's interrupt handler calls the keybd_event function.
Windows NT: This function has been superseded. Use SendInput instead.
VOID keybd_event(
BYTE bVk, // virtual-key code
BYTE bScan, // hardware scan code
DWORD dwFlags, // flags specifying various function options
DWORD dwExtraInfo // additional data associated with keystroke
);
Parameters
bVk
Specifies a virtual-key code. The code must be a value in the range 1 to 254.
bScan
Specifies a hardware scan code for the key.
dwFlags
A set of flag bits that specify various aspects of function operation. An application can use any combination of the following predefined constant values to set the flags. Value Meaning
KEYEVENTF_EXTENDEDKEY If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).
KEYEVENTF_KEYUP If specified, the key is being released. If not specified, the key is being depressed.
dwExtraInfo
Specifies an additional 32-bit value associated with the key stroke.
Return Values
This function has no return value.
The mouse_event function synthesizes mouse motion and button clicks.
Windows NT: This function has been superseded. Use SendInput instead.
VOID mouse_event(
DWORD dwFlags, // flags specifying various motion/click variants
DWORD dx, // horizontal mouse position or position change
DWORD dy, // vertical mouse position or position change
DWORD dwData, // amount of wheel movement
DWORD dwExtraInfo
// 32 bits of application-defined information
);
Parameters
dwFlags
A set of flag bits that specify various aspects of mouse motion and button clicking. The bits in this parameter can be any reasonable combination of the following values: Value Meaning
MOUSEEVENTF_ABSOLUTE Specifies that the dx and dy parameters contain normalized absolute coordinates. If not set, those parameters contain relative data: the change in position since the last reported position. This flag can be set, or not set, regardless of what kind of mouse or mouse-like device, if any, is connected to the system. For further information about relative mouse motion, see the following Remarks section.
MOUSEEVENTF_MOVE Specifies that movement occurred.
MOUSEEVENTF_LEFTDOWN Specifies that the left button is down.
MOUSEEVENTF_LEFTUP Specifies that the left button is up.
MOUSEEVENTF_RIGHTDOWN Specifies that the right button is down.
MOUSEEVENTF_RIGHTUP Specifies that the right button is up.
MOUSEEVENTF_MIDDLEDOWN Specifies that the middle button is down.
MOUSEEVENTF_MIDDLEUP Specifies that the middle button is up.
MOUSEEVENTF_WHEEL Windows NT: Specifies that the wheel has been moved, if the mouse has a wheel. The amount of movement is given in dwData
dx
Specifies the mouse's absolute position along the x-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual x-coordinate; relative data is given as the number of mickeys moved. A mickey is the amount that a mouse has to move for it to report that it has moved.
dy
Specifies the mouse's absolute position along the y-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual y-coordinate; relative data is given as the number of mickeys moved.
dwData
If dwFlags is MOUSEEVENTF_WHEEL, then dwData specifies the amount of wheel movement. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user. One wheel click is defined as WHEEL_DELTA, which is 120.
If dwFlags is not MOUSEEVENTF_WHEEL, then dwData should be zero.
dwExtraInfo
Specifies an additional 32-bit value associated with the mouse event. An application calls GetMessageExtraInfo to obtain this extra information.
Return Values
This function has no return value.
#6
至于说如何让被控制的程序被激活,而且让自己的程序能发送模拟的按键和鼠标消息。你可以使用注册热键或者用键盘钩子的方法实现。
注册热键:RegisterHotKey
注册钩子:SetWindowsHookEx
如果用SendMessage或者PostMessage就不需要这些东西,直接获取被控制窗口的句柄,然后发送相应的消息就行了。
注册热键:RegisterHotKey
注册钩子:SetWindowsHookEx
如果用SendMessage或者PostMessage就不需要这些东西,直接获取被控制窗口的句柄,然后发送相应的消息就行了。
#7
FindWindow获得窗口句柄
#8
谢谢了,不过对于我目前的东西没什么作用
#1
调用mouse_event()
#2
键盘的调用keybd_event()
#3
//模拟鼠标按下,然后松开
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
//模拟按键
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),0,0);
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),KEYEVENTF_KEYUP,0);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
//模拟按键
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),0,0);
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),KEYEVENTF_KEYUP,0);
#4
hyamw(林锋) 你写的这两个我大概知道,但不用获取窗口的句柄吗?我使用一个程序控制另一个程序,这个可以做到吗?那么如果想你说的可以,请把你写的两种用法大概说明一下,每输入一个字符,这两个东西要配套出现吗?
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),0,0);
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),KEYEVENTF_KEYUP,0);
还有具体是什么意思,你帮我节省时间,我给你高分
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),0,0);
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),KEYEVENTF_KEYUP,0);
还有具体是什么意思,你帮我节省时间,我给你高分
#5
如果当前被激活的窗口就是要控制的窗口,就可以用这两函数实现。
如果不是,就需要用PostMessage()或者SendMessage()来实现了
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),0,0);
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),KEYEVENTF_KEYUP,0);
这两个函数最好是同时出现。前面一个是模拟按下'+'键,后一个是模拟松开。
//keybd_event和mouse_event这两个函数在msdn里面有详细的解释
The keybd_event function synthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message. The keyboard driver's interrupt handler calls the keybd_event function.
Windows NT: This function has been superseded. Use SendInput instead.
VOID keybd_event(
BYTE bVk, // virtual-key code
BYTE bScan, // hardware scan code
DWORD dwFlags, // flags specifying various function options
DWORD dwExtraInfo // additional data associated with keystroke
);
Parameters
bVk
Specifies a virtual-key code. The code must be a value in the range 1 to 254.
bScan
Specifies a hardware scan code for the key.
dwFlags
A set of flag bits that specify various aspects of function operation. An application can use any combination of the following predefined constant values to set the flags. Value Meaning
KEYEVENTF_EXTENDEDKEY If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).
KEYEVENTF_KEYUP If specified, the key is being released. If not specified, the key is being depressed.
dwExtraInfo
Specifies an additional 32-bit value associated with the key stroke.
Return Values
This function has no return value.
The mouse_event function synthesizes mouse motion and button clicks.
Windows NT: This function has been superseded. Use SendInput instead.
VOID mouse_event(
DWORD dwFlags, // flags specifying various motion/click variants
DWORD dx, // horizontal mouse position or position change
DWORD dy, // vertical mouse position or position change
DWORD dwData, // amount of wheel movement
DWORD dwExtraInfo
// 32 bits of application-defined information
);
Parameters
dwFlags
A set of flag bits that specify various aspects of mouse motion and button clicking. The bits in this parameter can be any reasonable combination of the following values: Value Meaning
MOUSEEVENTF_ABSOLUTE Specifies that the dx and dy parameters contain normalized absolute coordinates. If not set, those parameters contain relative data: the change in position since the last reported position. This flag can be set, or not set, regardless of what kind of mouse or mouse-like device, if any, is connected to the system. For further information about relative mouse motion, see the following Remarks section.
MOUSEEVENTF_MOVE Specifies that movement occurred.
MOUSEEVENTF_LEFTDOWN Specifies that the left button is down.
MOUSEEVENTF_LEFTUP Specifies that the left button is up.
MOUSEEVENTF_RIGHTDOWN Specifies that the right button is down.
MOUSEEVENTF_RIGHTUP Specifies that the right button is up.
MOUSEEVENTF_MIDDLEDOWN Specifies that the middle button is down.
MOUSEEVENTF_MIDDLEUP Specifies that the middle button is up.
MOUSEEVENTF_WHEEL Windows NT: Specifies that the wheel has been moved, if the mouse has a wheel. The amount of movement is given in dwData
dx
Specifies the mouse's absolute position along the x-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual x-coordinate; relative data is given as the number of mickeys moved. A mickey is the amount that a mouse has to move for it to report that it has moved.
dy
Specifies the mouse's absolute position along the y-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual y-coordinate; relative data is given as the number of mickeys moved.
dwData
If dwFlags is MOUSEEVENTF_WHEEL, then dwData specifies the amount of wheel movement. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user. One wheel click is defined as WHEEL_DELTA, which is 120.
If dwFlags is not MOUSEEVENTF_WHEEL, then dwData should be zero.
dwExtraInfo
Specifies an additional 32-bit value associated with the mouse event. An application calls GetMessageExtraInfo to obtain this extra information.
Return Values
This function has no return value.
如果不是,就需要用PostMessage()或者SendMessage()来实现了
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),0,0);
keybd_event(VK_ADD,MapVirtualKey(VK_ADD,2),KEYEVENTF_KEYUP,0);
这两个函数最好是同时出现。前面一个是模拟按下'+'键,后一个是模拟松开。
//keybd_event和mouse_event这两个函数在msdn里面有详细的解释
The keybd_event function synthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message. The keyboard driver's interrupt handler calls the keybd_event function.
Windows NT: This function has been superseded. Use SendInput instead.
VOID keybd_event(
BYTE bVk, // virtual-key code
BYTE bScan, // hardware scan code
DWORD dwFlags, // flags specifying various function options
DWORD dwExtraInfo // additional data associated with keystroke
);
Parameters
bVk
Specifies a virtual-key code. The code must be a value in the range 1 to 254.
bScan
Specifies a hardware scan code for the key.
dwFlags
A set of flag bits that specify various aspects of function operation. An application can use any combination of the following predefined constant values to set the flags. Value Meaning
KEYEVENTF_EXTENDEDKEY If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).
KEYEVENTF_KEYUP If specified, the key is being released. If not specified, the key is being depressed.
dwExtraInfo
Specifies an additional 32-bit value associated with the key stroke.
Return Values
This function has no return value.
The mouse_event function synthesizes mouse motion and button clicks.
Windows NT: This function has been superseded. Use SendInput instead.
VOID mouse_event(
DWORD dwFlags, // flags specifying various motion/click variants
DWORD dx, // horizontal mouse position or position change
DWORD dy, // vertical mouse position or position change
DWORD dwData, // amount of wheel movement
DWORD dwExtraInfo
// 32 bits of application-defined information
);
Parameters
dwFlags
A set of flag bits that specify various aspects of mouse motion and button clicking. The bits in this parameter can be any reasonable combination of the following values: Value Meaning
MOUSEEVENTF_ABSOLUTE Specifies that the dx and dy parameters contain normalized absolute coordinates. If not set, those parameters contain relative data: the change in position since the last reported position. This flag can be set, or not set, regardless of what kind of mouse or mouse-like device, if any, is connected to the system. For further information about relative mouse motion, see the following Remarks section.
MOUSEEVENTF_MOVE Specifies that movement occurred.
MOUSEEVENTF_LEFTDOWN Specifies that the left button is down.
MOUSEEVENTF_LEFTUP Specifies that the left button is up.
MOUSEEVENTF_RIGHTDOWN Specifies that the right button is down.
MOUSEEVENTF_RIGHTUP Specifies that the right button is up.
MOUSEEVENTF_MIDDLEDOWN Specifies that the middle button is down.
MOUSEEVENTF_MIDDLEUP Specifies that the middle button is up.
MOUSEEVENTF_WHEEL Windows NT: Specifies that the wheel has been moved, if the mouse has a wheel. The amount of movement is given in dwData
dx
Specifies the mouse's absolute position along the x-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual x-coordinate; relative data is given as the number of mickeys moved. A mickey is the amount that a mouse has to move for it to report that it has moved.
dy
Specifies the mouse's absolute position along the y-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual y-coordinate; relative data is given as the number of mickeys moved.
dwData
If dwFlags is MOUSEEVENTF_WHEEL, then dwData specifies the amount of wheel movement. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user. One wheel click is defined as WHEEL_DELTA, which is 120.
If dwFlags is not MOUSEEVENTF_WHEEL, then dwData should be zero.
dwExtraInfo
Specifies an additional 32-bit value associated with the mouse event. An application calls GetMessageExtraInfo to obtain this extra information.
Return Values
This function has no return value.
#6
至于说如何让被控制的程序被激活,而且让自己的程序能发送模拟的按键和鼠标消息。你可以使用注册热键或者用键盘钩子的方法实现。
注册热键:RegisterHotKey
注册钩子:SetWindowsHookEx
如果用SendMessage或者PostMessage就不需要这些东西,直接获取被控制窗口的句柄,然后发送相应的消息就行了。
注册热键:RegisterHotKey
注册钩子:SetWindowsHookEx
如果用SendMessage或者PostMessage就不需要这些东西,直接获取被控制窗口的句柄,然后发送相应的消息就行了。
#7
FindWindow获得窗口句柄
#8
谢谢了,不过对于我目前的东西没什么作用