aboutsummaryrefslogtreecommitdiff
path: root/src/win/native/native_window.cpp
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-11 23:27:16 +0800
committer杨宇千 <crupest@outlook.com>2019-08-11 23:27:16 +0800
commit86e776eaebf7c45a269001ca7da0dfafba069d0a (patch)
tree64c0544d9839562a6fc6d08ea00682a94c5d222b /src/win/native/native_window.cpp
parent99a322a6badf5b6d95be4944e80d92fc1cb2589e (diff)
downloadcru-86e776eaebf7c45a269001ca7da0dfafba069d0a.tar.gz
cru-86e776eaebf7c45a269001ca7da0dfafba069d0a.tar.bz2
cru-86e776eaebf7c45a269001ca7da0dfafba069d0a.zip
...
Diffstat (limited to 'src/win/native/native_window.cpp')
-rw-r--r--src/win/native/native_window.cpp49
1 files changed, 37 insertions, 12 deletions
diff --git a/src/win/native/native_window.cpp b/src/win/native/native_window.cpp
index fd29f9c5..5da121cf 100644
--- a/src/win/native/native_window.cpp
+++ b/src/win/native/native_window.cpp
@@ -13,6 +13,10 @@
#include <windowsx.h>
namespace cru::platform::native::win {
+inline Point PiToDip(const POINT& pi_point) {
+ return Point(PixelToDipX(pi_point.x), PixelToDipY(pi_point.y));
+}
+
WinNativeWindow::WinNativeWindow(WinUiApplication* application,
std::shared_ptr<WindowClass> window_class,
DWORD window_style, WinNativeWindow* parent) {
@@ -36,8 +40,8 @@ WinNativeWindow::WinNativeWindow(WinUiApplication* application,
window_manager->RegisterWindow(hwnd_, this);
- window_render_target_.reset(
- new WindowRenderTarget(graph::win::direct::DirectGraphFactory::GetInstance(), hwnd_));
+ window_render_target_.reset(new WindowRenderTarget(
+ graph::win::direct::DirectGraphFactory::GetInstance(), hwnd_));
}
WinNativeWindow::~WinNativeWindow() {
@@ -70,8 +74,7 @@ Size WinNativeWindow::GetClientSize() {
if (!IsValid()) return Size{};
const auto pixel_rect = GetClientRectPixel();
- return Size(PixelToDipX(pixel_rect.right),
- PixelToDipY(pixel_rect.bottom));
+ return Size(PixelToDipX(pixel_rect.right), PixelToDipY(pixel_rect.bottom));
}
void WinNativeWindow::SetClientSize(const Size& size) {
@@ -104,8 +107,7 @@ Rect WinNativeWindow::GetWindowRect() {
throw Win32Error(::GetLastError(), "Failed to invoke GetWindowRect.");
return Rect::FromVertices(PixelToDipX(rect.left), PixelToDipY(rect.top),
- PixelToDipX(rect.right),
- PixelToDipY(rect.bottom));
+ PixelToDipX(rect.right), PixelToDipY(rect.bottom));
}
void WinNativeWindow::SetWindowRect(const Rect& rect) {
@@ -117,6 +119,34 @@ void WinNativeWindow::SetWindowRect(const Rect& rect) {
}
}
+Point WinNativeWindow::GetMousePosition() {
+ if (IsValid()) {
+ POINT p;
+ if (!::GetCursorPos(&p))
+ throw Win32Error(::GetLastError(), "Failed to get cursor position.");
+ if (!::ScreenToClient(hwnd_, &p))
+ throw Win32Error(::GetLastError(), "Failed to call ScreenToClient.");
+ return PiToDip(p);
+ }
+ return Point{};
+}
+
+bool WinNativeWindow::CaptureMouse() {
+ if (IsValid()) {
+ ::SetCapture(hwnd_);
+ return true;
+ }
+ return false;
+}
+
+bool WinNativeWindow::ReleaseMouse() {
+ if (IsValid()) {
+ const auto result = ::ReleaseCapture();
+ return result != 0;
+ }
+ return false;
+}
+
graph::Painter* WinNativeWindow::BeginPaint() {
return new WindowD2DPainter(this);
}
@@ -268,8 +298,7 @@ void WinNativeWindow::OnResizeInternal(const int new_width,
const int new_height) {
if (!(new_width == 0 && new_height == 0)) {
window_render_target_->ResizeBuffer(new_width, new_height);
- resize_event_.Raise(
- Size{PixelToDipX(new_width), PixelToDipY(new_height)});
+ resize_event_.Raise(Size{PixelToDipX(new_width), PixelToDipY(new_height)});
}
}
@@ -283,10 +312,6 @@ void WinNativeWindow::OnKillFocusInternal() {
focus_event_.Raise(false);
}
-inline Point PiToDip(const POINT& pi_point) {
- return Point(PixelToDipX(pi_point.x), PixelToDipY(pi_point.y));
-}
-
void WinNativeWindow::OnMouseMoveInternal(const POINT point) {
// when mouse was previous outside the window
if (!is_mouse_in_) {