C#获取某应用程序窗体中控件的句柄

C#获取某应用程序窗体中控件的句柄,第1张

如果窗口是现有程序的,使用VS自带的spy++获取窗口的相关信息,然後使用WinAPI获取句柄,具体参考spy++的使用方法和winapi的使用

FindWindow(

lpClassName, {窗口的类名}

lpWindowName: PChar {窗口的标题}

): HWND; {返回窗口的句柄; 失败返回 0}

//FindWindowEx 比 FindWindow 多出两个句柄参数:

FindWindowEx(

Parent: HWND; {要查找子窗口的父窗口句柄}

Child: HWND; {子窗口句柄}

ClassName: PChar; {}

WindowName: PChar {}

): HWND;

如果窗口是你的程序动态生成的,使用如下语句

Form _FORM=new Form();

IntPtr _P = _FORMHandle;

_P就是你实例化的窗口句柄

thisParentControls

你是说这个

foreach (Control theC in thisControls)

{

if (theC is DataGridView)

{

  List<string> list = new List<string>();

        [DllImport("user32dll")]

        [return: MarshalAs(UnmanagedTypeBool)]

        public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);

 

        [DllImport("user32dll")]

        public static extern int GetWindowText(int hWnd, IntPtr lpString, int nMaxCount);

 

        private void toolStripButton5_Click(object sender, EventArgs e)

        {

            listClear();

            EnumChildWindows(thisHandle, thisEnumWindowsMethod, IntPtrZero);

            //这里得到了所有的子窗口listCount;

        }

 

        private bool EnumWindowsMethod(int hWnd, int lParam)

        {

            IntPtr lpString = MarshalAllocHGlobal(200);

            GetWindowText(hWnd, lpString, 200);

            var text = MarshalPtrToStringAnsi(lpString);

            if (!stringIsNullOrWhiteSpace(text))

                listAdd(text);

            return true;

        }

以上就是关于C#获取某应用程序窗体中控件的句柄全部的内容,包括:C#获取某应用程序窗体中控件的句柄、c#如何根据父窗口句柄获取所有子窗口句柄、C#中已经获取了父窗口句柄如何枚举到子窗口的句柄。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/10046736.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存