Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1616861
  • 博文数量: 585
  • 博客积分: 14610
  • 博客等级: 上将
  • 技术积分: 7402
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-15 10:52
文章存档

2013年(5)

2012年(214)

2011年(56)

2010年(66)

2009年(44)

2008年(200)

分类: C/C++

2012-04-29 04:55:46

获取光标位置方法研究

                                       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  
    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界面框架的窗口具体能不能得到位置没有试过。

阅读(2261) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~