① 在基于MFC中的对话框应用程序中,可以在 PreTranslateMessage 中获取(【主对话框】的或者是【CWinApp】的,应该说放在【CWinApp】中的PreTranslateMessage更好一些),如同下面:
BOOL CTestApp::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_LBUTTONDOWN)//左键按下
{
if(m_bSend)
{
CPoint pt;
GetCursorPos(&pt);
memset(szMsg,'\0',sizeof(szMsg));
sprintf(szMsg,"WM_LBD;%d;%d;%d;0;\0",pt.x, pt.y, keyFlags);
LanSend(szMsg, strlen(szMsg));
}
}
else if (pMsg->message == WM_LBUTTONUP)//左键抬起
{
if(m_bSend)
{
CPoint pt;
GetCursorPos(&pt);
memset(szMsg,'\0',sizeof(szMsg));
sprintf(szMsg,"WM_LBU;%d;%d;%d;0;\0",pt.x, pt.y, keyFlags);
LanSend(szMsg, strlen(szMsg));
}
}
else if (pMsg->message == WM_MOUSEMOVE) //光标移动
{
if(m_bSend)
{
POINT pt;
::GetCursorPos(&pt);
memset(szMsg,'\0',sizeof(szMsg));
//sprintf(szMsg,"WM_MM;%d;%d;%d;0;\0", GET_X_LPARAM(pMsg->lParam), GET_Y_LPARAM(pMsg->lParam), 0);
sprintf(szMsg,"WM_MM;%d;%d;%d;0;\0", pt.x, pt.y, 0);
LanSend(szMsg, strlen(szMsg));
}
}
else if (pMsg->message == WM_KEYDOWN) //键盘按下
{
if(m_bSend)
{
memset(szMsg,'\0',sizeof(szMsg));
sprintf(szMsg,"WM_KD;%d;%d;%d;%d;\0",pMsg->wParam, 0, 0, 0);
LanSend(szMsg, strlen(szMsg));
}
}
else if (pMsg->message == WM_KEYUP) //键盘抬起
{
if(m_bSend)
{
memset(szMsg,'\0',sizeof(szMsg));
sprintf(szMsg,"WM_KU;%d;%d;%d;%d;\0",pMsg->wParam, 0, 0, 0);
LanSend(szMsg, strlen(szMsg));
}
}
return CDialog::PreTranslateMessage(pMsg);
}
HRESULT ReadImmediateData( HWND hDlg )
{
HRESULT hr;
TCHAR strNewText[128] = TEXT(""); // Output string
DIMOUSESTATE2 dims2; // DirectInput mouse state structure
if( NULL == g_pMouse )
return S_OK;
// Get the input's device state, and put the state in dims
ZeroMemory( &dims2, sizeof(dims2) );
hr = g_pMouse->GetDeviceState( sizeof(DIMOUSESTATE2), &dims2 );
if( FAILED(hr) )
{
// DirectInput may be telling us that the input stream has been
// interrupted. We aren't tracking any state between polls, so
// we don't have any special reset that needs to be done.
// We just re-acquire and try again.
// If input is lost then acquire and keep trying
hr = g_pMouse->Acquire();
while( hr == DIERR_INPUTLOST )
hr = g_pMouse->Acquire();
// Update the dialog text
if( hr == DIERR_OTHERAPPHASPRIO ||
hr == DIERR_NOTACQUIRED )
SetDlgItemText( hDlg, IDC_DATA, TEXT("Unacquired") );
// hr may be DIERR_OTHERAPPHASPRIO or other errors. This
// may occur when the app is minimized or in the process of
// switching, so just try again later
return S_OK;
}
// The dims structure now has the state of the mouse, so
// display mouse coordinates (x, y, z) and buttons.
StringCchPrintf( strNewText, 128, TEXT("(X=% 3.3d, Y=% 3.3d, Z=% 3.3d) B0=%c B1=%c B2=%c B3=%c B4=%c B5=%c B6=%c B7=%c"),
dims2.lX, dims2.lY, dims2.lZ,
(dims2.rgbButtons[0] & 0x80) ? '1' : '0',
(dims2.rgbButtons[1] & 0x80) ? '1' : '0',
(dims2.rgbButtons[2] & 0x80) ? '1' : '0',
(dims2.rgbButtons[3] & 0x80) ? '1' : '0',
(dims2.rgbButtons[4] & 0x80) ? '1' : '0',
(dims2.rgbButtons[5] & 0x80) ? '1' : '0',
(dims2.rgbButtons[6] & 0x80) ? '1' : '0',
(dims2.rgbButtons[7] & 0x80) ? '1' : '0');
// Get the old text in the text box
TCHAR strOldText[128];
GetDlgItemText( hDlg, IDC_DATA, strOldText, 127 );
// If nothing changed then don't repaint - avoid flicker
if( 0 != lstrcmp( strOldText, strNewText ) )
SetDlgItemText( hDlg, IDC_DATA, strNewText );
//
// 仍然使用::GetCursorPos 发送鼠标的绝对位置
//
char szMsg[100] = {0};
POINT pt;
::GetCursorPos(&pt);
sprintf(szMsg,"WM_MM;%d;%d;%d;0;\0", pt.x, pt.y, 0);
LanSend(szMsg, strlen(szMsg));
//
// 发送鼠标的【按下】和【抬起】事件
// 因为driectInput里无法识别鼠标抬起事件,这里只能模拟抬起
//
if (dims2.rgbButtons[0] & 0x80)
{
GetCursorPos(&pt);
sprintf(szMsg,"WM_LBD;%d;%d;%d;0;\0",pt.x, pt.y, 0);
LanSend(szMsg, strlen(szMsg));
Sleep(2);
sprintf(szMsg,"WM_LBU;%d;%d;%d;0;\0",pt.x, pt.y, 0);
LanSend(szMsg, strlen(szMsg));
}
return S_OK;
}
//
// 全局变量和全局函数定义
//
HHOOK hhookMs = NULL;
LRESULT CALLBACK LowLevelMouseProc (INT nCode, WPARAM wParam, LPARAM lParam);
BOOL UninstallKbHook();
BOOL InstallKbHook();
//
// 安装鼠标Hook
//
void CTestMFCDlg::OnButton1()
{
InstallKbHook();
}
//
// 卸掉键盘Hook
//
void CTestMFCDlg::OnButton2()
{
UninstallKbHook();
}
LRESULT CALLBACK LowLevelMouseProc (INT nCode, WPARAM wParam, LPARAM lParam)
{
MSLLHOOKSTRUCT *pkbhs = (MSLLHOOKSTRUCT *)lParam;
char strMsg[100] = {0};
switch (nCode)
{
case HC_ACTION:
{
//鼠标移动
if (wParam == WM_MOUSEMOVE)
{
sprintf(strMsg, "WM_MOUSEMOVE: x= %d, y= %d\n", pkbhs->pt.x, pkbhs->pt.y);
OutputDebugString(strMsg);
}
//鼠标左击
if(wParam == WM_LBUTTONDOWN)
{
sprintf(strMsg, "WM_LBUTTONDOWN: x= %d, y= %d\n", pkbhs->pt.x, pkbhs->pt.y);
OutputDebugString(strMsg);
}
// //滚轮事件
// if (wParam == WM_MOUSEWHEEL)
// {
// sprintf(strMsg, "WM_MOUSEWHEEL: %d\n", HIWORD(pkbhs->mouseData));
// OutputDebugString(strMsg);
// }
}
default:
break;
}
return CallNextHookEx (NULL, nCode, wParam, lParam);
}
BOOL InstallKbHook( )
{
if (hhookMs )
UninstallKbHook();
hhookMs = SetWindowsHookEx(WH_MOUSE_LL,
(HOOKPROC)LowLevelMouseProc, AfxGetApp()->m_hInstance, NULL);
return(hhookMs != NULL);
}
BOOL UninstallKbHook()
{
BOOL fOk = FALSE;
if (hhookMs ) {
fOk = UnhookWindowsHookEx(hhookMs );
hhookMs = NULL;
}
return(fOk);
}