// Grabby.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "Bitmap.h" #include "Overlay.h" #include "Grabby.h" /** * Print an error with a given context before bailing out */ void Die(LPTSTR lpContextMsg) { LPCTSTR lpszFmt = "%s: %s"; int iFullLen; LPTSTR lpszError = NULL, lpszFullMsg = NULL; // The whole point of this is to print an actual message, so get the // actual message if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, (LPTSTR) &lpszError, 0, NULL)) { if (lpszError != NULL) LocalFree(lpszError); lpszError = _T("unknown error"); } iFullLen = _sctprintf(lpszFmt, lpContextMsg, lpszError); lpszFullMsg = (LPTSTR) LocalAlloc(LPTR, sizeof(TCHAR) * (iFullLen + 1)); if (lpszFullMsg == NULL) { // I'm honestly gonna guess if we get here, we're out of memory. // So let's print what we have and bail MessageBox(NULL, lpContextMsg, _T("Catastrophic Error"), MB_OK | MB_ICONERROR); } else { _sntprintf(lpszFullMsg, iFullLen, lpszFmt, lpContextMsg, lpszError); MessageBox(NULL, lpszFullMsg, _T("Fatal Error"), MB_OK | MB_ICONERROR); } // TODO: should we still be nice and free memory here? ExitProcess(1); } int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { SYSTEMTIME stNow = {0}; RECT capRegion = {0}; Screen *scrn = CreateScreen(); #define OUTPUT_DIR_LEN 96 TCHAR szOutputDir[OUTPUT_DIR_LEN + 1]; #define FULL_OUT_PATH_LEN OUTPUT_DIR_LEN + 24 TCHAR szFullOutPath[FULL_OUT_PATH_LEN + 1]; HRESULT hRes; if (!scrn) { Die("Couldn't get screen information"); } switch (CreateOverlay(hInstance, scrn)) { case -1: Die("Couldn't create overlay window"); break; case 0: /* Do nothing */ break; case 1: // User requested we cancel out, so do that return 0; } GetChosenRect(&capRegion); if (!GetTempPath(OUTPUT_DIR_LEN, szOutputDir)) { Die("Couldn't get temporary directory"); } GetLocalTime(&stNow); hRes = StringCchPrintf(szFullOutPath, FULL_OUT_PATH_LEN, "%s\\scrn_%04d%02d%02d_%02d%02d%02d.bmp", szOutputDir, stNow.wYear, stNow.wMonth, stNow.wDay, stNow.wHour, stNow.wMinute, stNow.wSecond); if (FAILED(hRes)) { Die("Couldn't create screenshot file path"); } if (WriteRegionToFile(scrn, &capRegion, szFullOutPath) == -1) { Die("Couldn't grab region of the screen"); } MessageBox(NULL, _T("Successfully grabbed a part of the screen!"), _T("\\o/"), MB_OK | MB_ICONINFORMATION); return 0; }