C语言怎么实现截图
答案:2 悬赏:80 手机版
解决时间 2021-03-13 17:23
- 提问者网友:送舟行
- 2021-03-13 01:40
C语言怎么实现截图
最佳答案
- 五星知识达人网友:归鹤鸣
- 2021-03-13 02:23
1、用C语言实现windows程序截图,太麻烦。建议用C++做比较好。
2、截图例程:
#ifndef CAPTUREBLT
#define CAPTUREBLT 0x40000000
#endif
HBITMAP CaptureScreen(LPRECT lpRect)//lpRect为NULL时表示全屏幕截图
{
HDC hdc, hdcMem ;
HBITMAP hemfCopy ;
HWND hwndScr ;
int dstcx, dstcy ;
if(lpRect)
{
dstcx = lpRect->right - lpRect->left ;
dstcy = lpRect->bottom - lpRect->top ;
}
else
{
dstcx = GetSystemMetrics(SM_CXSCREEN) ;
dstcy = GetSystemMetrics(SM_CYSCREEN) ;
}
if (LockWindowUpdate(hwndScr = GetDesktopWindow ()))
{
hdc = GetDCEx (hwndScr, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;
hdcMem = CreateCompatibleDC (hdc) ;
if(NULL==hdcMem)
{
ReleaseDC (hwndScr, hdc) ;
LockWindowUpdate (NULL) ;
return NULL;
}
hemfCopy = CreateCompatibleBitmap (hdc, abs(dstcx), abs(dstcy)) ;
if(NULL==hemfCopy)
{
DeleteDC (hdcMem) ;
ReleaseDC (hwndScr, hdc) ;
LockWindowUpdate (NULL) ;
return NULL;
}
SelectObject (hdcMem, hemfCopy) ;
if(lpRect)
StretchBlt (hdcMem, 0, 0, abs(dstcx), abs(dstcy),
hdc, lpRect->left, lpRect->top, dstcx, dstcy, SRCCOPY|CAPTUREBLT) ;
else
BitBlt (hdcMem, 0, 0, dstcx, dstcy,
hdc, 0, 0, SRCCOPY|CAPTUREBLT) ;
DeleteDC (hdcMem) ;
ReleaseDC (hwndScr, hdc) ;
LockWindowUpdate (NULL) ;
return hemfCopy;
}
return NULL;
}
2、截图例程:
#ifndef CAPTUREBLT
#define CAPTUREBLT 0x40000000
#endif
HBITMAP CaptureScreen(LPRECT lpRect)//lpRect为NULL时表示全屏幕截图
{
HDC hdc, hdcMem ;
HBITMAP hemfCopy ;
HWND hwndScr ;
int dstcx, dstcy ;
if(lpRect)
{
dstcx = lpRect->right - lpRect->left ;
dstcy = lpRect->bottom - lpRect->top ;
}
else
{
dstcx = GetSystemMetrics(SM_CXSCREEN) ;
dstcy = GetSystemMetrics(SM_CYSCREEN) ;
}
if (LockWindowUpdate(hwndScr = GetDesktopWindow ()))
{
hdc = GetDCEx (hwndScr, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;
hdcMem = CreateCompatibleDC (hdc) ;
if(NULL==hdcMem)
{
ReleaseDC (hwndScr, hdc) ;
LockWindowUpdate (NULL) ;
return NULL;
}
hemfCopy = CreateCompatibleBitmap (hdc, abs(dstcx), abs(dstcy)) ;
if(NULL==hemfCopy)
{
DeleteDC (hdcMem) ;
ReleaseDC (hwndScr, hdc) ;
LockWindowUpdate (NULL) ;
return NULL;
}
SelectObject (hdcMem, hemfCopy) ;
if(lpRect)
StretchBlt (hdcMem, 0, 0, abs(dstcx), abs(dstcy),
hdc, lpRect->left, lpRect->top, dstcx, dstcy, SRCCOPY|CAPTUREBLT) ;
else
BitBlt (hdcMem, 0, 0, dstcx, dstcy,
hdc, 0, 0, SRCCOPY|CAPTUREBLT) ;
DeleteDC (hdcMem) ;
ReleaseDC (hwndScr, hdc) ;
LockWindowUpdate (NULL) ;
return hemfCopy;
}
return NULL;
}
全部回答
- 1楼网友:孤独的牧羊人
- 2021-03-13 03:57
// MSDN 一下,GDI接口,GetSystemMetrics
#ifndef CAPTUREBLT
#define CAPTUREBLT 0x40000000
#endif
HBITMAP CaptureScreen(LPRECT lpRect)//lpRect为NULL时表示全屏幕截图
{
HDC hdc, hdcMem ;
HBITMAP hemfCopy ;
HWND hwndScr ;
int dstcx, dstcy ;
if(lpRect)
{
dstcx = lpRect->right - lpRect->left ;
dstcy = lpRect->bottom - lpRect->top ;
}
else
{
dstcx = GetSystemMetrics(SM_CXSCREEN) ;
dstcy = GetSystemMetrics(SM_CYSCREEN) ;
}
if (LockWindowUpdate(hwndScr = GetDesktopWindow ()))
{
hdc = GetDCEx (hwndScr, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;
hdcMem = CreateCompatibleDC (hdc) ;
if(NULL==hdcMem)
{
ReleaseDC (hwndScr, hdc) ;
LockWindowUpdate (NULL) ;
return NULL;
}
hemfCopy = CreateCompatibleBitmap (hdc, abs(dstcx), abs(dstcy)) ;
if(NULL==hemfCopy)
{
DeleteDC (hdcMem) ;
ReleaseDC (hwndScr, hdc) ;
LockWindowUpdate (NULL) ;
return NULL;
}
SelectObject (hdcMem, hemfCopy) ;
if(lpRect)
StretchBlt (hdcMem, 0, 0, abs(dstcx), abs(dstcy),
hdc, lpRect->left, lpRect->top, dstcx, dstcy, SRCCOPY|CAPTUREBLT) ;
else
BitBlt (hdcMem, 0, 0, dstcx, dstcy,
hdc, 0, 0, SRCCOPY|CAPTUREBLT) ;
DeleteDC (hdcMem) ;
ReleaseDC (hwndScr, hdc) ;
LockWindowUpdate (NULL) ;
return hemfCopy;
}
return NULL;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯