[CPP]View PlainCopy
- BOOL Savehwndtobmpfile (hwnd hwnd, LPCTSTR lpszpath)
- {
- HWND hdesktop =:: GetDesktopWindow ();
- ASSERT (Hdesktop);
- if (NULL = = hWnd)
- {
- HWnd = Hdesktop;
- }
- Rect rect;
- :: GetWindowRect (HWnd, &rect);
- int nwidht = Rect.right-rect.left;
- int nheight = rect.bottom-rect.top;
- HDC HSRCDC =:: GETWINDOWDC (HWND);
- ASSERT (HSRCDC);
- HDC HMEMDC =:: CreateCompatibleDC (HSRCDC);
- ASSERT (HMEMDC);
- hbitmap hbitmap =:: CreateCompatibleBitmap (HSRCDC, NWIDHT, nheight);
- ASSERT (HBITMAP);
- hbitmap holdbitmap = (hbitmap):: SelectObject (HMEMDC, HBITMAP);
- :: BitBlt (HMEMDC, 0, 0, NWIDHT, nheight, HSRCDC, 0, 0, srccopy);
- BITMAP BITMAP = {0};
- :: GetObject (Hbitmap, sizeof (BITMAP), &bitmap);
- Bitmapinfoheader bi = {0};
- Bitmapfileheader bf = {0};
- CONST int nbitcount = 24;
- Bi.bisize = sizeof (bitmapinfoheader);
- Bi.biwidth = Bitmap.bmwidth;
- Bi.biheight = Bitmap.bmheight;
- Bi.biplanes = 1;
- Bi.bibitcount = Nbitcount;
- Bi.bicompression = Bi_rgb;
- DWORD dwsize = ((Bitmap.bmwidth * nbitcount + +)/+) * 4 * bitmap.bmheight;
- HANDLE Hdib = GlobalAlloc (ghnd, dwsize + sizeof (bitmapinfoheader));
- Lpbitmapinfoheader LPBI = (lpbitmapinfoheader) globallock (HDIB);
- *LPBI = bi;
- :: GetDIBits (HMEMDC, hbitmap, 0, Bitmap.bmheight, (byte*) LPBI + sizeof (Bitmapinfoheader), (bitmapinfo*) LPBI, Dib_rgb_colors);
- Try
- {
- CFile file;
- File. Open (Lpszpath, Cfile::modecreate | Cfile::modewrite);
- Bf.bftype = 0x4d42;
- dwsize + = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader);
- Bf.bfsize = dwsize;
- Bf.bfoffbits = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader);
- File. Write ((byte*) &BF, sizeof (Bitmapfileheader));
- File. Write ((byte*) LPBI, dwsize);
- File. Close ();
- }
- catch (cfileexception* e)
- {
- E->reporterror ();
- E->delete ();
- }
- GlobalUnlock (HDIB);
- GlobalFree (HDIB);
- :: SelectObject (HMEMDC, Holdbitmap);
- ::D eleteobject (HBITMAP);
- ::D Eletedc (HMEMDC);
- :: ReleaseDC (HWnd, HSRCDC);
- return TRUE;
- }
- Calling methods
- HWND hwnd =:: FindWindow (NULL, _t ("XXX"));
- if (hWnd)
- {
- Savehwndtobmpfile (HWnd, _t ("f://12.bmp"));
- }
http://blog.csdn.net/visualeleven/article/details/6202796
http://blog.csdn.net/visualeleven/article/details/6206715
http://blog.csdn.net/visualeleven/article/details/6093484
Window (can specify the HWND window handle) (three articles)