怎样获得当前桌面上活动窗口的标题?
我用了API 的GetActiveWindow
和CWnd::GetActiveWindows都不行用CWnd::GetActiveWindows时每次当我这个程序本身是活动窗口时是正常的但是当其他应用程序为活动窗口时程序就会非法操作,

解决方案 »

  1.   

    用查找窗口API Findwoindow()查找你要找的窗口。
      

  2.   

    int  nIndex=0;
    HWND hwDesktop=::GetDesktopWindow();//取 得 桌 面 句 柄 
    HWND hWnd=::GetWindow(hwDesktop,GW_CHILD);//取 得 桌 面 子 句 柄 
    HWND parentWnd = NULL;
    HWND listWnd = NULL;
    //CWnd* cWnd = NULL;
    // CListBox m_cListProcess;m_cListProcess.AddString (_T("The Top window"));
    m_cListProcess.SetItemData(nIndex++,NULL);CString strTitle,strClass,strLookstring; 
    TCHAR szbuf[255];while(hWnd!=NULL)//循 环 取 子 句 柄 的 同 级 句 柄 
        { 
    if(::IsWindowVisible(hWnd))//判 断 是 否 为 可 显 示 窗 口 

    parentWnd = NULL;
    listWnd = hWnd;
    while (listWnd )
    {
    parentWnd = listWnd;
    listWnd = ::GetParent(parentWnd);
    }
    ::SendMessage(parentWnd,WM_GETTEXT,255,(long)szbuf);
    TRACE("window caption is: %s .\r\n",szbuf);
    //GetClassName(hwnd,szbuf,255);//类名
    //TRACE("class name is :%s .\r\n",szbuf);
    ::GetWindowText(parentWnd,strTitle.GetBuffer(255),254); 
    ::GetClassName(parentWnd,strClass.GetBuffer(255),254); 
    strTitle.ReleaseBuffer();//标 题 
    strClass.ReleaseBuffer();//类 名 

    if(!strTitle.IsEmpty()) 
    m_cListProcess.AddString (strTitle);
    //else 
    // m_cListProcess.AddString (strClass); m_cListProcess.SetItemData(nIndex++,(DWORD)hWnd);
    TRACE("hwnd:%08X: , 标 题: %s ,类 名 %s .\r\n",hWnd,strTitle,strClass);

    hWnd=::GetWindow(hWnd,GW_HWNDNEXT); 
    //TRACE("~%08X~\r\n",hWnd);
    }