使用mouse_event来模拟一次选中文本框中的文本
2007-04-28 20:22
357 查看
使用mouse_event可以模拟一些通过鼠标执行的事情,下面我们就来模拟一次鼠标在EDIT中选择一段文本的过程。
首先我们来分解一下选中文本的鼠标动作,其包括基本方面:
将鼠标移动到指定的位置,文本的开始处
按下鼠标(WM_LBUTTONDOWN)
在按下鼠标的同时移动鼠标到指定的位置
松开鼠标左键(WM_LBUTTONUP)
知道了这个过程我们就来通过程序来模拟吧。我自己实验的时候建立了DIALOG工程,在DIALOG上放了一个BUTTON,一个EDIT,将程序写到BUTTON事件中,程序如下:
void CTestChkDlg: nBnClickedButton1()
{
RECT rect;
GetDlgItem(IDC_EDIT)->GetWindowRect(&rect);
SetCursorPos(rect.left+5,rect.top+5);
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,rect.left+1,rect.top+1,0,0);
SetCursorPos(rect.left+60,rect.top+10);
//mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE,500,600,0,0);
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,rect.left+60,rect.top+5,0,0);
}
首先我们需要做的是取得EDIT的位置,然后将鼠标移动到那里(SetCursorPos),这里加5是让鼠标在EDIT里面,因为EDIT有边,如果按照RECT的值来可能选择不了;然后使用MOUSEEVENTF_LEFTDOWN来按下鼠标,在按下鼠标的时候再将鼠标移动到文本的指定位置,然后松开鼠标。好了文本选中了。
鼠标模拟函数:
键盘模拟函数
首先我们来分解一下选中文本的鼠标动作,其包括基本方面:
将鼠标移动到指定的位置,文本的开始处
按下鼠标(WM_LBUTTONDOWN)
在按下鼠标的同时移动鼠标到指定的位置
松开鼠标左键(WM_LBUTTONUP)
知道了这个过程我们就来通过程序来模拟吧。我自己实验的时候建立了DIALOG工程,在DIALOG上放了一个BUTTON,一个EDIT,将程序写到BUTTON事件中,程序如下:
void CTestChkDlg: nBnClickedButton1()
{
RECT rect;
GetDlgItem(IDC_EDIT)->GetWindowRect(&rect);
SetCursorPos(rect.left+5,rect.top+5);
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,rect.left+1,rect.top+1,0,0);
SetCursorPos(rect.left+60,rect.top+10);
//mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE,500,600,0,0);
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,rect.left+60,rect.top+5,0,0);
}
首先我们需要做的是取得EDIT的位置,然后将鼠标移动到那里(SetCursorPos),这里加5是让鼠标在EDIT里面,因为EDIT有边,如果按照RECT的值来可能选择不了;然后使用MOUSEEVENTF_LEFTDOWN来按下鼠标,在按下鼠标的时候再将鼠标移动到文本的指定位置,然后松开鼠标。好了文本选中了。
鼠标模拟函数:
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 | 指定鼠标绝对坐标 |
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 |
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 |
相关文章推荐
- 使用mouse_event来模拟一次选中文本框中的文本
- 使用mouse_event来模拟一次选中文本框中的文本
- 使用mouse_event来模拟一次选中文本框中的文本
- 使用mouse_event来模拟一次选中文本框中的文本(转)
- 前端开发使用css禁止选中文本,模拟双击禁止选中文本,网页禁止选中文本
- 使用mouse_event模拟鼠标事件时,程序窗口失去焦点就失效
- keybd_event,mouse_event 模拟产生键盘事件、鼠标事件的使用
- [Struts2]s:combobox标签在使用出现问题:改变下拉列表的选中值,文本框的值不能随着被改变
- (总结)Swing组件的使用---文本框(JTextField)与文本区(JTextArea)
- 使用_TrackMouseEvent捕获WM_MOUSELEAVE和WM_MOUSEHOVER消息
- 使用keybd_event模拟键盘输入
- 使用TrackMouseEvent跟踪鼠标
- 使用基本字节输入流,字节输出流一次读写一个字节来复制文本 文件
- createTextRange()的使用(含文本框选中部分文字内容)
- Android 使用 sendevent 模拟鼠标和键盘事件
- 使用 Jquery-UI 实现一次拖拽多个选中的元素操作
- android模拟按键问题总结[使用IWindowManager.injectKeyEvent方法]
- android模拟按键问题总结[使用IWindowManager.injectKeyEvent方法]
- Android 使用 sendevent 模拟鼠标和键盘事件
- keybd_event和mouse_event完全彻底细致的模拟鼠标键盘所有动作