message_window.h 2.3 KB

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