z474243930 2017-11-30 14:09 采纳率: 33.3%
浏览 5459
已采纳

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

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

  • 写回答

3条回答 默认 最新

  • z474243930 2017-12-01 03:46
    关注

    答案出来了: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;
            }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • qq_41233270 2017-11-30 14:21
    关注

    我的方法:
    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;

    }

    评论
  • threenewbee 2017-12-01 01:22
    关注

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

    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 div editable中的光标问题
  • ¥15 mysql报错1415Not allowed to return a result set from a trigger 不知如何修改
  • ¥60 Python输出Excel数据整理,算法较为复杂
  • ¥15 回答几个问题 关于数据库
  • ¥15 51单片机串口通信问题,未完成且要修改
  • ¥15 百鸡问题 c++编程问题(相关搜索:输出数据)
  • ¥30 如何在CMD中设置代理
  • ¥15 我有一块薛定谔的硬盘
  • ¥15 微信小游戏开发2D碰撞检测问题
  • ¥30 MapReduce案例实践(实验过程需要截图加文字)