屏幕及窗口拷贝
2009-10-20 10:30
148 查看
以下来自于:http://www.codeguru.com/forum/showthread.php?t=244907
#define PW_WINDOW 9001
#define PW_CLIENT 9002
HBITMAP CMyProgramDlg::CopyScreenToBitmap(LPRECT lpRect)
{
HDC hScrDC, hMemDC; // screen DC and memory DC
int nX, nY, nX2, nY2; // coordinates of rectangle to grab
int nWidth, nHeight; // DIB width and height
int xScrn, yScrn; // screen resolution
HGDIOBJ hOldBitmap , hBitmap;
// check for an empty rectangle
if (IsRectEmpty(lpRect))
return NULL;
// create a DC for the screen and create
// a memory DC compatible to screen DC
hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
hMemDC = CreateCompatibleDC(hScrDC); // get points of rectangle to grab
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom; // get screen resolution
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
//make sure bitmap rectangle is visible
if (nX < 0)
nX = 0;
if (nY < 0)
nY = 0;
if (nX2 > xScrn)
nX2 = xScrn;
if (nY2 > yScrn)
nY2 = yScrn;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
// create a bitmap compatible with the screen DC
hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
// select new bitmap into memory DC
hOldBitmap = SelectObject (hMemDC, hBitmap);
// bitblt screen DC to memory DC
BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
// select old bitmap back into memory DC and get handle to
// bitmap of the screen
hBitmap = SelectObject(hMemDC, hOldBitmap);
// clean up
DeleteDC(hScrDC);
DeleteDC(hMemDC);
// return handle to the bitmap
return (HBITMAP)hBitmap;
}
HBITMAP CYourProgramDlg::CopyWindowToBitmap(CWnd* wnd , HWND hWnd, WORD fPrintArea)
{
HBITMAP hBitmap = NULL; // handle to device-dependent bitmap
// check for a valid window handle
if (!hWnd)
return NULL;
RECT rectWnd;
::GetWindowRect(hWnd, &rectWnd);
switch (fPrintArea)
{
case PW_WINDOW: // copy entire window
{
// get the window rectangle
// get the bitmap of that window by calling
// CopyScreenToBitmap and passing it the window rect
// GetWindowRect(&rectWnd);
hBitmap = CopyScreenToBitmap(&rectWnd);
break;
}
case PW_CLIENT: // copy client area
{
RECT rectClient;
POINT pt1, pt2; // get client dimensions
wnd->GetClientRect(&rectClient); // convert client coords to screen coords
pt1.x = rectClient.left;
pt1.y = rectClient.top;
pt2.x = rectClient.right;
pt2.y = rectClient.bottom;
wnd->ClientToScreen(&pt1);
wnd->ClientToScreen(&pt2);
rectClient.left = pt1.x;
rectClient.top = pt1.y;
rectClient.right = pt2.x;
rectClient.bottom = pt2.y;
// get the bitmap of the client area by calling
// CopyScreenToBitmap and passing it the client rect
hBitmap = CopyScreenToBitmap(&rectClient);
break;
}
default: // invalid print area
return NULL;
} // return handle to the bitmap
return hBitmap;
}A good usage is :
Code:
HBITMAP hBitmap = CopyWindowToBitmap(this,GetDesktopWindow()->m_hWnd,PW_
#define PW_WINDOW 9001
#define PW_CLIENT 9002
HBITMAP CMyProgramDlg::CopyScreenToBitmap(LPRECT lpRect)
{
HDC hScrDC, hMemDC; // screen DC and memory DC
int nX, nY, nX2, nY2; // coordinates of rectangle to grab
int nWidth, nHeight; // DIB width and height
int xScrn, yScrn; // screen resolution
HGDIOBJ hOldBitmap , hBitmap;
// check for an empty rectangle
if (IsRectEmpty(lpRect))
return NULL;
// create a DC for the screen and create
// a memory DC compatible to screen DC
hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
hMemDC = CreateCompatibleDC(hScrDC); // get points of rectangle to grab
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom; // get screen resolution
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
//make sure bitmap rectangle is visible
if (nX < 0)
nX = 0;
if (nY < 0)
nY = 0;
if (nX2 > xScrn)
nX2 = xScrn;
if (nY2 > yScrn)
nY2 = yScrn;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
// create a bitmap compatible with the screen DC
hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
// select new bitmap into memory DC
hOldBitmap = SelectObject (hMemDC, hBitmap);
// bitblt screen DC to memory DC
BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
// select old bitmap back into memory DC and get handle to
// bitmap of the screen
hBitmap = SelectObject(hMemDC, hOldBitmap);
// clean up
DeleteDC(hScrDC);
DeleteDC(hMemDC);
// return handle to the bitmap
return (HBITMAP)hBitmap;
}
HBITMAP CYourProgramDlg::CopyWindowToBitmap(CWnd* wnd , HWND hWnd, WORD fPrintArea)
{
HBITMAP hBitmap = NULL; // handle to device-dependent bitmap
// check for a valid window handle
if (!hWnd)
return NULL;
RECT rectWnd;
::GetWindowRect(hWnd, &rectWnd);
switch (fPrintArea)
{
case PW_WINDOW: // copy entire window
{
// get the window rectangle
// get the bitmap of that window by calling
// CopyScreenToBitmap and passing it the window rect
// GetWindowRect(&rectWnd);
hBitmap = CopyScreenToBitmap(&rectWnd);
break;
}
case PW_CLIENT: // copy client area
{
RECT rectClient;
POINT pt1, pt2; // get client dimensions
wnd->GetClientRect(&rectClient); // convert client coords to screen coords
pt1.x = rectClient.left;
pt1.y = rectClient.top;
pt2.x = rectClient.right;
pt2.y = rectClient.bottom;
wnd->ClientToScreen(&pt1);
wnd->ClientToScreen(&pt2);
rectClient.left = pt1.x;
rectClient.top = pt1.y;
rectClient.right = pt2.x;
rectClient.bottom = pt2.y;
// get the bitmap of the client area by calling
// CopyScreenToBitmap and passing it the client rect
hBitmap = CopyScreenToBitmap(&rectClient);
break;
}
default: // invalid print area
return NULL;
} // return handle to the bitmap
return hBitmap;
}A good usage is :
Code:
HBITMAP hBitmap = CopyWindowToBitmap(this,GetDesktopWindow()->m_hWnd,PW_
相关文章推荐
- vmware ubuntu14.04虚拟机不能正常拷贝文件到windows且不能自适应虚拟机屏幕窗口自动变化的解决办法
- wince窗口 屏幕拷贝为位图
- wince窗口 屏幕拷贝为位图
- vmware ubuntu14.04虚拟机不能正常拷贝文件到windows且不能自适应虚拟机屏幕窗口自动变化的解决办法
- vmware ubuntu14.04虚拟机不能正常拷贝文件到windows且不能自适应虚拟机屏幕窗口自动变化的解决办法
- JS获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
- 玩转 Javascript 弹出窗口, 居中与居角扩大窗口至屏幕最大值 By shawl.qiu
- Jquery 弹出窗口插件开发-在屏幕固定位置停留[原创]
- MFC实现类似qq的在屏幕右下角动画显示消息窗口 .
- JS获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
- 使用MoveWindow()函数来移动指定句柄的窗口在屏幕中的显示位置及这个窗口的大小
- 使用CToolTipCtrl让鼠标提示窗口跟着鼠标在屏幕上移动
- 自定义拷贝粘贴窗口
- C#屏幕拷贝源码
- 鼠标拖拽窗口效果(不允许拖出屏幕的可视区域)
- Java之获取屏幕大小并将窗口居中显示!...
- 自定义拷贝,粘贴窗口
- SWT编写界面窗口时让窗口处于屏幕中间
- EVC中重绘窗口时屏幕会闪动的解决方法
- 解决Duilib创建的win32窗口拖到屏幕上边缘自动最大化