use
EM_EXGETSEL[
^] to get the current location and save it and do this action when window lost it focus.
And also use
EM_EXSETSEL[
^] Window get back the focus to restore your last selected location.
EM_EXGETSEL will return you two position Min and Max, they will be equal if nothing is selected. If you don't understand ask.