📄 winmain.cpp
字号:
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 an 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. TCHAR szPath[_MAX_PATH]; if (GetWindowsDirectory(szPath, countof(szPath))) { *((LPTSTR)g_szTempDir) = *szPath; }#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");#else // Get our module's path and file name. We use the short path name for the // registry because it is guarenteed to contain no spaces. TCHAR szLongPath[_MAX_PATH]; TCHAR szPath[_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); // Associate "ZIP" file extensions to our application 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. int 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; wcstombs(pft->szExtAndDesc, szExtension + 1, length + 3); wcstombs(pft->szExtAndDesc + strlen(pft->szExtAndDesc) + 1, szDescription, length + 3); // Add the node to our list. if (pftLast) { pftLast->pNext = pft; } else { g_pftHead = pft; } pftLast = pft; } // Assign this image list to our tree control. ListView_SetImageList(g_hWndList, hil, LVSIL_SMALL);}//******************************************************************************//***** Our Main Window's Message Handler//******************************************************************************LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_CREATE: g_hWndMain = hWnd; return OnCreate(); case WM_ERASEBKGND: DrawBanner((HDC)wParam); return 0; case WM_SIZE: // Resize our list view control to match our client area. MoveWindow(g_hWndList, 0, g_cyCmdBar + 22, LOWORD(lParam), HIWORD(lParam) - (g_cyCmdBar + 22), TRUE);#ifndef _WIN32_WCE // On NT we have to resize our toolbar as well. MoveWindow(g_hWndCmdBar, 0, 0, LOWORD(lParam), g_cyCmdBar, TRUE);#endif return 0; case WM_SETFOCUS: // Always direct focus to our list control. SetFocus(g_hWndList); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; case WM_HELP: OnHelp(); return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -