message_window.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2013 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. #ifndef BASE_WIN_MESSAGE_WINDOW_H_
  5. #define BASE_WIN_MESSAGE_WINDOW_H_
  6. #include <string>
  7. #include "base/base_export.h"
  8. #include "base/callback.h"
  9. #include "base/compiler_specific.h"
  10. #include "base/threading/thread_checker.h"
  11. #include "base/win/windows_types.h"
  12. // Protect against windows.h being included before this header.
  13. #undef FindWindow
  14. namespace base {
  15. namespace win {
  16. // Implements a message-only window.
  17. class BASE_EXPORT MessageWindow {
  18. public:
  19. // Used to register a process-wide message window class.
  20. class WindowClass;
  21. // Implement this callback to handle messages received by the message window.
  22. // If the callback returns |false|, the first four parameters are passed to
  23. // DefWindowProc(). Otherwise, |*result| is returned by the window procedure.
  24. using MessageCallback =
  25. base::RepeatingCallback<bool(UINT message,
  26. WPARAM wparam,
  27. LPARAM lparam,
  28. LRESULT* result)>; // NOLINT
  29. MessageWindow();
  30. MessageWindow(const MessageWindow&) = delete;
  31. MessageWindow& operator=(const MessageWindow&) = delete;
  32. ~MessageWindow();
  33. // Creates a message-only window. The incoming messages will be passed by
  34. // |message_callback|. |message_callback| must outlive |this|.
  35. bool Create(MessageCallback message_callback);
  36. // Same as Create() but assigns the name to the created window.
  37. bool CreateNamed(MessageCallback message_callback,
  38. const std::wstring& window_name);
  39. HWND hwnd() const { return window_; }
  40. // Retrieves a handle of the first message-only window with matching
  41. // |window_name|.
  42. static HWND FindWindow(const std::wstring& window_name);
  43. private:
  44. // Give |WindowClass| access to WindowProc().
  45. friend class WindowClass;
  46. // Contains the actual window creation code.
  47. bool DoCreate(MessageCallback message_callback, const wchar_t* window_name);
  48. // Invoked by the OS to process incoming window messages.
  49. static LRESULT CALLBACK WindowProc(HWND hwnd,
  50. UINT message,
  51. WPARAM wparam,
  52. LPARAM lparam);
  53. // Invoked to handle messages received by the window.
  54. MessageCallback message_callback_;
  55. // Handle of the input window.
  56. HWND window_ = nullptr;
  57. THREAD_CHECKER(thread_checker_);
  58. };
  59. } // namespace win
  60. } // namespace base
  61. #endif // BASE_WIN_MESSAGE_WINDOW_H_