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

来源:互联网 发布:地暖暖气片空调知乎 编辑:程序博客网 时间:2024/05/22 02:18
 

使用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: ValueMeaningMOUSEEVENTF_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. ValueMeaningKEYEVENTF_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.
作者Blog:http://blog.csdn.net/windcsn/
原创粉丝点击