Microsoft Windows Shell |
| Most file operations (such as copy a file, delete a file, zip a file, etc.) that the user can perform on a computer using the mouse are provided by a program called Windows Shell. COM allows a program to run the Windows Shell as it will be illustrated in the following problems. La mayoría de las operaciones (tales como copiar un archivo, borrar un archivo, comprimir en formato zip un archivo, etc.) que el usuario puede realizar en una computadora usando el ratón son de un programa llamado el Shell de Windows. COM permite a un programa ejecutar el Shell de Windows como se ilustra en los siguientes problemas. |
| Problem 1 |
| Create a Wintempla dialog application called MyZipFile to compress a file. First, we create an empty ZIP file. Second, we add the source file data to the empty ZIP file. In the code, FOF stands for File Operation Flag. Cree una aplicación de diálogo de Wintempla llamada MyZipFile para comprimir un archivo. Primero, se crea un archivo ZIP vacío. Segundo, se agregan los datos del archivo fuente al archivo ZIP vacío. En el código, FOF quiere decir Bandera de Operación de Archivo. |
| MyZipFile.h |
| #pragma once //______________________________________ MyZipFile.h #include "Resource.h" class MyZipFile: public Win::Dialog { public: MyZipFile() { ::CoInitialize(nullptr); } ~MyZipFile() { ::CoUninitialize(); } wstring s; const wchar_t* Compress(const wchar_t* inFilename, const wchar_t* outFilename); protected: . . . }; |
| MyZipFile.cpp |
| . . . void MyZipFile::Window_Open(Win::Event& e) { const wchar_t* errorDesc = Compress(L"C:\\selo\\image.jpg", L"C:\\selo\\image.zip"); if (errorDesc != nullptr) this->MessageBox(errorDesc, L"MyZipFile", MB_OK | MB_ICONERROR); } const wchar_t* MyZipFile::Compress(const wchar_t* inFilename, const wchar_t* outFilename) { _variant_t var_sourceFile(inFilename); _variant_t var_zipFile(outFilename); //___________________________________________________________ We create an empty zip file if (Sys::FileAssistant::CreateEmptyZipFile(outFilename) == false) return L"Error creating zip file"; IShellDispatchPtr shell; FolderPtr destinationFile = NULL; _variant_t copyOptions = FOF_NO_UI;// Do not display User Interface(VT_I4, FOF_NO_UI); if (HR_FAIL(CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void **)&shell), s)) return s.c_str(); if (HR_FAIL(shell->NameSpace(var_zipFile, &destinationFile), s)) return s.c_str(); if (HR_FAIL(destinationFile->CopyHere(var_sourceFile, copyOptions), s)) return s.c_str(); //___________________________________________________________ Wait for the copy thread to complete Sleep(1000); return nullptr; } |
| Problem 2 |
| Create a Wintempla dialog application called MyZipFolder to compress a folder using the ZIP format. First, we create an empty ZIP file. Finally, we add data to the empty ZIP file. Cree una aplicación de diálogo de Wintempla llamada MyZipFolder para comprimir un folder en formato ZIP. Primero, se crea un archivo ZIP vacío. Finalmente, se agregan los datos al archivo ZIP vacío. |
| MyZipFolder.h |
| #pragma once //______________________________________ MyZipFolder.h #include "Resource.h" class MyZipFolder: public Win::Dialog { public: MyZipFolder() { ::CoInitialize(nullptr); } ~MyZipFolder() { ::CoUninitialize(); } wstring s; const wchar_t* Compress(const wchar_t* inFolderName, const wchar_t* outFilename); . . . }; |
| MyZipFolder.cpp |
| . . . const wchar_t* errorDesc = Compress(L"C:\\selo\\119SAD", L"C:\\selo\\data.zip"); if (errorDesc != nullptr) this->MessageBox(errorDesc, L"MyZipFolder", MB_OK | MB_ICONERROR); } const wchar_t* MyZipFolder::Compress(const wchar_t* inFolderName, const wchar_t* outFilename) { _variant_t var_sourceFolder(inFolderName); _variant_t var_zipFile(outFilename); //___________________________________________________________ We create an empty zip file if (Sys::FileAssistant::CreateEmptyZipFile(outFilename) == false) return L"Error creating zip file"; IShellDispatchPtr shell = nullptr; FolderPtr folderSource = nullptr; FolderPtr folderDestination = nullptr; FolderItemsPtr folderItems = nullptr; _variant_t copyOptions = FOF_NO_UI; // Do not display User Interface(VT_I4, FOF_NO_UI); if (HR_FAIL(CoCreateInstance(CLSID_Shell, nullptr, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void**)&shell), s)) return s.c_str(); if (HR_FAIL(shell->NameSpace(var_zipFile, &folderDestination), s)) return s.c_str(); if (HR_FAIL(shell->NameSpace(var_sourceFolder, &folderSource), s)) return s.c_str(); folderSource->Items(&folderItems); _variant_t var_folderItems((IDispatch*)folderItems, true); if (HR_FAIL(folderDestination->CopyHere(var_folderItems, copyOptions), s)) return s.c_str(); //___________________________________________________________ Wait for the copy thread to complete Sleep(1000); return nullptr; } |
Wintempla ZIP |
| Wintempla provides the functions Sys::FileAssistant::ZipFile and Sys::FileAssistant::ZipFolder to compress files and folders respectively. Wintempla proporciona las funciones Sys::FileAssistant::ZipFile y Sys::FileAssistant::ZipFolder para comprimir archivos y carpetas respectivamente. |