#include "mainFrom.h" #include "FileSystem.h" const wchar_t* xml = LR"xml( )xml"; void MainFrm::Init() { this->SetText(L"EzUI资源打包器"); //ui.LoadXmlFile("main.html"); UIString xmlData = xml; ui.LoadXml(xmlData.c_str(), xmlData.size()); ui.SetupUI(this); //第一页的控件 this->tab = (TabLayout*)this->FindControl("tab"); this->editPackDir = (TextBox*)this->FindControl("editPackDir"); this->editPackName = (TextBox*)this->FindControl("editPackName"); this->btnSatrtPackage = (Button*)this->FindControl("btnSatrtPackage"); this->labelTipsErr = (Label*)this->FindControl("labelTipsErr"); this->labelTips = (Label*)this->FindControl("labelTips"); this->editPackDir->TextChanged = [=](const UIString text)->void { this->OnPackDirChange(); }; //第二页的控件 this->editResFile = (TextBox*)this->FindControl("editResFile"); this->btnBrowserFile = (Button*)this->FindControl("btnBrowserFile"); this->listFiles = (VListView*)this->FindControl("listFiles"); this->btnUnPackage = (Button*)this->FindControl("btnUnPackage"); } MainFrm::MainFrm(const UIString& cmdLine) :Window(600, 400) { Init(); editPackDir->SetText(cmdLine); OnPackDirChange(); } void MainFrm::OnPackDirChange() { UIString dir = editPackDir->GetText(); if (dir.empty() || !PathExist(dir)) { editPackName->SetText(""); editPackName->Invalidate(); labelTipsErr->SetText(L"打包目录无效!"); labelTipsErr->Invalidate(); return; } else { labelTipsErr->SetText(""); labelTipsErr->Invalidate(); } ui_text::Replace(&dir, "\"", ""); ui_text::Replace(&dir, "\\", "/"); ui_text::Replace(&dir, "//", "/"); if (dir[dir.size() - 1] == '/') { dir.erase(dir.size() - 1, 1); } UIString resDir = dir; UIString rootDir; size_t pos = dir.rfind('/'); UIString dirName; if (pos != size_t(-1)) { rootDir = dir.substr(0, pos); dirName = dir.substr(pos + 1); } UIString resFile = rootDir + "/" + dirName + ".bin"; editPackName->SetText(resFile); editPackName->Invalidate(); } void MainFrm::OnClose(bool& close) { Application::Exit(0); } bool MainFrm::FileExists(const UIString& fileName) { DWORD dwAttr = GetFileAttributesW(fileName.unicode().c_str()); if (dwAttr == DWORD(-1)) { return false; } if (dwAttr & FILE_ATTRIBUTE_ARCHIVE) { return true; } return false; } void MainFrm::OnNotify(Control* sd, EventArgs& args) { if (args.EventType == Event::OnMouseDown) { if (sd->Name == "btnBrowserDir") { UIString dir = ShowFolderDialog(Hwnd(), "", ""); if (!dir.empty()) { this->editPackDir->SetText(dir); this->editPackDir->Invalidate(); this->OnPackDirChange(); } } if (sd->Name == "btnSatrtPackage") { do { UIString resDir = editPackDir->GetText(); UIString resFile = editPackName->GetText(); if (task && !task->IsStopped()) { ::MessageBoxW(Hwnd(), L"请等待上次任务完成!", L"失败", 0); break; } if (FileExists(resFile) && ::DeleteFileW(resFile.unicode().c_str()) == FALSE) { ::MessageBoxW(Hwnd(), L"文件已存在且无法覆盖!", L"失败", 0); break; } if (task) { delete task; task = NULL; } if (resFile.empty()) { ::MessageBoxW(Hwnd(), L"打包文件路径不正确!", L"失败", 0); break; } labelTips->SetText(L"正在计算..."); labelTips->Invalidate(); task = new Task([resDir, resFile, this]() { Resource::Package(resDir, resFile, [=](const UIString& file, int index, int count) { Invoke([&]() { int rate = (index + 1) * 1.0f / count * 100 + 0.5; labelTips->SetText(UIString("(" + std::to_string(rate) + "%)") + UIString(L"正在打包\"") + file + "\""); labelTips->Invalidate(); }); Sleep(2); }); Invoke([&]() { labelTips->SetText(L"打包成功!"); labelTips->Invalidate(); ::MessageBoxW(Hwnd(), L"打包成功!", L"成功", 0); }); }); } while (false); } if (sd->Name == "btnBrowserFile") { UIString resFile = ShowFileDialog(Hwnd()); OnResFileChange(resFile); } if (sd->Name == "btnUnPackage") { UIString resDir = ShowFolderDialog(Hwnd()); if (!resDir.empty() && PathExist(resDir)) { for (auto& it : this->res->Items) { UIString fileName = resDir + "/" + it.Name; UIString dir = Path::GetDirectoryName(fileName); Directory::Create(dir); File::Delete(fileName); UIString data; this->res->GetFile(it, &data); File::Write(data.c_str(), data.size(), fileName); } ::MessageBoxW(Hwnd(), L"解压完成!", L"", 0); } } } ezui::DefaultNotify(sd, args); } void MainFrm::OnResFileChange(UIString& resFile) { do { if (FileExists(resFile)) { Resource* newRes = new Resource(resFile); if (!newRes->IsGood()) { ::MessageBoxW(Hwnd(), L"不是标准的资源文件", L"错误", 0); delete newRes; break; } if (res) { delete res; res = NULL; } res = newRes; listFiles->Clear(true); for (auto& item : res->Items) { FileItem* fileItem = new FileItem(item.Name, item.Size); listFiles->Add(fileItem); } listFiles->Invalidate(); this->editResFile->SetText(resFile); this->editResFile->Invalidate(); } } while (false); } LRESULT MainFrm::WndProc(UINT msg, WPARAM wp, LPARAM lp) { //准备做一个解压的功能 if (msg == WM_DROPFILES) { HDROP hDrop = (HDROP)wp; UINT numFiles = ::DragQueryFileW(hDrop, 0xFFFFFFFF, NULL, 0); // 获取拖入的文件数量 TCHAR szFilePath[MAX_PATH]{ 0 }; ::DragQueryFileW(hDrop, 0, szFilePath, sizeof(szFilePath)); // 获取第一个文件路径 UIString file = szFilePath; if (tab->GetPageIndex() == 0) { //打包 if (PathExist(file)) { this->editPackDir->SetText(file); this->editPackDir->Invalidate(); this->OnPackDirChange(); } } else if (tab->GetPageIndex() == 1) { //解包 if (FileExists(file)) { this->OnResFileChange(file); } } } return __super::WndProc(msg, wp, lp); } MainFrm::~MainFrm() { if (task) { delete task; } if (res) { delete res; } }