none
如何取得插入光标所有的位置,即caret位置 RRS feed

  • 问题

  • 使用windows的api GetGuiThreadInfo 在获取光标位置时时灵时不灵。

    在以下目标可以

    1. notepad
    2. ie
    3. explorer
    4. word、

    在以下时就不行。获取的 GuiThreadInfo 结构中 hwndCaret的值为0

    1. Visual Studio
    2. Firefox
    3. Sublime Text 2

    谁知道这个应该怎么解决?

        public partial class Form1 : Form
            {
        
                [DllImport("user32.dll")]
                public static extern IntPtr GetForegroundWindow();
        
                [DllImport("user32.dll", CharSet = CharSet.Auto)]
                public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
                
                [DllImport("user32.dll")]
                static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
        
                [DllImport("user32.dll")]
                static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);
        
        
                [StructLayout(LayoutKind.Sequential)]
                public struct GUITHREADINFO
                {
                    public int cbSize;
                    public int flags;
                    public IntPtr hwndActive;
                    public IntPtr hwndFocus;
                    public IntPtr hwndCapture;
                    public IntPtr hwndMenuOwner;
                    public IntPtr hwndMoveSize;
                    public IntPtr hwndCaret;
                    public Rectangle rectCaret;
                }
        
                public  GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd)
                {
                    if (hwnd != IntPtr.Zero)
                    {
                        uint threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero);
        
                        GUITHREADINFO guiThreadInfo = new GUITHREADINFO();
                        guiThreadInfo.cbSize = Marshal.SizeOf(guiThreadInfo);
        
                        if (GetGUIThreadInfo(threadId, ref guiThreadInfo) == false)
                            return null;
                        return guiThreadInfo;
                    }
                    return null;
                }
        
                public void SendText(string text)
                {
                    IntPtr hwnd = GetForegroundWindow();
                    if (String.IsNullOrEmpty(text))
                        return;
                    GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd);
                    AddMessage("go");
                    if (guiInfo != null)
                    {
                        IntPtr ptr = (IntPtr)guiInfo.Value.hwndCaret;
                        AddMessage("hwndCaret = " + ptr.ToInt32());
                        if (ptr != IntPtr.Zero)
                        {
                            AddMessage("Left = " + guiInfo.Value.rectCaret.Left);
                            for (int i = 0; i < text.Length; i++)
                            {
                                SendMessage(ptr, 0x102, (IntPtr)(int)text[i], IntPtr.Zero);
                            }
                        }
                    }
                }
        
                public Form1()
                {
                    InitializeComponent();
                }
        
                private void AddMessage(string message)
                {
                    var i = listBox1.Items.Add(message);
                    listBox1.SelectedIndex = i;
                }
        
                private void button1_Click(object sender, EventArgs e)
                {
                    Thread.Sleep(2000);
                    //textBox1.Focus();
                    SendText("Abcdefg");
                    AddMessage("------------");
                }
            }



    2013年7月21日 9:44

答案

全部回复