Grabby/Grabby.c

94 lines
2.1 KiB
C
Raw Normal View History

// 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)
{
RECT capRegion = {0};
Screen *scrn = CreateScreen();
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 (WriteRegionToFile(scrn, &capRegion, _T("test.bmp")) == -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;
}