How to track position of the Caret

28 01 2012

Sometimes back one of my friends asked me, how could he able to find whether the caret is blinking in an active window? or just for an example if you have a dialog application which contains different controls like button, editbox, listbox etc and you are keep on moving the cursor focus over these control using “Tab” key, once the edit box gets focus, cursor starts to blink. How to detect such events?

The direct method to track caret position like windows events are to use Microsoft Active Accessibility. However the simpler approach to track caret position is to use GUITHREADINFO structure. This structure contains hwndCaret and rcCaret members that gives the handle to the window that owns the caret and client coordinate of the caret.

GetGUIThreadInfo function is used to get GUITHREADINFO structure info.

GetWindowThreadProcessId function retrieves the identifier of the thread that created the specified thread.


void OnTimer(UINT_PTR nIDEvent)
	// Retrieves the identifier of the thread that created the specified window
	DWORD dwThreadID = GetWindowThreadProcessId(GetForegroundWindow()->m_hWnd, NULL);

	guiThreadInfo.cbSize = sizeof(GUITHREADINFO);

	//Retrieves information about the active window or a specified GUI thread.
	if (GetGUIThreadInfo(dwThreadID, &guiThreadInfo))
		if (GUI_CARETBLINKING == guiThreadInfo.flags)
			AfxMessageBox(_T("Caret Blinking"));

		if (guiThreadInfo.hwndCaret)
			//caret position
			POINT pt = { guiThreadInfo.rcCaret.left, };
			//it should be an edit control
			AfxMessageBox(_T("edit control"));

Microsoft Active Accessibility provides a mechanism that allows to generate events to detect object creation, destruction, focus change, location change,… so on.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: