aboutsummaryrefslogtreecommitdiff
path: root/src/win/native/native_window.cpp
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-09-07 22:06:31 +0800
committer杨宇千 <crupest@outlook.com>2019-09-07 22:06:31 +0800
commit2c40085dd30d6e7370a0974ad1f642a61acc6e30 (patch)
treecdb940c72f95ae3d9b4a31f1ec18ef4c4643dd54 /src/win/native/native_window.cpp
parent78c00a8329dcabf86b30f5510bd6dfd3a4c141a1 (diff)
downloadcru-2c40085dd30d6e7370a0974ad1f642a61acc6e30.tar.gz
cru-2c40085dd30d6e7370a0974ad1f642a61acc6e30.tar.bz2
cru-2c40085dd30d6e7370a0974ad1f642a61acc6e30.zip
...
Diffstat (limited to 'src/win/native/native_window.cpp')
-rw-r--r--src/win/native/native_window.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/win/native/native_window.cpp b/src/win/native/native_window.cpp
index f301f955..bda84ea9 100644
--- a/src/win/native/native_window.cpp
+++ b/src/win/native/native_window.cpp
@@ -1,6 +1,9 @@
#include "cru/win/native/native_window.hpp"
+#include "cru/common/format.hpp"
+#include "cru/platform/debug.hpp"
#include "cru/win/graph/direct/graph_factory.hpp"
+#include "cru/win/native/cursor.hpp"
#include "cru/win/native/exception.hpp"
#include "cru/win/native/ui_application.hpp"
#include "cru/win/native/window_class.hpp"
@@ -160,6 +163,52 @@ graph::Painter* WinNativeWindow::BeginPaint() {
return new WindowD2DPainter(this);
}
+void WinNativeWindow::SetCursor(std::shared_ptr<Cursor> cursor) {
+ if (!IsValid()) return;
+ assert(cursor);
+ WinCursor* c = static_cast<WinCursor*>(cursor.get());
+
+ auto outputError = [] {
+ DebugMessage(util::Format(util::Format(
+ L"Failed to set cursor. Last error code: {}.", ::GetLastError())));
+ };
+
+ if (!::SetWindowLongPtrW(hwnd_, GCLP_HCURSOR,
+ reinterpret_cast<LONG_PTR>(c->GetHandle()))) {
+ outputError();
+ return;
+ }
+
+ ::POINT point;
+ if (!::GetCursorPos(&point)) {
+ outputError();
+ return;
+ }
+
+ ::RECT rect;
+ if (!::GetClientRect(hwnd_, &rect)) {
+ outputError();
+ return;
+ }
+
+ ::POINT lefttop{rect.left, rect.top};
+ ::POINT rightbottom{rect.right, rect.bottom};
+ if (!::ClientToScreen(hwnd_, &lefttop)) {
+ outputError();
+ return;
+ }
+
+ if (!::ClientToScreen(hwnd_, &rightbottom)) {
+ outputError();
+ return;
+ }
+
+ if (point.x >= lefttop.x && point.y >= lefttop.y &&
+ point.x <= rightbottom.x && point.y <= rightbottom.y) {
+ ::SetCursor(c->GetHandle());
+ }
+}
+
bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg,
WPARAM w_param, LPARAM l_param,
LRESULT* result) {