windows.window.h.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <chapter xml:id="windows.window.h">
  2. <title><tt>__vic/windows/window.h</tt></title>
  3. <chapter xml:id="windows--Window">
  4. <title><tt>windows::Window</tt></title>
  5. <code-block lang="C++"><![CDATA[
  6. class windows::Window
  7. {
  8. public:
  9. struct Class;
  10. struct CreateParams;
  11. Window() = default;
  12. explicit Window(HWND hwnd);
  13. static void Register(WNDCLASSEXW wcl);
  14. void Create(DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpWndName,
  15. DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent,
  16. HMENU hMenu, HINSTANCE hInstance, void *lpParam = nullptr);
  17. void Create(const CREATESTRUCTW &cs);
  18. LRESULT SendMessage(UINT msg, WPARAM w = 0, LPARAM l = 0);
  19. void Update();
  20. bool Show(int nWinMode);
  21. void Redraw(const RECT *rect = 0, HRGN hrgn = 0,
  22. UINT flags = RDW_INVALIDATE | RDW_UPDATENOW);
  23. void GetRect(RECT &r) const;
  24. RECT GetRect() const;
  25. void GetClientRect(RECT &r) const
  26. RECT GetClientRect() const;
  27. void SetText(LPCWSTR st);
  28. void SetText(const char *st);
  29. int GetText(LPWSTR st, int nMax) const;
  30. void SetPos(HWND hWndAfter, int x, int y, int w, int h, UINT uFlags);
  31. void SetPos(int x, int y, int w, int h);
  32. void SetSize(int w, int h);
  33. void SetClientAreaSize(int w, int h);
  34. void MoveTo(int x, int y);
  35. void MoveToCenter();
  36. HWND Handle() const;
  37. void Handle(HWND hwnd);
  38. operator HWND() const;
  39. };
  40. ]]></code-block>
  41. <p>Thin wrapper for Win32 API <tt>HWND</tt>.</p>
  42. <section><title>Class members</title>
  43. <synopsis>
  44. <prototype>Window() = default</prototype>
  45. <p>Creates an uninitialized value.</p>
  46. </synopsis>
  47. <synopsis>
  48. <prototype>explicit Window(HWND hwnd)</prototype>
  49. <postcondition><tt>Handle() == hwnd</tt></postcondition>
  50. </synopsis>
  51. <synopsis>
  52. <prototype>static void Register(WNDCLASSEXW wcl)</prototype>
  53. <p>Calls <tt>::RegisterClassExW()</tt>. Throws on error.</p>
  54. </synopsis>
  55. <synopsis>
  56. <prototype>void Create(DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpWndName,
  57. DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent,
  58. HMENU hMenu, HINSTANCE hInstance, void *lpParam = nullptr)</prototype>
  59. <p>Calls <tt>::CreateWindowExW()</tt> and updates the wrapped <tt>HWND</tt>
  60. value. Throws on error.</p>
  61. </synopsis>
  62. <synopsis>
  63. <prototype>void Create(const CREATESTRUCTW &amp;cs)</prototype>
  64. <p>Calls <tt>Create()</tt> with the values specified in <tt>cs</tt>.
  65. Throws on error.</p>
  66. </synopsis>
  67. <synopsis>
  68. <prototype>LRESULT SendMessage(UINT msg, WPARAM w = 0, LPARAM l = 0)</prototype>
  69. <p>Calls <tt>::SendMessage()</tt> and returns the returned value.</p>
  70. </synopsis>
  71. <synopsis>
  72. <prototype>void Update()</prototype>
  73. <p>Calls <tt>::UpdateWindow()</tt>. Throws on error.</p>
  74. </synopsis>
  75. <synopsis>
  76. <prototype>bool Show(int nWinMode)</prototype>
  77. <p>Calls <tt>::ShowWindow()</tt> and returns the returned value.</p>
  78. </synopsis>
  79. <synopsis>
  80. <prototype>void Redraw(const RECT *rect = 0, HRGN hrgn = 0,
  81. UINT flags = RDW_INVALIDATE | RDW_UPDATENOW)</prototype>
  82. <p>Calls <tt>::RedrawWindow()</tt>. Throws on error.</p>
  83. </synopsis>
  84. <synopsis>
  85. <prototype>void GetRect(RECT &amp;r) const</prototype>
  86. <prototype>RECT GetRect() const</prototype>
  87. <p>Calls <tt>::GetWindowRect()</tt>. Throws on error.</p>
  88. </synopsis>
  89. <synopsis>
  90. <prototype>void GetClientRect(RECT &amp;r) const</prototype>
  91. <prototype>RECT GetClientRect() const</prototype>
  92. <p>Calls <tt>::GetClientRect()</tt>. Throws on error.</p>
  93. </synopsis>
  94. <synopsis>
  95. <prototype>void SetText(LPCWSTR st)</prototype>
  96. <p>Calls <tt>::SetWindowTextW()</tt>. Throws on error.</p>
  97. </synopsis>
  98. <synopsis>
  99. <prototype>void SetText(const char *st)</prototype>
  100. <p>Converts the specified string from UTF-8 to UTF-16 then calls
  101. <tt>SetText()</tt>. Throws on error.</p>
  102. </synopsis>
  103. <synopsis>
  104. <prototype>int GetText(LPWSTR st, int nMax) const</prototype>
  105. <p>Calls <tt>::GetWindowTextW()</tt> and returns the returned value.</p>
  106. </synopsis>
  107. <synopsis>
  108. <prototype>void SetPos(HWND hWndAfter, int x, int y, int w, int h, UINT uFlags)</prototype>
  109. <p>Calls <tt>::SetWindowPos()</tt>. Throws on error.</p>
  110. </synopsis>
  111. <synopsis>
  112. <prototype>void SetPos(int x, int y, int w, int h)</prototype>
  113. <p>Sets position and size of the window. Throws on error.</p>
  114. </synopsis>
  115. <synopsis>
  116. <prototype>void SetSize(int w, int h)</prototype>
  117. <p>Sets size of the window. Throws on error.</p>
  118. </synopsis>
  119. <synopsis>
  120. <prototype>void SetClientAreaSize(int w, int h)</prototype>
  121. <p>Sets client area size of the window. Throws on error.</p>
  122. </synopsis>
  123. <synopsis>
  124. <prototype>void MoveTo(int x, int y)</prototype>
  125. <p>Moves the window to the specified position. Throws on error.</p>
  126. </synopsis>
  127. <synopsis>
  128. <prototype>void MoveToCenter()</prototype>
  129. <p>Moves the window to the center of the screen. Throws on error.</p>
  130. </synopsis>
  131. <synopsis>
  132. <prototype>HWND Handle() const</prototype>
  133. <prototype>operator HWND() const</prototype>
  134. <p>Returns the wrapped value.</p>
  135. </synopsis>
  136. <synopsis>
  137. <prototype>void Handle(HWND hwnd)</prototype>
  138. <postcondition><tt>Handle() == hwnd</tt></postcondition>
  139. </synopsis>
  140. </section>
  141. <section><title>Example</title>
  142. <code-block lang="C++"><![CDATA[
  143. // Skeleton Win32 API application
  144. #include<__vic/windows/window.h>
  145. #include<windows.h>
  146. #include<exception>
  147. extern "C" LRESULT CALLBACK WindowFunc(
  148. HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  149. {
  150. switch(msg)
  151. {
  152. case WM_DESTROY:
  153. PostQuitMessage(0); break;
  154. default:
  155. return DefWindowProc(hwnd, msg, wParam, lParam);
  156. }
  157. return 0;
  158. }
  159. WPARAM skeleton_win32_app(int nWinMode)
  160. {
  161. using __vic::windows::Window;
  162. static wchar_t szWinClassName[] = L"TestAppClass";
  163. Window wnd;
  164. HINSTANCE hInstance = ::GetModuleHandle(0);
  165. HICON hIcon = LoadIcon(NULL, IDI_APPLICATION);
  166. Window::Register(Window::Class(hInstance, szWinClassName, WindowFunc)
  167. .Icon(hIcon)
  168. .SmallIcon(hIcon)
  169. .Cursor(LoadCursor(0, IDC_ARROW))
  170. .Background((HBRUSH) GetStockObject(GRAY_BRUSH))
  171. );
  172. wnd.Create(Window::CreateParams(hInstance,
  173. szWinClassName, L"Skeleton Win32 application"));
  174. wnd.Show(nWinMode);
  175. wnd.Update();
  176. return __vic::windows::MessageLoop();
  177. }
  178. int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR lpszArgs, int nWinMode)
  179. {
  180. try
  181. {
  182. return skeleton_win32_app(nWinMode);
  183. }
  184. catch(const std::exception &ex)
  185. {
  186. __vic::windows::MsgBox(ex.what(),
  187. "Application error", MB_OK | MB_ICONERROR);
  188. }
  189. return 1;
  190. }
  191. ]]></code-block>
  192. </section>
  193. </chapter>
  194. <chapter xml:id="windows--Window--Class">
  195. <title><tt>windows::Window::Class</tt></title>
  196. <code-block lang="C++"><![CDATA[
  197. struct windows::Window::Class : WNDCLASSEXW
  198. {
  199. Class(HMODULE hInst, LPCWSTR name, WNDPROC wndProc)
  200. {
  201. cbSize = sizeof(WNDCLASSEXW);
  202. style = 0;
  203. lpfnWndProc = wndProc;
  204. cbClsExtra = cbWndExtra = 0;
  205. hInstance = hInst;
  206. hIcon = 0;
  207. hCursor = ::LoadCursorW(0, IDC_ARROW);
  208. hbrBackground = 0;
  209. lpszMenuName = 0;
  210. lpszClassName = name;
  211. hIconSm = 0;
  212. }
  213. Class &Style(UINT v) { style = v; return *this; }
  214. Class &ClsExtra(int v) { cbClsExtra = v; return *this; }
  215. Class &WndExtra(int v) { cbWndExtra = v; return *this; }
  216. Class &Instance(HINSTANCE v) { hInstance = v; return *this; }
  217. Class &Icon(HICON v) { hIcon = v; return *this; }
  218. Class &Cursor(HCURSOR v) { hCursor = v; return *this; }
  219. Class &Background(HBRUSH v) { hbrBackground = v; return *this; }
  220. Class &MenuName(LPCWSTR v) { lpszMenuName = v; return *this; }
  221. Class &SmallIcon(HICON v) { hIconSm = v; return *this; }
  222. };
  223. ]]></code-block>
  224. <p>Wrapper for <tt>WNDCLASSEXW</tt>.</p>
  225. <section><title>Example</title>
  226. <code-block lang="C++">
  227. using __vic::windows::Windows;
  228. Window::Register(Window::Class(hInstance, WndClassName, wndProc)
  229. .Icon(hIcon)
  230. .SmallIcon(hIcon)
  231. .Cursor(::LoadCursor(nullptr, IDC_ARROW))
  232. .Background((HBRUSH) ::GetStockObject(LTGRAY_BRUSH))
  233. );
  234. </code-block>
  235. </section>
  236. </chapter>
  237. <chapter xml:id="windows--Window--CreateParams">
  238. <title><tt>windows::Window::CreateParams</tt></title>
  239. <code-block lang="C++"><![CDATA[
  240. struct windows::Window::CreateParams : CREATESTRUCTW
  241. {
  242. CreateParams(HMODULE hInst, LPCWSTR lpClsName, LPCWSTR lpWndName = nullptr)
  243. {
  244. lpCreateParams = 0;
  245. hInstance = hInst;
  246. hMenu = 0;
  247. hwndParent = HWND_DESKTOP;
  248. cy = cx = y = x = CW_USEDEFAULT;
  249. style = WS_OVERLAPPEDWINDOW;
  250. lpszName = lpWndName;
  251. lpszClass = lpClsName;
  252. dwExStyle = 0;
  253. }
  254. CreateParams &ExtraParam(void *v) { lpCreateParams = v; return *this; }
  255. CreateParams &Instance(HINSTANCE v) { hInstance = v; return *this; }
  256. CreateParams &Menu(HMENU v) { hMenu = v; return *this; }
  257. CreateParams &Parent(HWND v) { hwndParent = v; return *this; }
  258. CreateParams &Position(int X, int Y) { x = X; y = Y; return *this; }
  259. CreateParams &Dimension(int w, int h) { cx = w; cy = h; return *this; }
  260. CreateParams &Style(DWORD v) { style = v; return *this; }
  261. CreateParams &ExStyle(DWORD v) { dwExStyle = v; return *this; }
  262. };
  263. ]]></code-block>
  264. <p>Wrapper for <tt>CREATESTRUCTW</tt>.</p>
  265. <section><title>Example</title>
  266. <code-block lang="C++">
  267. using __vic::windows::Windows;
  268. Windows wnd;
  269. wnd.Create(Window::CreateParams(hInstance, className, title).Position(x, y));
  270. </code-block>
  271. </section>
  272. </chapter>
  273. <chapter xml:id="windows--MsgBox">
  274. <title><tt>windows::MsgBox()</tt></title>
  275. <code-block lang="C++">
  276. int windows::MsgBox(HWND hwnd,
  277. const wchar_t *msg, const wchar_t *title = L"", int t = MB_OK);
  278. int windows::MsgBox(
  279. const wchar_t *msg, const wchar_t *title = L"", int t = MB_OK);
  280. int windows::MsgBox(HWND hwnd,
  281. const char *msg, const char *title = "", int t = MB_OK);
  282. int windows::MsgBox(
  283. const char *msg, const char *title = "", int t = MB_OK)
  284. </code-block>
  285. <p>Calls <tt>::MessageBoxW()</tt> and returns the returned value. <tt>hwnd</tt>
  286. parameter is <tt>NULL</tt> (<tt>HWND_DESKTOP</tt>) when not specified.
  287. <tt>char</tt>-functions convert strings from UTF-8 to UTF-16.</p>
  288. </chapter>
  289. <chapter xml:id="windows--MessageLoop">
  290. <title><tt>windows::MessageLoop()</tt></title>
  291. <code-block lang="C++">
  292. WPARAM windows::MessageLoop(HWND hwnd = NULL);
  293. </code-block>
  294. <p>Calls <tt>::GetMessage()</tt> while it returs nonzero (until
  295. <tt>WM_QUIT</tt> is read). Then calls <tt>::TranslateMessage()</tt> and
  296. <tt>::DispatchMessage()</tt> with the message. Returns <tt>MSG::wParam</tt>.</p>
  297. </chapter>
  298. <chapter xml:id="windows--ProcessMessages">
  299. <title><tt>windows::ProcessMessages()</tt></title>
  300. <code-block lang="C++">
  301. bool windows::ProcessMessages(HWND hwnd = NULL);
  302. </code-block>
  303. <p>Like <xref to="windows--MessageLoop"/> but returns when message queue is
  304. empty. Returns <tt>true</tt> if can be called again and <tt>false</tt> if
  305. <tt>WM_QUIT</tt> message was read.</p>
  306. </chapter>
  307. </chapter>