Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have located my program from the processes
C#
targetHandle = targetProcess.MainWindowHandle;


I now can get the Handle's hex which I use in spy++ (just preference)
C#
hex = targetHandle.ToString("X");


I convert it to an integer
C#
win = (int)targetHandle;


Now I want to use the handle info to retrieve general information as I would get with spy++.

Thanks.

What I have tried:

I tried the following...
C#
string str = "";

            // get title
            int txtLength =
                SendMessage(handle_get, WndMsg.WM_GETTEXTLENGTH, 0, 0);
            StringBuilder sbText = new StringBuilder(txtLength + 1);
            SendMessage(
                handle_get, WndMsg.WM_GETTEXT, sbText.Capacity, sbText);
            handle_Title[iIndex] = sbText.ToString();
            Console.WriteLine("caption : " + handle_Title[iIndex]);
            titleArrayList.Add(handle_Title[iIndex].ToString());

            // get classname
            StringBuilder sb = new StringBuilder(256);
            GetClassName(win, sb, sb.Capacity + 1);
            handle_Classname[iIndex] = sb.ToString();
            Console.WriteLine("classname : " + handle_Classname[iIndex]);
            classnameArrayList.Add(handle_Classname[iIndex].ToString());

            // get rectangle
            GetClientRect(handle_get, out handle_getRect);
            // if there is no rectangle
            if (IsRectEmpty(ref handle_getRect))
            {
                str = "\t\tRectangle : ";
            }
            else { Console.WriteLine("Handle has a rectangle"); }
            // or...
            if (GetClientRect( // if rectangle exists
                handle_get, out handle_getRect))
            {
                Console.WriteLine("Rectangle exists");
                handle_Rect[iIndex] = handle_getRect;

                #region Convert Coordinates 
                // top left point
                pointTL = new Point(
                    handle_Rect[iIndex].Locaation.X,
                    handle_Rect[iIndex].Locaation.Y);

                ClientToScreen(handle_get, ref pointTL);
                Console.WriteLine("point top left = " + pointTL);

                // bottom right point
                pointBR = new Point(
                    handle_Rect[iIndex].Locaation.X +
                    handle_Rect[iIndex].Right,
                    handle_Rect[iIndex].Locaation.Y +
                    handle_Rect[iIndex].Bottom);

                ClientToScreen(handle_get, ref pointBR);
                Console.WriteLine("point bottom right = " + pointBR);

                // set points to rectangle
                //SetRect(out handle_Rect[iIndex], pointTL.X, pointTL.Y,
                //    pointBR.X, pointBR.Y); 
                handle_Rect[iIndex].Left += pointTL.X;
                handle_Rect[iIndex].Right = pointBR.X;
                handle_Rect[iIndex].Top += pointTL.Y;
                handle_Rect[iIndex].Bottom = pointBR.Y;
                handle_Rect[iIndex].Width += pointBR.X - pointTL.X;
                handle_Rect[iIndex].Height += pointBR.Y - pointTL.Y;
                handle_Rect[iIndex].Size = new Size(
                    handle_Rect[iIndex].Width, handle_Rect[iIndex].Height);
                handle_Rect[iIndex].Locaation = new Point(
                    handle_Rect[iIndex].Left, handle_Rect[iIndex].Top);

                str =
                    "\t\tRectangle : (" + handle_Rect[iIndex].Left +
                    ", " + handle_Rect[iIndex].Top +
                    ")-(" + handle_Rect[iIndex].Right +
                    ", " + handle_Rect[iIndex].Bottom +
                    ") " + handle_Rect[iIndex].Size.Width +
                    "x" + handle_Rect[iIndex].Size.Height +
                    "\tWidth " + handle_Rect[iIndex].Width +
                    "\tHeight " + handle_Rect[iIndex].Height +
                    "\tLocation - " + handle_Rect[iIndex].Locaation +
                    "\tSize - " + handle_Rect[iIndex].Size;
                Console.WriteLine(str);
                #endregion

                #region Add to array list
                rectangleArrayList.Add(handle_Rect[iIndex].Left);
                rectangleArrayList.Add(handle_Rect[iIndex].Right);
                rectangleArrayList.Add(handle_Rect[iIndex].Top);
                rectangleArrayList.Add(handle_Rect[iIndex].Bottom);
                rectangleArrayList.Add(handle_Rect[iIndex].Width);
                rectangleArrayList.Add(handle_Rect[iIndex].Height);
                rectangleArrayList.Add(handle_Rect[iIndex].Size);
                rectangleArrayList.Add(handle_Rect[iIndex].Locaation);
                #endregion
            }


However, I dont get the Title, nor the Rectangle.
Spy++ shows that there is a title (caption) and rectangle.

So I must have done something wrong.
Posted
Updated 2-Jul-21 15:25pm
v2

1 solution

I found my mistake.
I used
handle_get = new IntPtr();

Whereas I needed to use
handle_get = new IntPtr(win);

It's working now. I get the title, and the rectangle. I just need to fix the rectangle as it's not correct.
 
Share this answer
 
v2
Comments
Member 7730841 3-Jul-21 12:17pm    
I also got the rectangle corrected.
In case this helps anyone, this works...

rect = GetClientRect(handle_get);
long result = GetWindowRect(handle_get, ref rect);
if (result > 0)
{
Console.WriteLine("Handle does have a rectangle");
}
else { Console.WriteLine("Rectangle is truly empty"); }

Apparently, this is useless...
GetClientRect(handle_get, out handle_getRect);
Or perhaps I haven't worked out how to use it correctly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900