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 | |
39 bool GrabWindowSnapshotImpl(gfx::NativeWindow window_handle, | |
40 std::vector<unsigned char>* png_representation, | |
41 const gfx::Rect& snapshot_bounds) { | |
42 DCHECK(snapshot_bounds.right() <= GetWindowBounds(window_handle).right()); | |
43 DCHECK(snapshot_bounds.bottom() <= GetWindowBounds(window_handle).bottom()); | |
44 | |
45 // Create a memory DC that's compatible with the window. | |
46 HDC window_hdc = GetWindowDC(window_handle); | |
47 base::win::ScopedCreateDC mem_hdc(CreateCompatibleDC(window_hdc)); | |
48 | |
49 BITMAPINFOHEADER hdr; | |
50 gfx::CreateBitmapHeader(snapshot_bounds.width(), | |
51 snapshot_bounds.height(), | |
52 &hdr); | |
53 unsigned char *bit_ptr = NULL; | |
54 base::win::ScopedBitmap bitmap( | |
55 CreateDIBSection(mem_hdc, | |
56 reinterpret_cast<BITMAPINFO*>(&hdr), | |
57 DIB_RGB_COLORS, | |
58 reinterpret_cast<void **>(&bit_ptr), | |
59 NULL, 0)); | |
60 | |
61 base::win::ScopedSelectObject select_bitmap(mem_hdc, bitmap); | |
62 // Clear the bitmap to white (so that rounded corners on windows | |
63 // show up on a white background, and strangely-shaped windows | |
64 // look reasonable). Not capturing an alpha mask saves a | |
65 // bit of space. | |
66 PatBlt(mem_hdc, 0, 0, snapshot_bounds.width(), snapshot_bounds.height(), | |
67 WHITENESS); | |
68 // Grab a copy of the window | |
69 // First, see if PrintWindow is defined (it's not in Windows 2000). | |
70 typedef BOOL (WINAPI *PrintWindowPointer)(HWND, HDC, UINT); | |
71 PrintWindowPointer print_window = | |
72 reinterpret_cast<PrintWindowPointer>( | |
73 GetProcAddress(GetModuleHandle(L"User32.dll"), "PrintWindow")); | |
74 | |
75 // If PrintWindow is defined, use it. It will work on partially | |
76 // obscured windows, and works better for out of process sub-windows. | |
77 // Otherwise grab the bits we can get with BitBlt; it's better | |
78 // than nothing and will work fine in the average case (window is | |
79 // completely on screen). Always BitBlt when grabbing the whole screen. | |
80 if (snapshot_bounds.origin() == gfx::Point() && print_window && window_handle) | |
81 (*print_window)(window_handle, mem_hdc, 0); | |
82 else | |
83 BitBlt(mem_hdc, 0, 0, snapshot_bounds.width(), snapshot_bounds.height(), | |
84 window_hdc, snapshot_bounds.x(), snapshot_bounds.y(), SRCCOPY); | |
85 | |
86 // We now have a copy of the window contents in a DIB, so | |
87 // encode it into a useful format for posting to the bug report | |
88 // server. | |
89 gfx::PNGCodec::Encode(bit_ptr, gfx::PNGCodec::FORMAT_BGRA, | |
90 snapshot_bounds.size(), | |
91 snapshot_bounds.width() * 4, true, | |
92 std::vector<gfx::PNGCodec::Comment>(), | |
93 png_representation); | |
94 | |
95 ReleaseDC(window_handle, window_hdc); | |
96 | |
97 return true; | |
98 } | |
99 | |
100 } // namespace chrome | |
OLD | NEW |