YSTest: HostWindow.cpp 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
HostWindow.cpp
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2013.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "Helper/HostWindow.h"
29 #include "Host.h" // for WindowClassName;
30
31 YSL_BEGIN
32
33 using namespace Drawing;
34
35 #if YCL_HOSTED
36 YSL_BEGIN_NAMESPACE(Host)
37
38 namespace
39 {
40
41 #if YCL_MINGW32
42
43 void
44 ResizeWindow(::HWND h_wnd, SDst w, SDst h)
45 {
46 ::SetWindowPos(h_wnd, nullptr, 0, 0, w, h,
47 SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE
48 | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING | SWP_NOZORDER);
49 }
50 #endif
51
52 } // unnamed namespace;
53
54
55 Window::Window(NativeWindowHandle h, Environment& e)
56 : env(e), h_wnd(h)
57 {
58 YAssert(::IsWindow(h), "Invalid window handle found.");
59 YAssert(::GetWindowThreadProcessId(h, nullptr) == ::GetCurrentThreadId(),
60 "Window not created on current thread found.");
61 YAssert(::GetWindowLongPtrW(h, GWLP_USERDATA) == 0,
62 "Invalid user data of window found.");
63
64 wchar_t buf[ystdex::arrlen(WindowClassName)];
65
66 ::GetClassName(h_wnd, buf, ystdex::arrlen(WindowClassName));
67 if(std::wcscmp(buf, WindowClassName) != 0)
68 throw LoggedEvent("Wrong windows class name found.");
69 ::SetWindowLongPtrW(h_wnd, GWLP_USERDATA, ::LONG_PTR(this));
70 ::SetWindowPos(h_wnd, nullptr, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE
71 | SWP_NOOWNERZORDER | SWP_NOREDRAW | SWP_NOSENDCHANGING | SWP_NOSIZE
72 | SWP_NOZORDER);
73 e.AddMappedItem(h_wnd, this);
74 }
75 Window::~Window()
76 {
77 ::SetWindowLongPtrW(h_wnd, GWLP_USERDATA, ::LONG_PTR());
78 env.get().RemoveMappedItem(h_wnd);
79 // Note: The window could be already destroyed in window procedure.
80 if(::IsWindow(h_wnd))
81 ::DestroyWindow(h_wnd);
82 }
83
84 pair<Point, Point>
85 Window::GetInputBounds() const ynothrow
86 {
87 ::RECT rect;
88
89 ::GetClientRect(h_wnd, &rect);
90
91 YAssert(rect.right - rect.left >= 0 && rect.bottom - rect.top >= 0,
92 "Invalid boundary found.");
93
94 return {Point(rect.left, rect.top), Point(rect.right, rect.bottom)};
95 }
96
97 void
98 Window::Close()
99 {
100 ::SendNotifyMessageW(h_wnd, WM_CLOSE, 0, 0);
101 }
102
103 void
104 Window::Resize(const Size& s)
105 {
106 ResizeWindow(GetNativeHandle(), s.Width, s.Height);
107 }
108
109 void
110 Window::ResizeClient(const Size& s)
111 {
112 const auto h_wnd(GetNativeHandle());
113 ::RECT rect{0, 0, s.Width, s.Height};
114
115 ::AdjustWindowRect(&rect, ::GetWindowLongW(h_wnd, GWL_STYLE), FALSE);
116 ResizeWindow(h_wnd, rect.right - rect.left, rect.bottom - rect.top);
117 }
118
119 void
120 Window::OnDestroy()
121 {
122 ::PostQuitMessage(0);
123 }
124
125 void
126 Window::OnLostFocus()
127 {
128 platform_ex::ClearKeyStates();
129 }
130
131 void
132 Window::OnPaint()
133 {}
134
135 void
136 Window::Show() ynothrow
137 {
138 ::ShowWindowAsync(h_wnd, SW_SHOWNORMAL);
139 }
140
141 YSL_END_NAMESPACE(Host)
142 #endif
143
144 YSL_END
145