📄 winmain.cpp
字号:
// Create our main window using our registered window class. g_hWndMain = CreateWindow(wc.lpszClassName, g_szAppName, dwStyle, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); // Quit now if we failed to create our main window. if (!g_hWndMain) { DebugOut(TEXT("CreateWindow() failed [%u]"), GetLastError()); ShutdownApplication(); return 0; } // Make sure our window is visible. Really only needed for NT. ShowWindow(g_hWndMain, nCmdShow); // Load our keyboard accelerator shortcuts. MSG msg; HACCEL hAccel = LoadAccelerators(g_hInst, MAKEINTRESOURCE(IDR_UNZIP)); DWORD dwPaintFlags = 0; // The message pump. Loop until we get a WM_QUIT message. while (GetMessage(&msg, NULL, 0, 0)) { // Check to see if this is an accelerator and handle it if neccessary. if (!TranslateAccelerator(g_hWndMain, hAccel, &msg)) { // If a normal message, then dispatch it to the correct window. TranslateMessage(&msg); DispatchMessage(&msg); // Wait until our application is up and visible before trying to // initialize some of our structures and load any command line file. if ((msg.message == WM_PAINT) && (dwPaintFlags != 0x11)) { if (msg.hwnd == g_hWndWaitFor) { dwPaintFlags |= 0x01; } else if (msg.hwnd == g_hWndList) { dwPaintFlags |= 0x10; } if (dwPaintFlags == 0x11) { InitializeApplication((szZipPath && *szZipPath) ? szZipPath : NULL); } } } } // Clean up code. ShutdownApplication(); // Nice clean finish - were out of here. return msg.wParam; } __except(EXCEPTION_EXECUTE_HANDLER) { // Something very bad happened. Try our best to appear somewhat graceful. MessageBox(NULL, TEXT("An internal error occurred. Possible causes are that you are ") TEXT("out of memory, a ZIP file (if one is loaded) contains an ") TEXT("unexpected error, or there is a bug in our program (that's why ") TEXT("it's free). Pocket UnZip cannot continue. It will exit now, ") TEXT("but you may restart it and try again.\n\n") TEXT("If the problem persists, please write to stevemil@pobox.com with ") TEXT("any information that might help track down the problem."), g_szAppName, MB_ICONERROR | MB_OK); } return 1;}//******************************************************************************//***** Startup and Shutdown Functions//******************************************************************************void InitializeApplication(LPCTSTR szZipFile) { // This function is called after our class is registered and all our windows // are created and visible to the user. // Show hour glass cursor. HCURSOR hCur = SetCursor(LoadCursor(NULL, IDC_WAIT)); // Register UnZip in the registry to handle ".ZIP" files. RegisterUnzip(); // Enumerate the system file assoications and build an image list. BuildImageList(); // Load our initial MRU into our menu. InitializeMRU(); // Restore/remove our cursor. SetCursor(hCur); // Clear our initialization window handle. g_hWndWaitFor = NULL; // Load our command line file if one was specified. Otherwise, just update // our banner to show that no file is loaded. if (szZipFile) { ReadZipFileList(szZipFile); } else { DrawBanner(NULL); } // Enable some controls. EnableAllMenuItems(IDM_FILE_OPEN, TRUE); EnableAllMenuItems(IDM_FILE_CLOSE, TRUE); EnableAllMenuItems(IDM_VIEW_EXPANDED_VIEW, TRUE); EnableAllMenuItems(IDM_HELP_ABOUT, TRUE); // Set our temporary directory.#ifdef _WIN32_WCE g_szTempDir = TEXT("\\Temporary Pocket UnZip Files");#else g_szTempDir = TEXT("C:\\Temporary Pocket UnZip Files"); // Set the drive to be the same drive as the OS installation is on. if (GetWindowsDirectory(g_szTempDirPath, countof(g_szTempDirPath))) { lstrcpy(g_szTempDirPath + 3, TEXT("Temporary Pocket UnZip Files")); g_szTempDir = g_szTempDirPath; }#endif}//******************************************************************************void ShutdownApplication() { // Free our banner font. if (g_hFontBanner) { DeleteObject(g_hFontBanner); g_hFontBanner = NULL; } // Delete our FILE_TYPE_NODE linked list. for (FILE_TYPE_NODE *pft = g_pftHead; pft; ) { FILE_TYPE_NODE *pftNext = pft->pNext; delete[] (BYTE*)pft; pft = pftNext; } g_pftHead = NULL; // If there are no other instances of our application open, then delete our // temporary directory and all the files in it. Any files opened for viewing // should be locked and will fail to delete. This is to be expected. if (g_szTempDir && (FindWindow(g_szClass, NULL) == NULL)) { TCHAR szPath[_MAX_PATH]; _tcscpy(szPath, g_szTempDir); DeleteDirectory(szPath); }}//******************************************************************************void RegisterUnzip() {#ifdef _WIN32_WCE // WARNING! Since Windows CE does not support any way to get your binary's // name at runtime, we have to hard-code in "punzip.exe". If our binary is // not named this or is in a non-path directory, then we will fail to // register ourself with the system as the default application to handle // ".zip" files. TCHAR szPath[32] = TEXT("punzip.exe"); TCHAR szTstPath[32];#else // Get our module's path and file name. We use the short path name for the // registry because it is guaranteed to contain no spaces. TCHAR szLongPath[_MAX_PATH]; TCHAR szPath[_MAX_PATH]; TCHAR szTstPath[_MAX_PATH]; GetModuleFileName(NULL, szLongPath, countof(szLongPath)); GetShortPathName(szLongPath, szPath, countof(szPath));#endif // Store a pointer to the end of our path for easy appending. LPTSTR szEnd = szPath + _tcslen(szPath); BOOL fDoRegisterPUnZip = TRUE; // Associate "ZIP" file extensions to our application if (RegReadKey(HKEY_CLASSES_ROOT, TEXT(".zip"), szTstPath, sizeof(szTstPath))) { if (_tcscmp(szTstPath, TEXT("zipfile")) != 0) fDoRegisterPUnZip = FALSE; else if (RegReadKey(HKEY_CLASSES_ROOT, TEXT("zipfile\\shell\\Open\\command"), szTstPath, sizeof(szTstPath)) && (_tcsncmp(szTstPath, szPath, _tcslen(szPath)) != 0)) fDoRegisterPUnZip = FALSE; if (!fDoRegisterPUnZip) { fDoRegisterPUnZip = (IDOK == MessageBox(g_hWndMain, TEXT("Currently, Pocket UnZip is not registered as default ") TEXT("handler for Zip archives.\n\n") TEXT("Please, confirm that Pocket UnZip should now register itself ") TEXT("as default application for handling Zip archives (.zip files)"), g_szAppName, MB_ICONQUESTION | MB_OKCANCEL)); } } if (fDoRegisterPUnZip) { RegWriteKey(HKEY_CLASSES_ROOT, TEXT(".zip"), TEXT("zipfile")); RegWriteKey(HKEY_CLASSES_ROOT, TEXT("zipfile"), TEXT("ZIP File")); RegWriteKey(HKEY_CLASSES_ROOT, TEXT("zipfile\\shell"), NULL); RegWriteKey(HKEY_CLASSES_ROOT, TEXT("zipfile\\shell\\Open"), NULL); _tcscpy(szEnd, TEXT(" %1")); RegWriteKey(HKEY_CLASSES_ROOT, TEXT("zipfile\\shell\\Open\\command"), szPath); // Register our program icon for all ZIP files. _stprintf(szEnd, TEXT(",-%u"), IDI_ZIPFILE); RegWriteKey(HKEY_CLASSES_ROOT, TEXT("zipfile\\DefaultIcon"), szPath); } // Create our application option location. RegWriteKey(HKEY_CURRENT_USER, TEXT("Software"), NULL); RegWriteKey(HKEY_CURRENT_USER, g_szRegKey, NULL);}//******************************************************************************void BuildImageList() { // Create our global image list.#ifdef _WIN32_WCE // On Windows CE, we can't spare a color for the mask, so we have to create // the mask in a separate monochrome bitmap. HIMAGELIST hil = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 8, 8); // Load our default bitmaps into the image list. HBITMAP hBmpImageList = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_IMAGELIST)); HBITMAP hBmpMask = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_IMAGELIST_MASK)); ImageList_Add(hil, hBmpImageList, hBmpMask); DeleteObject(hBmpImageList); DeleteObject(hBmpMask);#else // On Windows NT, we use magenta as a transparency mask color. HIMAGELIST hil = ImageList_LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_IMAGELIST), 16, 8, RGB(255, 0, 255));#endif // Set up for our registry file type enumeration. FILE_TYPE_NODE *pftLast = NULL; TCHAR szExtension[128], szKey[128], szDescription[_MAX_PATH], szIconFile[_MAX_PATH + 16]; DWORD dwIndex = 0, dwCount = countof(szExtension); // Enumerate all the keys immediately under HKEY_CLASSES_ROOT. while (ERROR_SUCCESS == RegEnumKeyEx(HKEY_CLASSES_ROOT, dwIndex++, szExtension, &dwCount, NULL, NULL, NULL, NULL)) { dwCount = countof(szExtension); // Check to see if we read an extension key (starts with a period) if (*szExtension != TEXT('.')) { continue; } // Read the actual key name for this extension. if (!RegReadKey(HKEY_CLASSES_ROOT, szExtension, szKey, sizeof(szKey))) { continue; } // Read the Description for this extension. RegReadKey(HKEY_CLASSES_ROOT, szKey, szDescription, sizeof(szDescription)); HICON hIcon = NULL; LPTSTR szEnd = szKey + _tcslen(szKey); // Attempt to get an icon for this extension from the "DefaultIcon" key. _tcscpy(szEnd, TEXT("\\DefaultIcon")); if (RegReadKey(HKEY_CLASSES_ROOT, szKey, szIconFile, sizeof(szIconFile))) { // Look for the comma between the file name and the image. LPTSTR szImageId = _tcschr(szIconFile, TEXT(',')); if (szImageId) { // NULL terminate the file name portion of szIconFile. *(szImageId++) = TEXT('\0'); // Get the image ID value from szIconFile. int imageId = _ttoi(szImageId); // Extract the icon from the module specified in szIconFile. ExtractIconEx(szIconFile, imageId, NULL, &hIcon, 1); if (hIcon == NULL) { ExtractIconEx(szIconFile, imageId, &hIcon, NULL, 1); } } } // If we failed to get the icon using the "DefaultIcon" key, then try // using the "shell\Open\command" key. if (hIcon == NULL) { _tcscpy(szEnd, TEXT("\\shell\\Open\\command")); if (RegReadKey(HKEY_CLASSES_ROOT, szKey, szIconFile, sizeof(szIconFile))) { // Get a pointer to just the binary - strip quotes and spaces. LPTSTR szPath; if (*szIconFile == TEXT('\"')) { szPath = szIconFile + 1; if (szEnd = _tcschr(szPath, TEXT('\"'))) { *szEnd = TEXT('\0'); } } else { szPath = szIconFile; if (szEnd = _tcschr(szPath, TEXT(' '))) { *szEnd = TEXT('\0'); } } // Extract the icon from the module specified in szIconFile. ExtractIconEx(szPath, 0, NULL, &hIcon, 1); if (hIcon == NULL) { ExtractIconEx(szPath, 0, &hIcon, NULL, 1); } } } // If we found an icon, add it to our image list. int image = -1; if (hIcon) { image = ImageList_AddIcon(hil, hIcon); } // If no icon could be found, then check to see if this is an executable. if ((image == -1) && (#ifndef _WIN32_WCE // Windows CE only recognizes EXE's as executable. !_tcsicmp(szExtension + 1, TEXT("bat")) || !_tcsicmp(szExtension + 1, TEXT("cmd")) || !_tcsicmp(szExtension + 1, TEXT("com")) ||#endif !_tcsicmp(szExtension + 1, TEXT("exe")))) { image = IMAGE_APPLICATION; } // If we don't have a description or a icon, then bail on this extension. if (!*szDescription && (image < 0)) { continue; } // Create our FILE_TYPE_NODE. size_t length = _tcslen(szExtension) - 1 + _tcslen(szDescription); FILE_TYPE_NODE *pft = (FILE_TYPE_NODE*) new BYTE[ sizeof(FILE_TYPE_NODE) + (sizeof(TCHAR) * length)]; // Bail out if we could not create our node. if (!pft) { DebugOut(TEXT("Not enough memory to create a FILE_TYPE_NODE.")); continue; } // Fill in the node. pft->pNext = NULL; pft->image = (image >= 0) ? image : IMAGE_GENERIC; TSTRTOMBS(pft->szExtAndDesc, szExtension + 1, length + 2); size_t sizext = (strlen(pft->szExtAndDesc) + 1); TSTRTOMBS(pft->szExtAndDesc + sizext, szDescription, length - sizext + 2); // Add the node to our list. if (pftLast) { pftLast->pNext = pft;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -