Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. GetGuiThreadInfo can not get the caret's position

GetGuiThreadInfo can not get the caret's position

Scheduled Pinned Locked Moved C#
csharpvisual-studioquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    goldli88
    wrote on last edited by
    #1

    I want to get the caret position with my C# application.but in some cases ,GetGuiThreadInfo does not work creectly. it works well on 1. notepad 2. ie 3. explorer 4. word and works bad on (hwndCaret will be 0) 1. Visual Studio 2. Firefox 3. Sublime Text 2 Anyone who can helps me?

    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());
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups