c#已知窗体句柄,如何遍历出所有控件句柄

根据网上提供代码,可以用鼠标获取指定窗体句柄,
现在想遍历指定窗体上所有控件的句柄,希望提供代码,
找了一天的帖子了,请勿发参考链接,直接上代码。小白。。。

答案出来了:https://zhidao.baidu.com/question/1575352645104568900.html
说到做到,发代码,链接为源地址,下面我备注一下而已


List<string> list = new List<string>();
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern int GetWindowText(int hWnd, IntPtr lpString, int nMaxCount);

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            list.Clear();
            EnumChildWindows(this.Handle, this.EnumWindowsMethod, IntPtr.Zero);
            //这里得到了所有的子窗口list.Count;
                        //this.Handle改成你已得到的窗体句柄
        }

        private bool EnumWindowsMethod(int hWnd, int lParam)
        {
            IntPtr lpString = Marshal.AllocHGlobal(200);
            GetWindowText(hWnd, lpString, 200);//这里获得控件text
            var text = Marshal.PtrToStringAnsi(lpString);
            if (!string.IsNullOrWhiteSpace(text))
                list.Add(text);//添加到list,如果要获得句柄就新建list添加hWnd
            return true;
        }

我的方法:
BOOL CReadV850ChecksumeDlg::EnumChildProc(HWND hWnd, LPARAM lParam)
{
CWnd* pWnd=CWnd::FromHandle(hWnd);

hWnd1 = pWnd->GetSafeHwnd(); //CWnd->HWND
int IDn = 0;

char strWindowText[200];

char strClassName[100];

char m_store[MAX_PATH];
FILE *fp6;
FILE *fp1;
::GetClassName(hWnd,strClassName,100); ////get class name
::GetWindowText(hWnd,m_store,1024*1024); /////眔北ン
IDn = ::GetDlgCtrlID(hWnd1);//眔ID
/////log all the dilog iD Number

/////log all the dilog ID ,Name, ClassName.
// if(fp6=fopen("C:\V850_Test.txt","a+"))
// {
// _ftprintf(fp6,_T("ID:%d,markName:%s,ClassName:%s\r\n"),IDn,m_store,strClassName);
// fclose(fp6);
// }

if(strcmp(m_store,"EIS_A1 (0x001), Zundung")==0)

{
{
sprintf(EIS_A1_Name, "%s", m_store);//// find the "EIS_A1 (0x001), Zundung"
pWnd_EIS_A1=pWnd;

}
}

if(strcmp(m_store,"IGN_ON")==0)

{
{

sprintf(IGN_ON_Name, "%s", m_store);/// find the "IGN_ON"

pWnd_IGN_ON=pWnd;

}
}
/////////////
if(strcmp(strClassName,"Button")==0) //琌Button北ン
{
if(strcmp(m_store,"Init")==0)
{
DWORD dwStyle; // CButton* pBTN=(CButton *)GetDlgItem(IDC_BUTTON);

dwStyle=GetWindowLong(hWnd1,GWL_STYLE);

if((dwStyle &SS_TYPEMASK)==BS_PUSHBUTTON)
{
// AfxMessageBox("usual button ");
pWnd_InitButton=pWnd;
}

if((dwStyle&SS_TYPEMASK)==BS_AUTORADIOBUTTON)

{
// AfxMessageBox("radio button ");
pWnd_InitButton2=pWnd;
}
sprintf(InitButton_ClassName,"Class_Init:%s",strClassName);///find the "Init" button
sprintf(InitButton_Name,"Name_Init:%s",m_store);///find the "Init" button
// AfxMessageBox(InitButton_ClassName);
// AfxMessageBox(InitButton_Name);
Sleep(1000);
}

if(strcmp(m_store,"Start")==0)

{
if(IDn==1076)//////this is first " start" button, and the ID Number is 1076.if you need know all the dilog ID Number, you could look into C:\V850_Test.txt.
{
pWnd_StartButton=pWnd;
}
else
{
if(IDn==1103)///// this is second " start " button. and the ID Number is 1103.

{
pWnd_StartButton2=pWnd;
}
}
sprintf(StartButton_Name, "%s", m_store);///find the "Stop" button

}

if(strcmp(m_store,"Show Receive Messages")==0)

{
sprintf(Show_Receive_MessagesButton_Name, "%s", m_store);////// find the "Show Receive Messages" button

pWnd_Show_Receive_MessagesButton=pWnd;

}

}
// Sleep(3000);
/*if(strcmp(m_store,"Stop")==0)

{
sprintf(StartButton_Name, "%s", m_store);///find the "Stop" button

pWnd_StopButton=pWnd;

}

if(strcmp(m_store,"Show Receive Messages")==0)

{
sprintf(Show_Receive_MessagesButton_Name, "%s", m_store);////// find the "Show Receive Messages" button

pWnd_Show_Receive_MessagesButton=pWnd;

}

}

*/
return TRUE;

}

遍历调用 EnumChildWindows或者GetChildWindow API。
现在灌水和刷分的太多。不辨真假。所以详细代码,先采纳了再给。见谅。