This source file includes following definitions.
- GetWindowBounds
- GrabHwndSnapshot
- GrabViewSnapshot
- GrabWindowSnapshot
- GrapWindowSnapshotAsync
- GrabViewSnapshotAsync
- GrabWindowSnapshotAsync
#include "ui/snapshot/snapshot_win.h"
#include "base/callback.h"
#include "base/win/scoped_gdi_object.h"
#include "base/win/scoped_hdc.h"
#include "base/win/scoped_select_object.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/gdi_util.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"
#include "ui/snapshot/snapshot.h"
namespace {
gfx::Rect GetWindowBounds(HWND window_handle) {
RECT content_rect = {0, 0, 0, 0};
if (window_handle) {
::GetWindowRect(window_handle, &content_rect);
} else {
MONITORINFO monitor_info = {};
monitor_info.cbSize = sizeof(monitor_info);
if (GetMonitorInfo(MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY),
&monitor_info)) {
content_rect = monitor_info.rcMonitor;
}
}
content_rect.right++;
return gfx::Rect(content_rect.right - content_rect.left,
content_rect.bottom - content_rect.top);
}
}
namespace ui {
namespace internal {
bool GrabHwndSnapshot(HWND window_handle,
const gfx::Rect& snapshot_bounds,
std::vector<unsigned char>* png_representation) {
DCHECK(snapshot_bounds.right() <= GetWindowBounds(window_handle).right());
DCHECK(snapshot_bounds.bottom() <= GetWindowBounds(window_handle).bottom());
HDC window_hdc = GetWindowDC(window_handle);
base::win::ScopedCreateDC mem_hdc(CreateCompatibleDC(window_hdc));
BITMAPINFOHEADER hdr;
gfx::CreateBitmapHeader(snapshot_bounds.width(),
snapshot_bounds.height(),
&hdr);
unsigned char *bit_ptr = NULL;
base::win::ScopedBitmap bitmap(
CreateDIBSection(mem_hdc,
reinterpret_cast<BITMAPINFO*>(&hdr),
DIB_RGB_COLORS,
reinterpret_cast<void **>(&bit_ptr),
NULL, 0));
base::win::ScopedSelectObject select_bitmap(mem_hdc, bitmap);
PatBlt(mem_hdc, 0, 0, snapshot_bounds.width(), snapshot_bounds.height(),
WHITENESS);
typedef BOOL (WINAPI *PrintWindowPointer)(HWND, HDC, UINT);
PrintWindowPointer print_window =
reinterpret_cast<PrintWindowPointer>(
GetProcAddress(GetModuleHandle(L"User32.dll"), "PrintWindow"));
if (snapshot_bounds.origin() == gfx::Point() && print_window && window_handle)
(*print_window)(window_handle, mem_hdc, 0);
else
BitBlt(mem_hdc, 0, 0, snapshot_bounds.width(), snapshot_bounds.height(),
window_hdc, snapshot_bounds.x(), snapshot_bounds.y(), SRCCOPY);
gfx::PNGCodec::Encode(bit_ptr, gfx::PNGCodec::FORMAT_BGRA,
snapshot_bounds.size(),
snapshot_bounds.width() * 4, true,
std::vector<gfx::PNGCodec::Comment>(),
png_representation);
ReleaseDC(window_handle, window_hdc);
return true;
}
}
#if !defined(USE_AURA)
bool GrabViewSnapshot(gfx::NativeView view_handle,
std::vector<unsigned char>* png_representation,
const gfx::Rect& snapshot_bounds) {
return GrabWindowSnapshot(view_handle, png_representation, snapshot_bounds);
}
bool GrabWindowSnapshot(gfx::NativeWindow window_handle,
std::vector<unsigned char>* png_representation,
const gfx::Rect& snapshot_bounds) {
DCHECK(window_handle);
return internal::GrabHwndSnapshot(window_handle, snapshot_bounds,
png_representation);
}
void GrapWindowSnapshotAsync(
gfx::NativeWindow window,
const gfx::Rect& snapshot_bounds,
const gfx::Size& target_size,
scoped_refptr<base::TaskRunner> background_task_runner,
GrabWindowSnapshotAsyncCallback callback) {
callback.Run(gfx::Image());
}
void GrabViewSnapshotAsync(
gfx::NativeView view,
const gfx::Rect& source_rect,
scoped_refptr<base::TaskRunner> background_task_runner,
const GrabWindowSnapshotAsyncPNGCallback& callback) {
callback.Run(scoped_refptr<base::RefCountedBytes>());
}
void GrabWindowSnapshotAsync(
gfx::NativeWindow window,
const gfx::Rect& source_rect,
scoped_refptr<base::TaskRunner> background_task_runner,
const GrabWindowSnapshotAsyncPNGCallback& callback) {
callback.Run(scoped_refptr<base::RefCountedBytes>());
}
#endif
}