前一篇介绍了如何在应用程序中操作FrameBuffer,实际上得到FrameBuffer后,对于我们来说截取屏幕就是很容易的事情了,完全可以绕开GDI,并且效率要比使用GDI高。
1
BITMAPINFO bi;
2 BITMAPFILEHEADER bmfHdr;
3 HANDLE hFile;
4 DWORD dwWritten;
5
6 ZeroMemory( & bi, sizeof (BITMAPINFO));
7 bi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
8 bi.bmiHeader.biWidth = 800 ;
9 bi.bmiHeader.biHeight = - 480 ;
10 bi.bmiHeader.biPlanes = 1 ;
11 bi.bmiHeader.biBitCount = 32 ;
12 bi.bmiHeader.biCompression = BI_RGB;
13
14 ZeroMemory( & bmfHdr, sizeof (BITMAPFILEHEADER));
15 bmfHdr.bfType = 0x4d42 ;
16 bmfHdr.bfOffBits = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER);
17 bmfHdr.bfSize = bmfHdr.bfOffBits + 800 * 480 * 4 ;
18
19 hFile = CreateFile(_T( " \\1.bmp " ), GENERIC_WRITE, 0 , NULL, CREATE_ALWAYS,NULL, NULL);
20 if (INVALID_HANDLE_VALUE != hFile)
21 {
22 WriteFile(hFile, (LPSTR) & bmfHdr, sizeof (BITMAPFILEHEADER), & dwWritten, NULL);
23 WriteFile(hFile, (LPSTR) & (bi.bmiHeader), sizeof (BITMAPINFOHEADER), & dwWritten, NULL);
24 WriteFile(hFile, (LPSTR)pFrameBuffer, 800 * 480 * 4 , & dwWritten, NULL);
25 CloseHandle(hFile);
26 }
2 BITMAPFILEHEADER bmfHdr;
3 HANDLE hFile;
4 DWORD dwWritten;
5
6 ZeroMemory( & bi, sizeof (BITMAPINFO));
7 bi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
8 bi.bmiHeader.biWidth = 800 ;
9 bi.bmiHeader.biHeight = - 480 ;
10 bi.bmiHeader.biPlanes = 1 ;
11 bi.bmiHeader.biBitCount = 32 ;
12 bi.bmiHeader.biCompression = BI_RGB;
13
14 ZeroMemory( & bmfHdr, sizeof (BITMAPFILEHEADER));
15 bmfHdr.bfType = 0x4d42 ;
16 bmfHdr.bfOffBits = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER);
17 bmfHdr.bfSize = bmfHdr.bfOffBits + 800 * 480 * 4 ;
18
19 hFile = CreateFile(_T( " \\1.bmp " ), GENERIC_WRITE, 0 , NULL, CREATE_ALWAYS,NULL, NULL);
20 if (INVALID_HANDLE_VALUE != hFile)
21 {
22 WriteFile(hFile, (LPSTR) & bmfHdr, sizeof (BITMAPFILEHEADER), & dwWritten, NULL);
23 WriteFile(hFile, (LPSTR) & (bi.bmiHeader), sizeof (BITMAPINFOHEADER), & dwWritten, NULL);
24 WriteFile(hFile, (LPSTR)pFrameBuffer, 800 * 480 * 4 , & dwWritten, NULL);
25 CloseHandle(hFile);
26 }
以上这段代码在800*480*24位色的WinCE系统上测试,效果不错。当然,通过这种方法写出来的程序只能是专用的,在其他的平台上就不灵了。