redbug | J'ai trouvé ceci sur les newsgroups
Code :
- #include<windows.h>
- // variables
- static HWND main_window = 0; // main window
- static HWND capture = 0; // window we want to capture
- static HBITMAP bitmap = 0; // dibsection
- static BYTE* memory = 0; // dibsection memory
- // functions
- LRESULT CALLBACK MainWndProc(HWND window, UINT message, WPARAM wparam,
- LPARAM lparam);
- HBITMAP CreateDibsection24(HDC winDC, size_t dx, size_t dy, BYTE** memory);
- BOOL CaptureWindow(void);
- void OnIdle(void);
- int WINAPI WinMain(HINSTANCE instance, HINSTANCE, char*, int)
- {
- WNDCLASS wc;
- wc.style = CS_DBLCLKS;
- wc.lpfnWndProc = MainWndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = instance;
- wc.hIcon = 0;
- wc.hCursor = LoadCursor(0, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wc.lpszMenuName = 0;
- wc.lpszClassName = "Fundamental Window";
- if(!RegisterClass(&wc))
- return MessageBox(0, "Could not register main window.", "Error",
- MB_ICONSTOP);
- int x = CW_USEDEFAULT; int w = CW_USEDEFAULT;
- int y = CW_USEDEFAULT; int h = CW_USEDEFAULT;
- HWND window = CreateWindow("Fundamental Window", "Fundamental Window",
- WS_OVERLAPPEDWINDOW, x, y, w, h, 0, 0, instance, 0);
- if(!window) return MessageBox(0, "Could not create main window.", "Error",
- MB_ICONSTOP);
- ShowWindow(window, SW_SHOW);
- UpdateWindow(window);
- MSG msg;
- for(;;) {
- if(PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE)) {
- if(GetMessage(&msg, 0, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else break;
- }
- else
- OnIdle();
- } return (int)msg.wParam;
- }
- LRESULT CALLBACK MainWndProc(HWND window, UINT message, WPARAM wparam,
- LPARAM lparam)
- {
- switch(message) {
- case(WM_CREATE) : {
- // create bitmap
- HDC winDC = GetDC(window);
- bitmap = CreateDibsection24(winDC, 800, 600, &memory);
- ReleaseDC(window, winDC);
- // set window variables
- main_window = window;
- capture = GetDesktopWindow();
- // do default
- return DefWindowProc(window, message, wparam, lparam);
- }
- case(WM_DESTROY) : {
- // destroy bitmap
- DeleteObject(bitmap);
- bitmap = 0;
- // erase window variables
- main_window = 0;
- capture = 0;
- // quit application
- PostQuitMessage(0);
- break;
- }
- } return DefWindowProc(window, message, wparam, lparam);
- }
- HBITMAP CreateDibsection24(HDC winDC, size_t dx, size_t dy, BYTE** memory)
- {
- // bitmap information
- BITMAPINFOHEADER infoHeader;
- infoHeader.biSize = sizeof(infoHeader);
- infoHeader.biWidth = (LONG)dx;
- infoHeader.biHeight = (LONG)dy;
- infoHeader.biPlanes = 1;
- infoHeader.biBitCount = 24;
- infoHeader.biCompression = BI_RGB;
- infoHeader.biSizeImage = 0;
- infoHeader.biXPelsPerMeter = 0;
- infoHeader.biYPelsPerMeter = 0;
- infoHeader.biClrUsed = 0;
- infoHeader.biClrImportant = 0;
- // dibsection information
- BITMAPINFO info;
- info.bmiHeader = infoHeader;
- return CreateDIBSection(winDC, &info, DIB_RGB_COLORS, (void**)memory, 0,
- 0);
- }
- BOOL CaptureWindow(void)
- {
- // bitmap must be a dibsection
- DIBSECTION ds;
- if(!GetObject(bitmap, sizeof(ds), &ds)) return FALSE;
- int bitmap_dx = ds.dsBmih.biWidth;
- int bitmap_dy = ds.dsBmih.biHeight;
- // get window dimensions
- RECT rect;
- GetWindowRect(capture, &rect);
- int window_dx = rect.right - rect.left;
- int window_dy = rect.bottom - rect.top;
- // get window context
- HDC winDC = GetWindowDC(capture);
- if(!winDC) return FALSE;
- // create a memory context to select the dibsection into
- HDC memDC = CreateCompatibleDC(winDC);
- if(!memDC) { ReleaseDC(capture, winDC); return FALSE; }
- // fill dibsection with black
- // then copy the contents of the winDC into the dibsection
- HBRUSH oldbrush = (HBRUSH)SelectObject(memDC, GetStockObject
- (BLACK_BRUSH));
- HBITMAP oldbitmap = (HBITMAP)SelectObject(memDC, bitmap);
- PatBlt(memDC, 0, 0, bitmap_dx, bitmap_dy, PATCOPY);
- BitBlt(memDC, 0, 0, window_dx, window_dy, winDC, 0, 0, SRCCOPY);
- // cleanup contexts
- SelectObject(memDC, oldbrush);
- SelectObject(memDC, oldbitmap);
- DeleteDC(memDC);
- ReleaseDC(capture, winDC);
- return TRUE;
- }
- void OnIdle(void)
- {
- // capture the desktop window
- if(!bitmap) return;
- else CaptureWindow();
- // the pixel position to extract the color for
- int x = 41;
- int y = 32;
- // use GetPixel
- HDC winDC = GetWindowDC(capture);
- COLORREF c1 = GetPixel(winDC, x, y);
- ReleaseDC(capture, winDC);
- // use dibsection memory
- DIBSECTION ds;
- GetObject(bitmap, sizeof(ds), &ds);
- int pitch_bytes = (((24*ds.dsBmih.biWidth + 31) & (~31))/8);
- y = (ds.dsBmih.biHeight - 1) - y;
- int index_b = (pitch_bytes*y + 3*x);
- int index_g = index_b + 1;
- int index_r = index_b + 2;
- COLORREF c2 = RGB(memory[index_r], memory[index_g], memory[index_b]);
- // print colors to string
- char buffer[256];
- wsprintf(buffer, "cGetPixel = (%d,%d,%d) and cDibsection = (%d,%d,%d).",
- GetRValue(c1), GetGValue(c1), GetBValue(c1),
- GetRValue(c2), GetGValue(c2), GetBValue(c2));
- SetWindowText(main_window, buffer);
- }
|
La fonction OnIdle devrait te convenir ---------------
Kyuran's World: Karaoke pour pocket pc, outil de synchro pour sunbird, ...
|