擷取游標位置方法研究
johnchen
擷取游標位置可以使用GetCaretPos函數擷取位置,也可以通過GetGUIThreadInfo函數擷取位置。
1、GetCaretPos函數擷取游標位置,實現代碼:
CPoint point;
CRect rect;
GetWindowRect(&rect);
HWND hwnd=::GetFocus();
HWND pHwnd=::GetForegroundWindow();
AttachThreadInput(GetCurrentThreadId(),::GetWindowThreadProcessId(pHwnd,NULL),TRUE);
::GetCaretPos(&point);
::ClientToScreen(hwnd,&point);
AttachThreadInput(GetCurrentThreadId(),::GetWindowThreadProcessId(pHwnd,NULL),FALSE);
2、GetGUIThreadInfo函數擷取游標位置,實現代碼:
#include <winable.h>
HWND hwnd;
GUITHREADINFO pg;
POINT point;//游標位置
pg.cbSize=48;
::GetGUIThreadInfo(NULL,&pg);
hwnd=pg.hwndCaret;
if (pg.hwndCaret)
{
point.x=pg.rcCaret.right;
point.y=pg.rcCaret.bottom;
::ClientToScreen(pg.hwndCaret,&point);
}
//CString str;
//str.Format("x=%d,y=%d",point.x,point.y);
//AfxMessageBox(str);
總結:GetCaretPos函數能擷取WIN32一些程式視窗中游標位置,但是在IE7和WORD裡GetCaretPos是不能擷取游標位置的。而GetGUIThreadInfo函數擷取視窗座標位置是沒有限制,不過在VCL、GTK、SWT介面架構的視窗具體能不能得到位置沒有試過。