Add browser download confirmation and parameterized client startup
This commit is contained in:
1 parent
028fec64be
commit
3cf197a98c
25 files changed
+568
-46
No files matched your search
+29
-5
@@ -12,14 +12,38 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
|
||||
UNREFERENCED_PARAMETER(hInstance);
|
||||
UNREFERENCED_PARAMETER(nCmdShow);
|
||||
|
||||
const std::wstring command=GetCommandLineW();
|
||||
StartupOptions startup;
|
||||
if (!ParseStartupCommandLine(command,startup)) {
|
||||
MessageBoxW(nullptr,L"启动参数无效。\n用法:ttdownloader.exe --add \"https://example.com/file.zip\"\n也可直接传入下载地址,或使用 --add 打开空白添加窗口。",L"TTDownloader",MB_ICONERROR);
|
||||
return 1;
|
||||
}
|
||||
HANDLE instance_mutex = ::CreateMutexW(nullptr, TRUE, L"Local\\TTDownloader.SingleInstance");
|
||||
if (!instance_mutex) {
|
||||
MessageBoxW(nullptr,L"无法访问客户端实例,请使用相同用户和权限启动。",L"TTDownloader",MB_ICONERROR); return 1;
|
||||
}
|
||||
if (::GetLastError() == ERROR_ALREADY_EXISTS)
|
||||
{
|
||||
HWND hwnd = ::FindWindowW(DownloaderForm::kClassName.c_str(), nullptr);
|
||||
if (hwnd != nullptr)
|
||||
::PostMessageW(hwnd, DownloaderForm::kShowMainWindowMessage, 0, 0);
|
||||
HWND hwnd=nullptr;
|
||||
const auto deadline=GetTickCount64()+5000;
|
||||
do {
|
||||
hwnd=FindWindowW(DownloaderForm::kClassName.c_str(),nullptr);
|
||||
if (hwnd) break;
|
||||
Sleep(50);
|
||||
} while (GetTickCount64()<deadline);
|
||||
bool delivered=false;
|
||||
if (hwnd) {
|
||||
DWORD pid=0; GetWindowThreadProcessId(hwnd,&pid); AllowSetForegroundWindow(pid);
|
||||
if (!startup.show_add) delivered=PostMessageW(hwnd,DownloaderForm::kShowMainWindowMessage,0,0)!=FALSE;
|
||||
else {
|
||||
COPYDATASTRUCT data{kStartupCopyDataTag,static_cast<DWORD>((command.size()+1)*sizeof(wchar_t)),const_cast<wchar_t*>(command.c_str())};
|
||||
DWORD_PTR result=0;
|
||||
delivered=SendMessageTimeoutW(hwnd,WM_COPYDATA,0,reinterpret_cast<LPARAM>(&data),SMTO_ABORTIFHUNG|SMTO_BLOCK,3000,&result) && result;
|
||||
}
|
||||
}
|
||||
::CloseHandle(instance_mutex);
|
||||
return 0;
|
||||
if (!delivered) MessageBoxW(nullptr,L"正在运行的客户端未能接收启动参数,请完全退出旧版后重试。",L"TTDownloader",MB_ICONERROR);
|
||||
return delivered?0:1;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +52,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
|
||||
if (!form.Create(nullptr,DownloaderForm::kClassName.c_str(),L"TTDownloader",940,640,true)) {
|
||||
MessageBoxW(nullptr,L"无法创建主窗口。",L"TTDownloader",MB_ICONERROR); return false;
|
||||
}
|
||||
form.CenterWindow(); form.ShowWindow(); return true;
|
||||
form.CenterWindow(); form.ShowWindow(); form.OpenStartupRequest(startup); return true;
|
||||
});
|
||||
|
||||
::CloseHandle(instance_mutex);
|
||||
|
||||
Reference in new issue
Block a user