码迷,mamicode.com
首页 > 其他好文 > 详细

光标跟随

时间:2017-03-14 10:37:54      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:local   client   osc   targe   region   x64   kernel   word   read   

       #region 得到光标在屏幕上的位置
 
        [DllImport("user32")]
 
         public static extern bool GetCaretPos(out Point lpPoint);
 
         [DllImport("user32.dll")]
 
         private static extern IntPtr GetForegroundWindow();
 
         [DllImport("user32.dll")]
 
         private static extern IntPtr GetFocus();
 
         [DllImport("user32.dll")]
 
         private static extern IntPtr AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, int fAttach);
 
         [DllImport("user32.dll")]
 
         private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
 
         [DllImport("kernel32.dll")]
 
         private static extern IntPtr GetCurrentThreadId();
 
         [DllImport("user32.dll")]
 
         private static extern void ClientToScreen(IntPtr hWnd, ref Point p);
 
 
         private Point CaretPos()
 
         {
 
             IntPtr ptr = GetForegroundWindow();
 
             Point p = new Point();
 
 
             //得到Caret在屏幕上的位置   
 
             if (ptr.ToInt32() != 0)
 
             {
 
                 IntPtr targetThreadID = GetWindowThreadProcessId(ptr, IntPtr.Zero);
 
                 IntPtr localThreadID = GetCurrentThreadId();
 
 
                 if (localThreadID != targetThreadID)
 
                 {
 
                     AttachThreadInput(localThreadID, targetThreadID, 1);
 
                     ptr = GetFocus();
 
                     if (ptr.ToInt32() != 0)
 
                     {
 
                         GetCaretPos(out   p);
 
                         ClientToScreen(ptr, ref   p);
 
                     }
 
                     AttachThreadInput(localThreadID, targetThreadID, 0);
 
                 }
 
             }
 
             return p;
 
         }
 
         #endregion

光标跟随

标签:local   client   osc   targe   region   x64   kernel   word   read   

原文地址:http://www.cnblogs.com/swtool/p/6546937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!