// use user's temp folder name as part of mutex name// extra nameautoconstexprextra{L"PlayerWinMutex\0"sv};// name bufferautoname{std::to_array<wchar_t,MAX_PATH+2+extra.size()+1>({})};// get tmp folder, not guaranteed path availablityautolength{GetTempPathW(static_cast<DWORD>(name.size()),&name[0])};// add extra to pathstd::memcpy(&name[length],extra.data(),extra.size()+1);// mutex name can't contain '\'std::replace(&name[0],&name[length],'\\','/');// check mutex is held by another instance, NO NEED TO USE GetLastErrorif(!::CreateMutexExW(nullptr,&name[0],CREATE_MUTEX_INITIAL_OWNER,NULL)){// do something else,// such as print log, notify user or synchroniz with other instance::ExitProcess(1u);}
// enumerate all direct child windows of desktopfor(autopre{::FindWindowExW(nullptr,nullptr,nullptr,nullptr)};// return null if at the endpre!=nullptr;// pass the pre as the 2nd argument to get next handlepre=::FindWindowExW(nullptr,pre,nullptr,nullptr)){// check if window has the specified property// set in MainWindow constructorif(::GetPropW(pre,L"PlayerWinRT")){// show and restore window::ShowWindow(pre,SW_RESTORE);// activate, set foreground and get forcus::SetForegroundWindow(pre);// exit app, DO NOT USE this->EXIT BECAUSE NOT CONSTRUCTED::ExitProcess(1u);}}
在创建窗口时添加如下代码:
C++
HWNDhandle;// current window's handle::SetPropW(handle,L"PlayerWinRT",handle);