Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the word under the mouse cursor in Powerpoint 2013 using C#?

Tags:

c#

powerpoint

I would like to know the word under the mouse cursor in Powerpoint so that it can be used for a screen reader. Accessibility solutions are acceptable if it can distinguish between different words (vs a block).

like image 914
tofutim Avatar asked Sep 04 '15 16:09

tofutim


1 Answers

This is actually really hard, if you do not know what you are doing. There is a easy way and a hard way to do this. Easy way would be to use Microsoft UI automation framework (that includes Powerpoint automation). Alternative frameworks can also be used.

Hard way wold be to directly use win api.

For example: To get window title currently under the mouse.

    public static class dllRef
    {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GetCursorPos(out Point lpPoint);
        [DllImport("user32.dll")]
        private static extern IntPtr WindowFromPoint(Point point);
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int RegisterWindowMessage(string lpString);
        [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
        public static extern bool SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SendMessage(int hWnd, int Msg, int wparam, int lparam);

        public const int WM_USER = 0x400;
        public const int WM_COPYDATA = 0x4A;
        public const int WM_GETTEXT = 0x000D;
        public const int WM_GETTEXTLENGTH = 0x000E;

        public static void RegisterControlforMessages()
        {
            RegisterWindowMessage("WM_GETTEXT");
        }

        public static string GetText()
        {
            StringBuilder title = new StringBuilder();
            Point p = dllRef.getMousePosition();
            var lhwnd = dllRef.WindowFromPoint(p);
            var lTextlen = dllRef.SendMessage((int)lhwnd, dllRef.WM_GETTEXTLENGTH, 0, 0).ToInt32();
            if (lTextlen > 0)
            {
                title = new StringBuilder(lTextlen + 1);
                SendMessage(lhwnd, WM_GETTEXT, title.Capacity, title);
            }
            return title.ToString();
        }

        public static Point getMousePosition()
        {
            Point p = new Point();
            GetCursorPos(out p);
            return p;
        }
    }

and

    private void Form1_Load(object sender, EventArgs e)
    {
        Timer t = new Timer();
        t.Interval = 25;
        t.Tick += new EventHandler(Timer_Tick);
        t.Start();
    }
    public void Timer_Tick(object sender, EventArgs eArgs)
    {
        this.label1.Text = dllRef.GetText();
    }

In addition you can use Microsoft Spy++

enter image description here

to find if information you are looking for is exposed. Other then that I can really recommend you use automation framework that is layer built on top of this. Google has more then enough examples on this (as well as how to build sophisticated keyloggers).

like image 86
Margus Avatar answered Oct 10 '22 05:10

Margus