All pastes #708127 Raw Edit

Miscellany

public text v1 · immutable
#708127 ·published 2007-09-23 16:22 UTC
rendered paste body
//Steve Bussey

#include <windows.h>

#include <stdio.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

static char sClassName[] = "MyClass";
static HINSTANCE zhInstance = NULL;
HWND HwndHideShow;
HFONT hf;
int i;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
    int nCmdShow) {
            WNDCLASSEX WndClass;
       HWND hwnd;
       MSG Msg;


       zhInstance = hInstance;

       WndClass.cbSize        = sizeof(WNDCLASSEX);
       WndClass.style         = NULL;
       WndClass.lpfnWndProc   = WndProc;
       WndClass.cbClsExtra    = 0;
       WndClass.cbWndExtra    = 0;
       WndClass.hInstance     = zhInstance;
       WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
       WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
       WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW);
       WndClass.lpszMenuName  = NULL;
       WndClass.lpszClassName = sClassName;
       WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

       if(!RegisterClassEx(&WndClass)) {
               MessageBox(0, "Error Registering Class!", "Error!", MB_ICONSTOP | MB_OK);
               return 0;
       }

       hwnd = CreateWindowEx(WS_EX_STATICEDGE, sClassName, "Printscreen Demo", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
                             250, 100, NULL, NULL, zhInstance, NULL);

       if(hwnd == NULL) {
               MessageBox(0, "Error Creating Window!", "Error!", MB_ICONSTOP | MB_OK);
               return 0;
       }
       ShowWindow(hwnd, nCmdShow);
       UpdateWindow(hwnd);





       while(GetMessage(&Msg, NULL, 0, 0)) {
               TranslateMessage(&Msg);
               DispatchMessage(&Msg);
       }

       return Msg.wParam;



}

void find(){
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
i = 0;
hFind = FindFirstFile("bitmap*.bmp", &FindFileData);

	if(hFind == INVALID_HANDLE_VALUE)
	{
	  MessageBox(0, "Error Invalid Path!", "Error!", MB_ICONSTOP | MB_OK);
	  i = 0;
	}
    else{
        i++;
    }
while(FindNextFile(hFind, &FindFileData) != 0)
	{
	i++;
	}

FindClose(hFind);
}

bool SaveBitmap(HDC hDC,HBITMAP hBitmap, char* szPath)
{
    FILE* fp = NULL;
    fp = fopen(szPath ,"wb");
    if(fp == NULL)
        return false;

    BITMAP Bm;
    BITMAPINFO BitInfo;
    ZeroMemory(&BitInfo, sizeof(BITMAPINFO));
    BitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    BitInfo.bmiHeader.biBitCount = 0;

    if(!GetDIBits(hDC, hBitmap, 0, 0, NULL, &BitInfo, DIB_RGB_COLORS))
        return (false);

    Bm.bmHeight = BitInfo.bmiHeader.biHeight;
    Bm.bmWidth  = BitInfo.bmiHeader.biWidth;

    BITMAPFILEHEADER    BmHdr;

    BmHdr.bfType        = 0x4d42;   // 'BM' WINDOWS_BITMAP_SIGNATURE
    BmHdr.bfSize        = (((3 * Bm.bmWidth + 3) & ~3) * Bm.bmHeight)
                          + sizeof(BITMAPFILEHEADER)
                          + sizeof(BITMAPINFOHEADER);
    BmHdr.bfReserved1    = BmHdr.bfReserved2 = 0;
    BmHdr.bfOffBits      = (DWORD) sizeof(BITMAPFILEHEADER)
                          + sizeof(BITMAPINFOHEADER);

    BitInfo.bmiHeader.biCompression = 0;
    // Writing Bitmap File Header ////
    fwrite(&BmHdr,sizeof(BITMAPFILEHEADER),1,fp);

    fwrite(&BitInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);

    BYTE *pData = new BYTE[BitInfo.bmiHeader.biSizeImage + 5];
    if(!GetDIBits(hDC, hBitmap, 0, Bm.bmHeight,
                 pData, &BitInfo, DIB_RGB_COLORS))
        return (false);
    if(pData != NULL)
        fwrite(pData,1,BitInfo.bmiHeader.biSizeImage,fp);

    fclose(fp);
    delete (pData);

    return (true);
}

void CreateBit (HDC hdc, int xStart, int yStart, int xFinish, int yFinish, HBITMAP hBitmap, bool bol)
{
    BITMAP bm;
    HDC hdcScreen, hdcCompatible;
    POINT pt;
    char* bitmap;

        hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
        hdcCompatible = CreateCompatibleDC(hdcScreen);
        hBitmap = CreateCompatibleBitmap(hdcScreen,
                     GetDeviceCaps(hdcScreen, HORZRES),
                     GetDeviceCaps(hdcScreen, VERTRES));
    if (hBitmap == 0)
    MessageBox(0, "Error, hBitmap", "Error!", MB_ICONSTOP | MB_OK);

    if (!SelectObject(hdcCompatible, hBitmap))
    MessageBox(0, "Error, Select Object", "Error!", MB_ICONSTOP | MB_OK);
    GetObject (hBitmap, sizeof(BITMAP), &bm);
    if(bol)
    {
         

    }
    if(!bol)
    {
              if (!BitBlt(hdcCompatible,
               0,0,
               bm.bmWidth, bm.bmHeight,
               hdcScreen,
               0,0,
               SRCCOPY))
            MessageBox(0, "Error, BitBlt1", "Error!", MB_ICONSTOP | MB_OK);
            wsprintf(bitmap,"%s%d%s","Bit",i,".bmp");

                if(!SaveBitmap(hdcCompatible, hBitmap, bitmap))
            MessageBox(0, "Error Creating bmp!", "Error!", MB_ICONSTOP | MB_OK);
            i++;

    }
    DeleteDC(hdcScreen);
    DeleteDC(hdcCompatible);
}


LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
        HBITMAP hBitmap;
        HDC hdc;
        PAINTSTRUCT ps;
        switch(Message) {
    case WM_CREATE:

        find();

        if (!RegisterHotKey(hwnd, 1, MOD_CONTROL, VK_F12))
        MessageBox(0, "Error RegisterHotkey", "Error", MB_OK);
    break;

    case WM_HOTKEY:
        if(wParam == 1)
        {
                    CreateBit (NULL, 0, 0, 500, 400, hBitmap, FALSE);
                    DeleteObject(hBitmap);
        }


        break;
    case WM_CLOSE:
        if(!UnregisterHotKey(hwnd, 1))
        MessageBox(0, "Error UnRegisterHotkey", "Error", MB_OK);

        DeleteObject(hBitmap);
        DestroyWindow(hwnd);
    break;

    case WM_DESTROY:

        PostQuitMessage(0);
    break;

    default:
        return DefWindowProc(hwnd, Message, wParam, lParam);
       }
       return 0;
}