OLD | NEW |
---|---|
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 "app/win_util.h" | 5 #include "app/win_util.h" |
6 | 6 |
7 #include <atlbase.h> | |
8 #include <atlapp.h> | |
9 #include <commdlg.h> | 7 #include <commdlg.h> |
10 #include <dwmapi.h> | 8 #include <dwmapi.h> |
11 #include <propvarutil.h> | 9 #include <propvarutil.h> |
12 #include <shellapi.h> | 10 #include <shellapi.h> |
13 #include <shlobj.h> | 11 #include <shlobj.h> |
14 | 12 |
13 #include <algorithm> | |
14 | |
15 #include "app/l10n_util.h" | 15 #include "app/l10n_util.h" |
16 #include "app/l10n_util_win.h" | 16 #include "app/l10n_util_win.h" |
17 #include "base/base_switches.h" | 17 #include "base/base_switches.h" |
18 #include "base/command_line.h" | 18 #include "base/command_line.h" |
19 #include "base/file_util.h" | 19 #include "base/file_util.h" |
20 #include "base/gfx/gdi_util.h" | 20 #include "base/gfx/gdi_util.h" |
21 #include "base/gfx/png_encoder.h" | 21 #include "base/gfx/png_encoder.h" |
22 #include "base/logging.h" | 22 #include "base/logging.h" |
23 #include "base/native_library.h" | 23 #include "base/native_library.h" |
24 #include "base/registry.h" | 24 #include "base/registry.h" |
25 #include "base/scoped_comptr_win.h" | 25 #include "base/scoped_comptr_win.h" |
26 #include "base/scoped_handle.h" | 26 #include "base/scoped_handle.h" |
27 #include "base/scoped_handle_win.h" | |
27 #include "base/string_util.h" | 28 #include "base/string_util.h" |
28 #include "base/win_util.h" | 29 #include "base/win_util.h" |
29 #include "grit/app_strings.h" | 30 #include "grit/app_strings.h" |
30 #include "net/base/mime_util.h" | 31 #include "net/base/mime_util.h" |
31 | 32 |
32 // Ensure that we pick up this link library. | 33 // Ensure that we pick up this link library. |
33 #pragma comment(lib, "dwmapi.lib") | 34 #pragma comment(lib, "dwmapi.lib") |
34 | 35 |
35 namespace win_util { | 36 namespace win_util { |
36 | 37 |
(...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
683 return !extended_key && | 684 return !extended_key && |
684 ((key_code >= VK_PRIOR && key_code <= VK_DOWN) || // All keys but 5 | 685 ((key_code >= VK_PRIOR && key_code <= VK_DOWN) || // All keys but 5 |
685 // and 0. | 686 // and 0. |
686 (key_code == VK_CLEAR) || // Key 5. | 687 (key_code == VK_CLEAR) || // Key 5. |
687 (key_code == VK_INSERT)); // Key 0. | 688 (key_code == VK_INSERT)); // Key 0. |
688 } | 689 } |
689 | 690 |
690 void GrabWindowSnapshot(HWND window_handle, | 691 void GrabWindowSnapshot(HWND window_handle, |
691 std::vector<unsigned char>* png_representation) { | 692 std::vector<unsigned char>* png_representation) { |
692 // Create a memory DC that's compatible with the window. | 693 // Create a memory DC that's compatible with the window. |
693 CWindowDC window_hdc(window_handle); | 694 HDC window_hdc = GetWindowDC(window_handle); |
694 CDC mem_hdc(::CreateCompatibleDC(window_hdc)); | 695 ScopedHDC mem_hdc(CreateCompatibleDC(window_hdc)); |
695 | 696 |
696 // Create a DIB that's the same size as the window. | 697 // Create a DIB that's the same size as the window. |
697 RECT content_rect = {0, 0, 0, 0}; | 698 RECT content_rect = {0, 0, 0, 0}; |
698 ::GetWindowRect(window_handle, &content_rect); | 699 ::GetWindowRect(window_handle, &content_rect); |
699 content_rect.right++; // Match what PrintWindow wants. | 700 content_rect.right++; // Match what PrintWindow wants. |
700 int width = content_rect.right - content_rect.left; | 701 int width = content_rect.right - content_rect.left; |
701 int height = content_rect.bottom - content_rect.top; | 702 int height = content_rect.bottom - content_rect.top; |
702 BITMAPINFOHEADER hdr; | 703 BITMAPINFOHEADER hdr; |
703 gfx::CreateBitmapHeader(width, height, &hdr); | 704 gfx::CreateBitmapHeader(width, height, &hdr); |
704 unsigned char *bit_ptr = NULL; | 705 unsigned char *bit_ptr = NULL; |
705 CBitmap bitmap(::CreateDIBSection(mem_hdc, | 706 ScopedBitmap bitmap(CreateDIBSection(mem_hdc, |
706 reinterpret_cast<BITMAPINFO*>(&hdr), | 707 reinterpret_cast<BITMAPINFO*>(&hdr), |
707 DIB_RGB_COLORS, | 708 DIB_RGB_COLORS, |
708 reinterpret_cast<void **>(&bit_ptr), | 709 reinterpret_cast<void **>(&bit_ptr), |
709 NULL, 0)); | 710 NULL, 0)); |
710 | 711 |
711 mem_hdc.SelectBitmap(bitmap); | 712 SelectObject(mem_hdc, bitmap); |
712 // Clear the bitmap to white (so that rounded corners on windows | 713 // Clear the bitmap to white (so that rounded corners on windows |
713 // show up on a white background, and strangely-shaped windows | 714 // show up on a white background, and strangely-shaped windows |
714 // look reasonable). Not capturing an alpha mask saves a | 715 // look reasonable). Not capturing an alpha mask saves a |
715 // bit of space. | 716 // bit of space. |
716 mem_hdc.PatBlt(0, 0, width, height, WHITENESS); | 717 PatBlt(mem_hdc, 0, 0, width, height, WHITENESS); |
717 // Grab a copy of the window | 718 // Grab a copy of the window |
718 // First, see if PrintWindow is defined (it's not in Windows 2000). | 719 // First, see if PrintWindow is defined (it's not in Windows 2000). |
719 typedef BOOL (WINAPI *PrintWindowPointer)(HWND, HDC, UINT); | 720 typedef BOOL (WINAPI *PrintWindowPointer)(HWND, HDC, UINT); |
720 PrintWindowPointer print_window = | 721 PrintWindowPointer print_window = |
721 reinterpret_cast<PrintWindowPointer>( | 722 reinterpret_cast<PrintWindowPointer>( |
722 GetProcAddress(GetModuleHandle(L"User32.dll"), "PrintWindow")); | 723 GetProcAddress(GetModuleHandle(L"User32.dll"), "PrintWindow")); |
723 | 724 |
724 // If PrintWindow is defined, use it. It will work on partially | 725 // If PrintWindow is defined, use it. It will work on partially |
725 // obscured windows, and works better for out of process sub-windows. | 726 // obscured windows, and works better for out of process sub-windows. |
726 // Otherwise grab the bits we can get with BitBlt; it's better | 727 // Otherwise grab the bits we can get with BitBlt; it's better |
727 // than nothing and will work fine in the average case (window is | 728 // than nothing and will work fine in the average case (window is |
728 // completely on screen). | 729 // completely on screen). |
729 if (print_window) | 730 if (print_window) |
730 (*print_window)(window_handle, mem_hdc, 0); | 731 (*print_window)(window_handle, mem_hdc, 0); |
731 else | 732 else |
732 mem_hdc.BitBlt(0, 0, width, height, window_hdc, 0, 0, SRCCOPY); | 733 BitBlt(mem_hdc, 0, 0, width, height, window_hdc, 0, 0, SRCCOPY); |
733 | 734 |
734 // We now have a copy of the window contents in a DIB, so | 735 // We now have a copy of the window contents in a DIB, so |
735 // encode it into a useful format for posting to the bug report | 736 // encode it into a useful format for posting to the bug report |
736 // server. | 737 // server. |
737 PNGEncoder::Encode(bit_ptr, PNGEncoder::FORMAT_BGRA, | 738 PNGEncoder::Encode(bit_ptr, PNGEncoder::FORMAT_BGRA, |
738 width, height, width * 4, true, | 739 width, height, width * 4, true, |
739 png_representation); | 740 png_representation); |
741 | |
742 ReleaseDC(window_handle, window_hdc); | |
Peter Kasting
2009/09/03 04:24:48
BTW, we don't have any problems due to destruction
| |
740 } | 743 } |
741 | 744 |
742 bool IsWindowActive(HWND hwnd) { | 745 bool IsWindowActive(HWND hwnd) { |
743 WINDOWINFO info; | 746 WINDOWINFO info; |
744 return ::GetWindowInfo(hwnd, &info) && | 747 return ::GetWindowInfo(hwnd, &info) && |
745 ((info.dwWindowStatus & WS_ACTIVECAPTION) != 0); | 748 ((info.dwWindowStatus & WS_ACTIVECAPTION) != 0); |
746 } | 749 } |
747 | 750 |
748 bool IsReservedName(const std::wstring& filename) { | 751 bool IsReservedName(const std::wstring& filename) { |
749 // This list is taken from the MSDN article "Naming a file" | 752 // This list is taken from the MSDN article "Naming a file" |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
877 if (S_OK == pps->SetValue(PKEY_AppUserModel_ID, pv)) | 880 if (S_OK == pps->SetValue(PKEY_AppUserModel_ID, pv)) |
878 pps->Commit(); | 881 pps->Commit(); |
879 } | 882 } |
880 | 883 |
881 // Cleanup. | 884 // Cleanup. |
882 PropVariantClear(&pv); | 885 PropVariantClear(&pv); |
883 base::UnloadNativeLibrary(shell32_library); | 886 base::UnloadNativeLibrary(shell32_library); |
884 } | 887 } |
885 | 888 |
886 } // namespace win_util | 889 } // namespace win_util |
OLD | NEW |