Detect text (word) under mouse pointer

Hi forum!

I'm wondering how could I read the word behind the mouse pointer. Let's say my app. has a CRichEditView and when user stops moving the mouse I want to grab the word that is under the pointer, so I can show a tooltip with certain info. Yes in a fashion like Visual Studio displays a variable value when you mouse over it in debugging mode.

After googling for a couple of days I've just found some approaches like using IAccessible class from MS Active Accesibility:

http://blogs.msdn.com/oldnewthing/archive/2004/04/23/118893.aspx

But it just gives the whole content of the view. And I guess it's not trivial to get the mouse coordinates and translate it into a document position.

Any clues, code samples, references or even claps in the shoulder will be much appreciated.

Thanks in advance
you can use IAccessiblePtr and here is a code which may help you..

http://nibuthomas.files.wordpress.com/2008/08/capturetext_srczip.jpg


after saving it change .jpg to .zip and see.

what else can be done??
like store the coordinates of each word in a std::map!! what you say??
now when someone hover the mouse according to coordinates find the word under that position from the map and display the information you want.. it is very much possible i am sure but what about the performance?? because if a textview has say 1Lakh words then what will be the performance..you need to device some other data structure.. as a map wont do..
thanks Writetonsharma, interesting idea.
I'll work on it and post back... if it takes me somewhere ;-)
I've found something easier that seems to fit:

SendMessage( EM_CHARFROMPOS, 0, <cursosposition> ) returns the character number (from 0 to text length) and additionally the line number where the mouse pointer is.

By now I'm making some tests with a dialog and a CEdit control and looks cool. Just remind newbies like me to derive a class from CEdit to add a WM_MOUSEMOVE handler for your control.
oh.. thats great.. the message might work as it looks from its name.. let me know if it works..


ok..
it is called subclassing in mfc terms..if you dont know..

see the code below..i created tictactoe some time back.. not to create a game but to show how subclassing works.. because if you have a cedit/cstatic or any control on a dialog and you want to catch a click or any other like you said a mouse move.. you cant without subclassing..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//ticStatic_drv.h
class ticStatic_drv : public CStatic
{
private:

	int			m_bCross;			//tells if its cross or circle


public:
	ticStatic_drv();
	virtual ~ticStatic_drv();
	
	//global, tells which turn would be played
	static int	m_iTurn;
	
	int GetStatus()
	{
		return m_bCross;
	}

	void SetStatus(int iStatus = 0)
	{
		m_bCross = iStatus;
	}


// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(ticStatic_drv)
	//}}AFX_VIRTUAL


protected:
	//{{AFX_MSG(ticStatic_drv)
	afx_msg void OnClicked();
	afx_msg void OnPaint();
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//ticStatic_drv.cpp
int	ticStatic_drv::m_iTurn;
//int ticStatic_drv::m_iStatusInfo[9];

/////////////////////////////////////////////////////////////////////////////
// ticStatic_drv

ticStatic_drv::ticStatic_drv()
{
	m_bCross = 0;
	
}

ticStatic_drv::~ticStatic_drv()
{
}


BEGIN_MESSAGE_MAP(ticStatic_drv, CStatic)
	//{{AFX_MSG_MAP(ticStatic_drv)
	ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// ticStatic_drv message handlers

//how to catch a click on controls...
void ticStatic_drv::OnClicked() 
{
	//if game hasnt started, return
	if(!CTicDlg::m_bGameStarted)
	{
		MessageBox("Start the game first from menu!!");
		return;
	}

	if(!m_bCross)
	{
		m_iTurn++;
//		m_bCross = (m_iTurn % 2) == 0 ? CIRCLE : CROSS;		//used when two players playing
		m_bCross = CROSS;		//when playing with computer, its always cross
		

				RedrawWindow();
		GetParent()->SendMessage(WM_UPDATEINFO,0,0);

	}
	
}

void ticStatic_drv::OnPaint() 
{
	CPaintDC dc(this);
	

	RECT		rect;

	if(m_bCross == CROSS)
	{
		HPEN hPen = CreatePen(PS_SOLID,2,RGB(0,0,255));
		SelectObject(dc,hPen);

		GetClientRect(&rect);
		
		dc.MoveTo(0,0);
		dc.LineTo(rect.right,rect.bottom);
		dc.MoveTo(rect.right,0);
		dc.LineTo(0,rect.bottom);

		DeleteObject(hPen);

	}

	if(m_bCross == CIRCLE)
	{
		HPEN hPen = CreatePen(PS_SOLID,2,RGB(0,0,255));
		HBRUSH hBrush = CreateSolidBrush(RGB(255,0,255));
		SelectObject(dc,hPen);
		SelectObject(dc,hBrush);

		GetClientRect(&rect);

		dc.Ellipse(rect.left + 5,rect.top + 5,rect.right - 5,rect.bottom - 5);

	}

}



see if that helps..
Topic archived. No new replies allowed.