使用mouse_event来模拟一次选中文本框中的文本(转)

 

使用mouse_event可以模拟一些通过鼠标执行的事情,下面我们就来模拟一次鼠标在EDIT中选择一段文本的过程。

首先我们来分解一下选中文本的鼠标动作,其包括基本方面:

  1. 将鼠标移动到指定的位置,文本的开始处
  2. 按下鼠标(WM_LBUTTONDOWN)
  3. 在按下鼠标的同时移动鼠标到指定的位置
  4. 松开鼠标左键(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:
ValueMeaning
MOUSEEVENTF_ABSOLUTE指定鼠标绝对坐标
MOUSEEVENTF_MOVESpecifies that movement occurred.
MOUSEEVENTF_LEFTDOWNSpecifies that the left button is down.
MOUSEEVENTF_LEFTUPSpecifies that the left button is up.
MOUSEEVENTF_RIGHTDOWNSpecifies that the right button is down.
MOUSEEVENTF_RIGHTUPSpecifies that the right button is up.
MOUSEEVENTF_MIDDLEDOWNSpecifies that the middle button is down.
MOUSEEVENTF_MIDDLEUPSpecifies that the middle button is up.
MOUSEEVENTF_WHEELWindows 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.
ValueMeaning
KEYEVENTF_EXTENDEDKEYIf specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).
KEYEVENTF_KEYUPIf specified, the key is being released. If not specified, the key is being depressed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值