| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" | |
| 6 | |
| 7 #include "base/win/scoped_gdi_object.h" | |
| 8 #include "base/win/scoped_hdc.h" | |
| 9 #include "base/win/scoped_select_object.h" | |
| 10 #include "ui/gfx/codec/png_codec.h" | |
| 11 #include "ui/gfx/gdi_util.h" | |
| 12 #include "ui/gfx/rect.h" | |
| 13 #include "ui/gfx/size.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 gfx::Rect GetWindowBounds(gfx::NativeWindow window_handle) { | |
| 18 RECT content_rect = {0, 0, 0, 0}; | |
| 19 if (window_handle) { | |
| 20 ::GetWindowRect(window_handle, &content_rect); | |
| 21 } else { | |
| 22 MONITORINFO monitor_info = {}; | |
| 23 monitor_info.cbSize = sizeof(monitor_info); | |
| 24 if (GetMonitorInfo(MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY), | |
| 25 &monitor_info)) { | |
| 26 content_rect = monitor_info.rcMonitor; | |
| 27 } | |
| 28 } | |
| 29 content_rect.right++; // Match what PrintWindow wants. | |
| 30 | |
| 31 return gfx::Rect(content_rect.right - content_rect.left, | |
| 32 content_rect.bottom - content_rect.top); | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 namespace chrome { | |
| 38 namespace internal { | |
| 39 | |
| 40 bool GrabWindowSnapshot(gfx::NativeWindow window_handle, | |
| 41 std::vector<unsigned char>* png_representation, | |
| 42 const gfx::Rect& snapshot_bounds) { | |
| 43 DCHECK(snapshot_bounds.right() <= GetWindowBounds(window_handle).right()); | |
| 44 DCHECK(snapshot_bounds.bottom() <= GetWindowBounds(window_handle).bottom()); | |
| 45 | |
| 46 // Create a memory DC that's compatible with the window. | |
| 47 HDC window_hdc = GetWindowDC(window_handle); | |
| 48 base::win::ScopedCreateDC mem_hdc(CreateCompatibleDC(window_hdc)); | |
| 49 | |
| 50 BITMAPINFOHEADER hdr; | |
| 51 gfx::CreateBitmapHeader(snapshot_bounds.width(), | |
| 52 snapshot_bounds.height(), | |
| 53 &hdr); | |
| 54 unsigned char *bit_ptr = NULL; | |
| 55 base::win::ScopedBitmap bitmap( | |
| 56 CreateDIBSection(mem_hdc, | |
| 57 reinterpret_cast<BITMAPINFO*>(&hdr), | |
| 58 DIB_RGB_COLORS, | |
| 59 reinterpret_cast<void **>(&bit_ptr), | |
| 60 NULL, 0)); | |
| 61 | |
| 62 base::win::ScopedSelectObject select_bitmap(mem_hdc, bitmap); | |
| 63 // Clear the bitmap to white (so that rounded corners on windows | |
| 64 // show up on a white background, and strangely-shaped windows | |
| 65 // look reasonable). Not capturing an alpha mask saves a | |
| 66 // bit of space. | |
| 67 PatBlt(mem_hdc, 0, 0, snapshot_bounds.width(), snapshot_bounds.height(), | |
| 68 WHITENESS); | |
| 69 // Grab a copy of the window | |
| 70 // First, see if PrintWindow is defined (it's not in Windows 2000). | |
| 71 typedef BOOL (WINAPI *PrintWindowPointer)(HWND, HDC, UINT); | |
| 72 PrintWindowPointer print_window = | |
| 73 reinterpret_cast<PrintWindowPointer>( | |
| 74 GetProcAddress(GetModuleHandle(L"User32.dll"), "PrintWindow")); | |
| 75 | |
| 76 // If PrintWindow is defined, use it. It will work on partially | |
| 77 // obscured windows, and works better for out of process sub-windows. | |
| 78 // Otherwise grab the bits we can get with BitBlt; it's better | |
| 79 // than nothing and will work fine in the average case (window is | |
| 80 // completely on screen). Always BitBlt when grabbing the whole screen. | |
| 81 if (snapshot_bounds.origin() == gfx::Point() && print_window && window_handle) | |
| 82 (*print_window)(window_handle, mem_hdc, 0); | |
| 83 else | |
| 84 BitBlt(mem_hdc, 0, 0, snapshot_bounds.width(), snapshot_bounds.height(), | |
| 85 window_hdc, snapshot_bounds.x(), snapshot_bounds.y(), SRCCOPY); | |
| 86 | |
| 87 // We now have a copy of the window contents in a DIB, so | |
| 88 // encode it into a useful format for posting to the bug report | |
| 89 // server. | |
| 90 gfx::PNGCodec::Encode(bit_ptr, gfx::PNGCodec::FORMAT_BGRA, | |
| 91 snapshot_bounds.size(), | |
| 92 snapshot_bounds.width() * 4, true, | |
| 93 std::vector<gfx::PNGCodec::Comment>(), | |
| 94 png_representation); | |
| 95 | |
| 96 ReleaseDC(window_handle, window_hdc); | |
| 97 | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 } // namespace internal | |
| 102 } // namespace chrome | |
| OLD | NEW |