为什么我的鼠标取词程序只在VC窗口中有效?(附代码)
鼠标在vc的窗口中程序工作的很好,鼠标一离开vc窗口就无效了。
请大虾帮忙看看。
原码如下:
// Myhook.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "apihook32.h"
#include <stdlib.h>
#pragma data_seg("mydata")
HWND glhDisplayWnd1=NULL;//显示目标窗口标题编辑框的句柄
HWND glhDisplayWnd2=NULL;
HWND glhDisplayWnd3=NULL;
HHOOK glhHook=NULL;//安装的鼠标勾子句柄
HMODULE hModDLL;
APIHOOK32_ENTRY hkOutA,hkExtOutA,hkOutW,hkExtOutW;
#pragma data_seg()
#pragma comment(linker, "/section:mydata,rws")
extern "C" __declspec(dllexport) BOOL starthook(HWND hWnd1,HWND hWnd2,HWND hWnd3);
extern "C" __declspec(dllexport) BOOL stophook();
//////////////////////////////////////////////////
LRESULT WINAPI MouseProc(int nCode,WPARAM wParam,LPARAM lParam)
{
LPMOUSEHOOKSTRUCT pMouseHook=(MOUSEHOOKSTRUCT FAR *) lParam;
if (nCode<0)
{
return CallNextHookEx(glhHook,nCode,wParam,lParam);
}
if(wParam == WM_MOUSEMOVE)
{
POINT MousePoint;
RECT rect;
MousePoint=pMouseHook->pt;
HWND hwnd=WindowFromPoint(MousePoint);
ScreenToClient(hwnd,&MousePoint);
rect.left=MousePoint.x-100;
rect.top=MousePoint.y-3;
rect.right=MousePoint.x+100;
rect.bottom=MousePoint.y+3;
InvalidateRect(hwnd,&rect,FALSE);
}
return CallNextHookEx(glhHook,nCode,wParam,lParam);//继续传递消息
}
BOOL WINAPI MyTextOutA(HDC hdc, int nXStart, int nYStart, LPCSTR lpszString,int cbString)
{
//MessageBeep(0);
char szBuffer[1024];
POINT hdcPt2,tMouseP,PtExtent;
SIZE TextSize;
//BOOL bRet;
int x1 , x2 , y1 , y2 ;
GetDCOrgEx(hdc,&PtExtent);
hdcPt2.x = nXStart;
hdcPt2.y = nYStart;
LPtoDP(hdc,(POINT FAR *)&hdcPt2,1);
GetCursorPos(&tMouseP);
x1 = tMouseP.x ;
y1 = tMouseP.y ;
x2 = hdcPt2.x+ PtExtent.x;
y2 = hdcPt2.y+ PtExtent.y;
GetCurrentPositionEx(hdc, &PtExtent);
if ( GetTextAlign(hdc) & TA_UPDATECP )
{
GetCurrentPositionEx(hdc, &PtExtent);
x2 += PtExtent.x;
y2 += PtExtent.y;
}
GetTextExtentPoint(hdc, lpszString, cbString, &TextSize);
if((x1 >= x2)&&(x1<= x2 + TextSize.cx) &&
(y1 >= y2)&&(y1<= y2+ TextSize.cy))
{
::ZeroMemory((PBYTE)szBuffer, sizeof(szBuffer));
memcpy((PBYTE)szBuffer, (PBYTE)lpszString, cbString*sizeof(char));
::SendMessage(glhDisplayWnd2,WM_SETTEXT,0,(LPARAM)(LPCTSTR)szBuffer);
}
return TextOutA(hdc,nXStart,nYStart,lpszString,cbString);
}
BOOL WINAPI MyTextOutW(
HDC hdc, // handle to DC
int nXStart, // x-coordinate of starting position
int nYStart, // y-coordinate of starting position
LPWSTR lpString, // character string
int cbString // number of characters
)
{
bResult = ::TextOutW(
hdc, // handle to DC
nXStart, // x-coordinate of starting position
nYStart, // y-coordinate of starting position
lpString, // character string
cbString // number of characters
);
return bResult;
}
BOOL WINAPI MyExtTextOutA(HDC hdc, int nXStart, int nYStart, UINT fuOptions,
const RECT FAR *lprc, LPCSTR lpszString,
UINT cbString,int FAR *lpDx)
{
//MessageBeep(0);
char szBuffer[1024];
POINT hdcPt2,tMouseP,PtExtent;
SIZE TextSize;
//BOOL bRet;
int x1 , x2 , y1 , y2 ;
GetDCOrgEx(hdc,&PtExtent);
hdcPt2.x = nXStart;
hdcPt2.y = nYStart;
LPtoDP(hdc,(POINT FAR *)&hdcPt2,1);
GetCursorPos(&tMouseP);
x1 = tMouseP.x ;
y1 = tMouseP.y ;
x2 = hdcPt2.x+ PtExtent.x;
y2 = hdcPt2.y+ PtExtent.y;
GetCurrentPositionEx(hdc, &PtExtent);
if ( GetTextAlign(hdc) & TA_UPDATECP )
{
GetCurrentPositionEx(hdc, &PtExtent);
x2 += PtExtent.x;
y2 += PtExtent.y;
}
GetTextExtentPoint(hdc, lpszString, cbString, &TextSize);
if((x1 >= x2)&&(x1<= x2 + TextSize.cx) &&
(y1 >= y2)&&(y1<= y2+ TextSize.cy))
{
::ZeroMemory((PBYTE)szBuffer, sizeof(szBuffer));
memcpy((PBYTE)szBuffer, (PBYTE)lpszString, cbString*sizeof(char));
::SendMessage(glhDisplayWnd3,WM_SETTEXT,0,(LPARAM)(LPCTSTR)szBuffer);
}
return ExtTextOutA(hdc,nXStart,nYStart,fuOptions,lprc,lpszString,cbString,lpDx);
}
BOOL WINAPI MyExtTextOutW(HDC hdc, int nXStart, int nYStart, UINT fuOptions,
LPRECT lpRect, LPWSTR lpString,
UINT cbString, LPINT lpDx)
{
bResult = ::ExtTextOutW(hdc,nXStart,nYStart,fuOptions,
lpRect,lpString,cbString,lpDx);
return bResult;
}
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
hModDLL = (HMODULE)hModule;
hkOutA.hModCallerModule = NULL;
hkOutA.pszAPIName = "TextOutA";
hkOutA.pszCalleeModuleName = "gdi32.dll";
hkOutA.pfnDummyFuncAddress = (PROC) & MyTextOutA;
hkOutA.pfnOriginApiAddress = GetProcAddress(GetModuleHandle("gdi32.dll"),"TextOutA");
hkOutW.hModCallerModule = NULL;
hkOutW.pszAPIName = "TextOutW";
hkOutW.pszCalleeModuleName = "gdi32.dll";
hkOutW.pfnDummyFuncAddress = (PROC) & MyTextOutW;
hkOutW.pfnOriginApiAddress = GetProcAddress(GetModuleHandle("gdi32.dll"),"TextOutW");
hkExtOutA.hModCallerModule = NULL;
hkExtOutA.pszAPIName = "ExtTextOutA";
hkExtOutA.pszCalleeModuleName = "gdi32.dll";
hkExtOutA.pfnDummyFuncAddress = (PROC) & MyExtTextOutA;
hkExtOutA.pfnOriginApiAddress = GetProcAddress(GetModuleHandle("gdi32.dll"),"ExtTextOutA");
hkExtOutW.hModCallerModule = NULL;
hkExtOutW.pszAPIName = "ExtTextOutW";
hkExtOutW.pszCalleeModuleName = "gdi32.dll";
hkExtOutW.pfnDummyFuncAddress = (PROC) & MyExtTextOutW;
hkExtOutW.pfnOriginApiAddress = GetProcAddress(GetModuleHandle("gdi32.dll"),"ExtTextOutW");
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
SetWindowsAPIHook(&hkOutA);
SetWindowsAPIHook(&hkExtOutA);
SetWindowsAPIHook(&hkOutW);
SetWindowsAPIHook(&hkExtOutW);
return TRUE;
case DLL_PROCESS_DETACH:
UnhookWindowsAPIHooks(hkOutA);
UnhookWindowsAPIHooks(hkExtOutA);
UnhookWindowsAPIHooks(hkOutW);
UnhookWindowsAPIHooks(hkExtOutW);
}
return TRUE;
}
BOOL starthook(HWND hWnd1,HWND hWnd2,HWND hWnd3)
{
//安装钩子并设定接收显示窗口句柄
BOOL bResult=FALSE;
glhHook=SetWindowsHookEx(WH_MOUSE,MouseProc,hModDLL,0);
if(glhHook!=NULL)
bResult=TRUE;
glhDisplayWnd1=hWnd1;
glhDisplayWnd2=hWnd2;
glhDisplayWnd3=hWnd3;
//设置显示目标窗口标题编辑框的句柄
return bResult;
}
BOOL stophook()
{
//卸载钩子
BOOL bResult=FALSE;
if(glhHook)
{
bResult= UnhookWindowsHookEx(glhHook);
if(bResult)
{
glhDisplayWnd1=NULL;
glhDisplayWnd2=NULL;
glhDisplayWnd3=NULL;
glhHook=NULL;
}
}
return bResult;
}