G
Guest
Hi everybody. We write in eC++ a .dll function to save, and another to restore a windows region. It works quite well. We did it, because it's not possible directly from .Net CF
The only problem is, it's saving the entire SCREEN not the canvas of the window passed - as a windows handle -. So, if some Window2 is over Window1, saving a region of Window1, in fact, save the whole screen. Then when restoring it show a bitmap, with Window2 overlapping Window1
Here is the code in C++, as you see quite simple. What we are doing wrong ?? .. Is possible to save a Windows regions - not the screen - ?? .. Doesnt' a device context (GetDc(hWnd)) serve to this purpose ?? .. Thank you all ! .. :-
EXPORTA HBITMAP __stdcall SaveWindowRegion(HWND hWnd, int fromX, int fromY, int toX, int toY)
HDC WindowDc, MemDc
HBITMAP hBitmap = 0;
int width, height, err = 0
if (hWnd == NULL)
err = ERR_BAD_WINDOW
if (fromX < 0 || fromY < 0 || toX < 0 || toY < 0)
err = ERR_BAD_PARAMS
if (!err)
width = toX - fromX
height = toY - fromY
WindowDc = GetDC (hWnd)
MemDc = CreateCompatibleDC (WindowDc)
hBitmap = CreateCompatibleBitmap (WindowDc, width, height)
SelectObject (MemDc, hBitmap)
if (!BitBlt (MemDc, 0, 0, width, height, WindowDc, fromX, fromY, SRCCOPY))
DeleteObject (hBitmap)
hBitmap = 0
DeleteDC (MemDc)
ReleaseDC (hWnd, WindowDc)
return hBitmap
EXPORTA int __stdcall RestoreWindowRegion(HWND hWnd, HBITMAP hBitmap, int x, int y)
HDC WindowDc, MemDc
int width, height, ret = 0
if (hWnd == NULL)
ret = ERR_BAD_WINDOW
if (hBitmap == NULL)
ret = ERR_BAD_BITMAP
if (x < 0 || y < 0)
ret = ERR_BAD_PARAMS
if (ret < 0 && ret != ERR_BAD_BITMAP)
DeleteObject (hBitmap)
} else if (!ret)
WindowDc = GetDC (hWnd)
MemDc = CreateCompatibleDC (WindowDc)
SelectObject (MemDc, hBitmap)
width = GetDeviceCaps (MemDc, HORZRES)
height = GetDeviceCaps (MemDc, VERTRES)
if (!BitBlt (WindowDc, x, y, width, height, MemDc, 0, 0, SRCCOPY))
ret = ERR_FATAL
DeleteObject (hBitmap)
DeleteDC (MemDc)
ReleaseDC (hWnd, WindowDc)
return ret
The only problem is, it's saving the entire SCREEN not the canvas of the window passed - as a windows handle -. So, if some Window2 is over Window1, saving a region of Window1, in fact, save the whole screen. Then when restoring it show a bitmap, with Window2 overlapping Window1
Here is the code in C++, as you see quite simple. What we are doing wrong ?? .. Is possible to save a Windows regions - not the screen - ?? .. Doesnt' a device context (GetDc(hWnd)) serve to this purpose ?? .. Thank you all ! .. :-
EXPORTA HBITMAP __stdcall SaveWindowRegion(HWND hWnd, int fromX, int fromY, int toX, int toY)
HDC WindowDc, MemDc
HBITMAP hBitmap = 0;
int width, height, err = 0
if (hWnd == NULL)
err = ERR_BAD_WINDOW
if (fromX < 0 || fromY < 0 || toX < 0 || toY < 0)
err = ERR_BAD_PARAMS
if (!err)
width = toX - fromX
height = toY - fromY
WindowDc = GetDC (hWnd)
MemDc = CreateCompatibleDC (WindowDc)
hBitmap = CreateCompatibleBitmap (WindowDc, width, height)
SelectObject (MemDc, hBitmap)
if (!BitBlt (MemDc, 0, 0, width, height, WindowDc, fromX, fromY, SRCCOPY))
DeleteObject (hBitmap)
hBitmap = 0
DeleteDC (MemDc)
ReleaseDC (hWnd, WindowDc)
return hBitmap
EXPORTA int __stdcall RestoreWindowRegion(HWND hWnd, HBITMAP hBitmap, int x, int y)
HDC WindowDc, MemDc
int width, height, ret = 0
if (hWnd == NULL)
ret = ERR_BAD_WINDOW
if (hBitmap == NULL)
ret = ERR_BAD_BITMAP
if (x < 0 || y < 0)
ret = ERR_BAD_PARAMS
if (ret < 0 && ret != ERR_BAD_BITMAP)
DeleteObject (hBitmap)
} else if (!ret)
WindowDc = GetDC (hWnd)
MemDc = CreateCompatibleDC (WindowDc)
SelectObject (MemDc, hBitmap)
width = GetDeviceCaps (MemDc, HORZRES)
height = GetDeviceCaps (MemDc, VERTRES)
if (!BitBlt (WindowDc, x, y, width, height, MemDc, 0, 0, SRCCOPY))
ret = ERR_FATAL
DeleteObject (hBitmap)
DeleteDC (MemDc)
ReleaseDC (hWnd, WindowDc)
return ret