The Windows interface program is popular with many users today. The operation of these programs is only two kinds, keyboard input control and mouse input control. So can we program to control the system's mouse, such as hiding the mouse in the computer? The answer to this question is yes. This is done primarily through a Windows API function. The following is an example of Visual C + + that describes how to implement this functionality. Readers need to pay attention to the code is a certain risk, the program after the operation you will not be able to control your computer with the mouse, this time need to the computer's current username to log off or restart the computer.
One, the realization method
Windows provides an API function mouse_event that simulates a mouse event, such as left-click, double-click, right-click, and so on. Using this function in a program will produce a very interesting effect, the simulated mouse event will mask the real mouse event, our example is to use this function to implement the mouse in the hidden program. The Mouse_event function prototype is as follows:
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
);
The first parameter represents the mouse message you want to simulate, and the left button is pressed for mouseeventf_leftdown, which means that the left button is released for Mouseeventf_leftup. In general, dwflags takes the following values:
Mouseeventf_move Move Mouse
Mouseeventf_leftdown simulate the left mouse button press
Mouseeventf_leftup simulate the left mouse button to lift
Mouseeventf_rightdown simulate the right mouse button press
Mouseeventf_rightup Simulate right mouse button lift
Mouseeventf_middledown Analog Mouse middle button press
Mouseeventf_middleup Analog Mouse middle button lift
Mouseeventf_absolute indicate whether absolute coordinates are used
The 23rd parameter represents the position coordinate x,y of the mouse, and specifies the absolute position or relative position of the x,y direction according to the Mouseeventf_absolute flag. The 45th parameter is not important and can generally be set to 0, 0.
The following is a sample code about Mouse_event, with two mouse_event (one drop at a time, one release at a time) to implement a mouse click simulation:
POINT lpPoint;
GetCursorPos(&lpPoint);
SetCursorPos(lpPoint.x, lpPoint.y);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
In order to continuously screen the mouse of the system, you need to start an auxiliary thread to simulate the action of the mouse, see the Code section for the specific implementation.
thread function;
CMainFrame::CMainFrame()
{
HWND hWnd=::GetParent(NULL);
GetCursorPos(mp);
AfxBeginThread(FMouse,hWnd,0); //启动模拟鼠标行为的线程;
}
UINT FMouse(LPVOID param)
{
WINDOWPLACEMENT wp;///窗口位置
wp.length=sizeof(WINDOWPLACEMENT);
HWND hWnd;
char tmp[20];
RECT rt;
int iResult;
iResult=AfxMessageBox("确实要隐藏吗?",MB_OKCANCEL);
if(iResult==IDOK)
{
while(1)
{
hWnd=GetForegroundWindow();//得到系统中最前端窗口的句柄;
GetWindowRect(hWnd,rt);
GetWindowText(hWnd,tmp,20);
GetWindowPlacement(hWnd,wp);//得到当前窗口的位置;
GetCursorPos(&cursorNew);//得到当前鼠标的位置;
while(1){
::mouse_event(MOUSEEVENTF_MOVE,cursorNew.x,cursorNew.y,0,0);
//模拟鼠标的移动;
}
}
}
return 0;//结束线程;
}
Iv. Summary
The above program describes the use of Windows API function Mouse_event () to simulate the mouse action, thereby hiding the mouse in the machine. In response, Windows also provides an analog keyboard API function keybd_event (), which can be used to screen the keyboard's actions accordingly. The keybd_event () function triggers a keystroke event, which means that a wm_keydown or WM_KEYUP message is generated back. The function prototype is as follows:
VOIDkeybd_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
);
From the above prototype can be seen, keybd_event () a total of four parameters, the first is the key to the virtual key value, such as the return key for Vk_return, tab to Vk_tab. The second parameter is scan code, generally do not need to set, with 0 instead on the line the third parameter is the option flag, if the KeyDown 0 can be, if the KEYUP is set to "Keyeventf_keyup", the fourth parameter is generally 0 can be. The virtual key value of ' a ' is 65, so you can use the following code to implement the simulation press ' a ' key,
keybd_event (65,0,0,0);
keybd_event (65,0,keyeventf_keyup,0);