HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Jul 13, 2009 8:00 pm Post subject: |
|
|
in c++?
This is my code for a program that will save the pictures like (bitmap0, bitmap1, bitmap2 etc)
saving a bitmap is pretty complex so i just found some code and modified it to my needs .
| Code: | //Steve aka blankrider
#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;
HBITMAP hBitmap;
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", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT,
500, 250, 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)
i = 0;
else{
i++;
}
while(FindNextFile(hFind, &FindFileData) != 0)
i++;
FindClose(hFind);
}
//found this somewhere and modified it somewhat, but mostly from codeproject (i think)
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 (int xStart, int yStart, int xFinish, int yFinish, HBITMAP hBitmap)
{
BITMAP bm;
HDC hdcScreen, hdcCompatible;
POINT pt;
char buffer2[33];
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 (!BitBlt(hdcCompatible,
0,0,
bm.bmWidth, bm.bmHeight,
hdcScreen,
0,0,
SRCCOPY))
MessageBox(0, "Error, BitBlt1", "Error!", MB_ICONSTOP | MB_OK);
wsprintf(buffer2, "%s%i%s", "bitmap", i , ".bmp");
if(!SaveBitmap(hdcCompatible, hBitmap, buffer2))
MessageBox(0, "Error Creating bmp!", "Error!", MB_ICONSTOP | MB_OK);
i++;
DeleteDC(hdcScreen);
DeleteDC(hdcCompatible);
DeleteObject(hBitmap);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
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 (0, 0, 500, 400, 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;
}
|
_________________
|
|