[置顶] 得到桌面上所有打开窗口的标题

// 取得桌面上打开的所有窗口的标题
void CshareFileDlg::OnBnClickedButtonShare()
{

    HWND        hwndDesk;
    HWND        hwndAll    ;
    LPWSTR       lpwstr    ;
    CString        strTitle;
    CString        strText    ;

    lpwstr = NULL            ;
    lpwstr = new WCHAR[256]    ;

    hwndDesk = ::GetDesktopWindow(); // 得到桌面窗口句柄
    hwndAll  = ::GetWindow( hwndDesk , GW_CHILD );// 得到桌面窗口的子窗口句柄

    // 当存在子窗口
    while( hwndAll ){

        // 得到子窗口的标题
        ::GetWindowText( hwndAll , lpwstr , 256);

        strTitle = (LPCTSTR)lpwstr;

        // 判断是否存在打开的word文档,是则显示此word文档的标题,问用户是否关闭
        if( -1 != strTitle.Find( TEXT( "Microsoft Word" ) )){

            strText.Format( TEXT( "有word在运行,标题为/"%s/",您是否关闭此word文档?" ) , strTitle );

            // 如果用户选择yes,则提醒用户只有关闭word后本应用程序才能继续运行
            if ( IDYES == AfxMessageBox( strText , MB_YESNO )){

                AfxMessageBox( TEXT( "请您关闭打开的word文档,只有关闭word后本应用程序才能继续运行" ) );

            } else {
    
                return;
            }

        }
        // 得到下一个桌面子窗口
        hwndAll = ::GetWindow( hwndAll , GW_HWNDNEXT );

    }

}

你可能感兴趣的:([置顶] 得到桌面上所有打开窗口的标题)