using mouse hook to get selected text
Hi all,
Please I want a help
I used the mouse hook and I want to get the selected text in a windowand the title of the window.
can anyone help me how to do this ? What API's can help me and get the text ?
I using c++ languge and I use This code to get HookDLL
Code:
===============================
#pragma data_seg(".Segment")
HWND hWndServer = NULL;
UINT UWM_MOUSEMOVE;
UINT UWM_MOUSELBUTTONUP;
UINT UWM_MOUSELBUTTONDOWN;
UINT UWM_MOUSERBUTTONUP;
UINT UWM_MOUSERBUTTONDOWN;
UINT UWM_MOUSELDBCLICK;
#pragma data_seg()
#pragma comment(linker, "/section:.Segment,rws")
HINSTANCE hInst;
//HWND hWndServer = NULL;
HHOOK hook;
static LRESULT CALLBACK MouseMsgProc(UINT nCode, WPARAM wParam, LPARAM lParam);
BOOL APIENTRY DllMain( HINSTANCE hModule,
DWORD dwReason,
LPVOID lpReserved )
{
switch( dwReason )
{
case DLL_PROCESS_ATTACH:
hInst = hModule;
UWM_MOUSEMOVE = ::RegisterWindowMessage(UWM_MOUSEMOVE_MSG);
UWM_MOUSELBUTTONUP = ::RegisterWindowMessage(UWM_MOUSELBUTTONUP_MSG);
UWM_MOUSELBUTTONDOWN = ::RegisterWindowMessage(UWM_MOUSELBUTTONDOWN_MSG);
UWM_MOUSERBUTTONUP = ::RegisterWindowMessage(UWM_MOUSERBUTTONUP_MSG);
UWM_MOUSERBUTTONDOWN = ::RegisterWindowMessage(UWM_MOUSERBUTTONDOWN_MSG);
UWM_MOUSELDBCLICK = ::RegisterWindowMessage(UWM_MOUSELDBCLICK_MSG);
break;
default:
break;
}
return TRUE;
}
__declspec(dllexport) BOOL InstallHook( HWND hWndParent )
{
if(hWndServer != NULL)
return FALSE; // already hooked!
hook = SetWindowsHookEx( WH_GETMESSAGE, (HOOKPROC)MouseMsgProc,
hInst, 0);
if(hook != NULL)
{
hWndServer = hWndParent;
return TRUE;
}
return FALSE;
}
__declspec(dllexport) BOOL UnInstallHook( HWND hWndParent )
{
if(hWndParent != hWndServer || hWndParent == NULL)
return FALSE;
BOOL unhooked = UnhookWindowsHookEx(hook);
if(unhooked)
hWndServer = NULL;
return unhooked;
return TRUE;
}
static LRESULT CALLBACK MouseMsgProc(UINT nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode < 0)
{
CallNextHookEx(hook, nCode, wParam, lParam);
return 0;
}
LPMSG msg = (LPMSG)lParam;
switch( msg->message )
{
case WM_LBUTTONDBLCLK:
SendMessage( hWndServer, UWM_MOUSELDBCLICK, 0 , 0);
break;
case WM_MOUSEMOVE:
SendMessage( hWndServer, UWM_MOUSEMOVE, 0, 0);
break;
case WM_NCMOUSEMOVE:
SendMessage( hWndServer, UWM_MOUSEMOVE, 0, 0);
break;
case WM_LBUTTONDOWN:
SendMessage( hWndServer, UWM_MOUSELBUTTONDOWN, 0 , 0 );
break;
case WM_NCLBUTTONDOWN:
SendMessage( hWndServer, UWM_MOUSELBUTTONDOWN, 0 , 0);
break;
case WM_LBUTTONUP:
SendMessage( hWndServer, UWM_MOUSELBUTTONUP, 0 , 0 );
break;
case WM_NCLBUTTONUP:
SendMessage( hWndServer, UWM_MOUSELBUTTONUP, 0 , 0);
break;
case WM_RBUTTONDOWN:
SendMessage( hWndServer, UWM_MOUSERBUTTONDOWN, 0 , 0 );
break;
case WM_NCRBUTTONDOWN:
SendMessage( hWndServer, UWM_MOUSERBUTTONDOWN, 0 , 0);
break;
case WM_RBUTTONUP:
SendMessage( hWndServer, UWM_MOUSERBUTTONUP, 0 , 0 );
break;
case WM_NCRBUTTONUP:
SendMessage( hWndServer, UWM_MOUSERBUTTONUP, 0 , 0);
break;
default:
break;
}
return CallNextHookEx(hook, nCode, wParam, lParam);
}
============================
can you help me
Thanks :)
Re: using mouse hook to get selected text
For you it would be more helpful if you explain your immediate problem you want to solve, and move forward step by step. Your ultimate task can wait so far. :)
Re: using mouse hook to get selected text
OK
I'm trying to do this function
========================================================
// Mouse Handling Function
LRESULT CRecordPlayerDlg::OnMyMouseMove(WPARAM, LPARAM param)
{
MOUSEHOOKSTRUCT *pWndMsg;
pWndMsg = (MOUSEHOOKSTRUCT *) param;
UpdateData( 1 );
CString csFormatString;
if( "NC" != (LPCSTR)param)
{
if(pWndMsg->wHitTestCode == HTCAPTION)
{
char Title[60] ;
GetWindowTextW(pWndMsg->hwnd,(LPWSTR)Title ,60);
csFormatString.Format(Title);
}
}
else
{
csFormatString.Format( "Nothing");
}
m_csArray.Add( csFormatString );
m_nTimeStamp = GetTickCount();
UpdateData(0);
return 1;
} // CHookDlg::OnMyMouseMove
=========================================================
but I get this Problem:
a window will appears with these information " Visual studio just in Time Debugger, An unhandled win32 exception occurred in Record Player.exe"
after that I can not do any thing because the computer does not respond to anything, and I have to restart it :(
so what is the problem?
note:"Record Player.exe"---> is the execution file for my project
Thank you
Re: using mouse hook to get selected text
Great. Now you have to find out the line that causes the effect. You add debug logging to file into your dll and analyze the trace next time after rebooting your system (please do not forget to flush log file buffers ;)).
Or better you search this site for mouse hook examples to compare with your code. :)
Re: using mouse hook to get selected text
And another question is: what message CRecordPlayerDlg::OnMyMouseMove handles? If this is WM_MOUSEMOVE, how can you interpret param as MOUSEHOOKSTRUCT* while it's:
Quote:
lParam
The low-order word specifies the x-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.
The high-order word specifies the y-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.
And if UWM_MOUSEMOVE is handled by that, are you sure your pointer points to the same process address space?
Re: using mouse hook to get selected text
Thanks for your help
I can solve the problem
but now I face another problem when I want to get the text of the selected item in the menu like Start menu in Windows XP
first I defined a handle for the menu then get the item under point of the mouse then get the item id after that get the menu text but the problem is that the handling is failing, i know this when I use Ismenu()
the code is:
================================
void MenuTextH ( )
{
CString csFormatString;
POINT point;
char lpString[100];
GetCursorPos(&point);
HWND hWnd = ::WindowFromPoint( point ); //window handle
HMENU hMenu= ::GetMenu(hWnd); //menu handle
if(::IsMenu(hMenu))
{
int wID= ::MenuItemFromPoint(hWnd,hMenu,point);
UINT uIDItem= ::GetMenuItemID(hMenu,wID);
int t=::GetMenuString(hMenu,NULL,(LPTSTR)lpString,100,MF_BYCOMMAND);
csFormatString.Format( lpString);
m_csArray.Add( csFormatString );
}
=============================================================
can any one help me