Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

c++ - How to capture the screen with the "Tool Tips"?

I am using GDI to capture the screen, and I have noticed that the "Tool Tips" are not included in the screenshot. This is my basic code:

HDC hdcDesk = GetDC(0);

HDC hdcMem = CreateCompatibleDC(hdcDesk);
HBITMAP hbmMem = CreateCompatibleBitmap(hdcDesk, 1920, 1080);
SelectObject(hdcMem, hbmMem);

StretchBlt(hdcMem, 0, 0, 1920, 1080, hdcDesk, 0, 0, 1920, 1080, SRCCOPY);

// Now save the bitmap...

Can this be fixed, or should I use another approach to capture the screen (other than GDI)?


Edit:

This is a screenshot that I took that does not display the Tool Tip.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update: added CAPTUREBLT as suggested by Alex K., Adrian McCarthy et al.

I can't reproduce the same problem. If you succeed in taking screen shot of desktop then everything should be there! Try this code instead. Note the 3 second wait is supposed to give time to manually activate a tool tip.

SetProcessDPIAware();
HDC hdc = GetDC(HWND_DESKTOP);
RECT rc; GetWindowRect(GetDesktopWindow(), &rc);
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;

HBITMAP hbitmap = CreateCompatibleBitmap(hdc, width, height);
HDC memdc = CreateCompatibleDC(hdc);
HGDIOBJ oldbmp = SelectObject(memdc, hbitmap);
BitBlt(memdc, 0, 0, width, height, hdc, 0, 0, CAPTUREBLT | SRCCOPY);

WORD bpp = 24; //save 24-bit bitmap
DWORD size = ((width * bpp + 31) / 32) * 4 * height;
BITMAPFILEHEADER filehdr = { 'MB', 54 + size, 0, 0, 54 };
BITMAPINFOHEADER infohdr = { 40, width, height, 1, bpp };
BYTE* pix = malloc(size); 
GetDIBits(hdc, hbitmap, 0, height, pix, (BITMAPINFO*)&infohdr, DIB_RGB_COLORS);

FILE* fout = fopen("c:\test\_bmp.bmp", "wb"); 
if (fout) //save to file
{
    fwrite(&filehdr, sizeof(filehdr), 1, fout);
    fwrite(&infohdr, sizeof(infohdr), 1, fout);
    fwrite(pix, 1, size, fout);
    fclose(fout);
}

//cleanup
free(pix);
SelectObject(memdc, oldbmp);
DeleteObject(memdc);
DeleteObject(hbitmap);
ReleaseDC(HWND_DESKTOP, hdc);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...