Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: ui/snapshot/snapshot_win.cc

Issue 13926006: Add ui::GrabDesktopSnapshot for Windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« ui/snapshot/snapshot.h ('K') | « ui/snapshot/snapshot.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/snapshot/snapshot.h" 5 #include "ui/snapshot/snapshot.h"
6 6
7 #include "base/win/scoped_gdi_object.h" 7 #include "base/win/scoped_gdi_object.h"
8 #include "base/win/scoped_hdc.h" 8 #include "base/win/scoped_hdc.h"
9 #include "base/win/scoped_select_object.h" 9 #include "base/win/scoped_select_object.h"
10 #include "ui/gfx/codec/png_codec.h" 10 #include "ui/gfx/codec/png_codec.h"
11 #include "ui/gfx/gdi_util.h" 11 #include "ui/gfx/gdi_util.h"
12 #include "ui/gfx/rect.h" 12 #include "ui/gfx/rect.h"
13 #include "ui/gfx/size.h" 13 #include "ui/gfx/size.h"
14 14
15 namespace { 15 namespace {
16 16
17 gfx::Rect GetWindowBounds(gfx::NativeWindow window_handle) { 17 gfx::Rect GetWindowBounds(HWND window_handle) {
18 RECT content_rect = {0, 0, 0, 0}; 18 RECT content_rect = {0, 0, 0, 0};
19 if (window_handle) { 19 if (window_handle) {
20 ::GetWindowRect(window_handle, &content_rect); 20 ::GetWindowRect(window_handle, &content_rect);
21 } else { 21 } else {
22 MONITORINFO monitor_info = {}; 22 MONITORINFO monitor_info = {};
23 monitor_info.cbSize = sizeof(monitor_info); 23 monitor_info.cbSize = sizeof(monitor_info);
24 if (GetMonitorInfo(MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY), 24 if (GetMonitorInfo(MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY),
25 &monitor_info)) { 25 &monitor_info)) {
26 content_rect = monitor_info.rcMonitor; 26 content_rect = monitor_info.rcMonitor;
27 } 27 }
28 } 28 }
29 content_rect.right++; // Match what PrintWindow wants. 29 content_rect.right++; // Match what PrintWindow wants.
30 30
31 return gfx::Rect(content_rect.right - content_rect.left, 31 return gfx::Rect(content_rect.right - content_rect.left,
32 content_rect.bottom - content_rect.top); 32 content_rect.bottom - content_rect.top);
33 } 33 }
34 34
35 } // namespace 35 bool GrabWindowSnapshotImpl(HWND window_handle,
36 36 std::vector<unsigned char>* png_representation,
37 namespace ui { 37 const gfx::Rect& snapshot_bounds) {
38
39 bool GrabViewSnapshot(gfx::NativeView view_handle,
40 std::vector<unsigned char>* png_representation,
41 const gfx::Rect& snapshot_bounds) {
42 return GrabWindowSnapshot(view_handle, png_representation, snapshot_bounds);
43 }
44
45 bool GrabWindowSnapshot(gfx::NativeWindow window_handle,
46 std::vector<unsigned char>* png_representation,
47 const gfx::Rect& snapshot_bounds) {
48 DCHECK(snapshot_bounds.right() <= GetWindowBounds(window_handle).right()); 38 DCHECK(snapshot_bounds.right() <= GetWindowBounds(window_handle).right());
49 DCHECK(snapshot_bounds.bottom() <= GetWindowBounds(window_handle).bottom()); 39 DCHECK(snapshot_bounds.bottom() <= GetWindowBounds(window_handle).bottom());
50 40
51 // Create a memory DC that's compatible with the window. 41 // Create a memory DC that's compatible with the window.
52 HDC window_hdc = GetWindowDC(window_handle); 42 HDC window_hdc = GetWindowDC(window_handle);
53 base::win::ScopedCreateDC mem_hdc(CreateCompatibleDC(window_hdc)); 43 base::win::ScopedCreateDC mem_hdc(CreateCompatibleDC(window_hdc));
54 44
55 BITMAPINFOHEADER hdr; 45 BITMAPINFOHEADER hdr;
56 gfx::CreateBitmapHeader(snapshot_bounds.width(), 46 gfx::CreateBitmapHeader(snapshot_bounds.width(),
57 snapshot_bounds.height(), 47 snapshot_bounds.height(),
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 snapshot_bounds.size(), 86 snapshot_bounds.size(),
97 snapshot_bounds.width() * 4, true, 87 snapshot_bounds.width() * 4, true,
98 std::vector<gfx::PNGCodec::Comment>(), 88 std::vector<gfx::PNGCodec::Comment>(),
99 png_representation); 89 png_representation);
100 90
101 ReleaseDC(window_handle, window_hdc); 91 ReleaseDC(window_handle, window_hdc);
102 92
103 return true; 93 return true;
104 } 94 }
105 95
96 } // namespace
97
98 namespace ui {
99
100 #if !defined(USE_AURA)
101
102 bool GrabViewSnapshot(gfx::NativeView view_handle,
103 std::vector<unsigned char>* png_representation,
104 const gfx::Rect& snapshot_bounds) {
105 return GrabWindowSnapshot(view_handle, png_representation, snapshot_bounds);
106 }
107
108 bool GrabWindowSnapshot(gfx::NativeWindow window_handle,
109 std::vector<unsigned char>* png_representation,
110 const gfx::Rect& snapshot_bounds) {
111 DCHECK(window_handle);
112 return GrabWindowSnapshotImpl(window_handle, png_representation,
113 snapshot_bounds);
114 }
115
116 #endif // !defined(USE_AURA)
117
118 bool GrabDesktopSnapshot(std::vector<unsigned char>* png_representation,
119 const gfx::Rect& snapshot_bounds) {
120 return GrabWindowSnapshotImpl(NULL, png_representation, snapshot_bounds);
121 }
122
106 } // namespace ui 123 } // namespace ui
OLDNEW
« ui/snapshot/snapshot.h ('K') | « ui/snapshot/snapshot.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698