LIBURANIA
GUI library (a wrapper of Win32 API) in C++
読み取り中…
検索中…
一致する文字列を見つけられません
bwin.h
[詳解]
1/*
2 * Copyright 2002-2021 oZ/acy (名賀月晃嗣)
3 * Redistribution and use in source and binary forms,
4 * with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
41#ifndef INCLUDE_GUARD_URANIA_BASICWINDOW_H
42#define INCLUDE_GUARD_URANIA_BASICWINDOW_H
43
44#include <memory>
45#include <vector>
46#include "wbase.h"
47#include "paintdev.h"
48
49
50/*----------------------------------------------*/
54{
55public:
56 //-------------------
57 // struct D0_
58 // 記述構造体
59 //-------------------
60 struct D0_
61 {
62 std::wstring title; // タイトル
63 HWND pwnd; // 親ウィンドウ
64 HMENU hm; // メニュー
65 WNDPROC winproc; // ウィンドウプロシージャ
66
67 int x, y, w, h; // ウィンドウの位置、幅、高さ
68
69 unsigned icon; // アイコンのリソースID
70 unsigned cursor; //* カーソルのリソースID
71 unsigned bkcolor; // 背景色ID
72
73 bool maxbox; // 最大化ボックスの有無
74 bool minbox; // 最小化ボックスの有無
75 bool h_scrollbar; // 橫スクロールバーの有無
76 bool v_scrollbar; // 縱スクロールバーの有無
77 bool popup; // ポップアップウィンドウか否か
78 bool border_only; // 境界のみか否か
79 bool can_resize; // 大きさ變更の可否
80
81 D0_() noexcept
82 : pwnd(nullptr), hm(nullptr), winproc(nullptr),
83 x(CW_USEDEFAULT), w(CW_USEDEFAULT),
85 maxbox(false), minbox(false),
86 h_scrollbar(false), v_scrollbar(false), popup(false),
87 border_only(false), can_resize(false)
88 {}
89 };
90
91
92private:
93 //----------------------------------
94 // struct WC_
95 // WNDCLASS生成情報格納クラス
96 //--------------------------------//
97 struct WC_
98 {
99 unsigned icon_id_;
100 unsigned cursor_id_;
101 unsigned bkcolor_;
102 WNDPROC proc_;
103 std::wstring wcname_;
104
105 //========================================================================
106 // WNDCLASS登録の要不要を決めるための operator==() と operator!=()
107 //========================================================================
108 bool operator==(const WC_& other) const noexcept
109 {
110 if (icon_id_ != other.icon_id_)
111 return false;
112 if (cursor_id_ != other.cursor_id_)
113 return false;
114 if (bkcolor_ != other.bkcolor_)
115 return false;
116 if (proc_ != other.proc_)
117 return false;
118
119 return true;
120 }
121
122 bool operator!=(const WC_& other) const noexcept
123 {
124 return !(*this == other);
125 }
126 };
127
128
129private:
130 static std::vector<WC_> vwc_S; // WNDCLASS情報ベクタ
131
132protected:
133 BasicWindow() = default;
134
135 void createWindow0_(const D0_& de);
136
142 void bindHWND_(HWND hw)
143 {
144 link_(hw);
145 SetWindowLongPtr(hw, 0, reinterpret_cast<ULONG_PTR>(this));
146 }
147
150 {
151 if (hw_)
152 SetWindowLongPtr(hw_, 0, reinterpret_cast<ULONG_PTR>(nullptr));
153 }
154
156 void destroyWindow_() override
157 {
158 kill_();
159 }
160
169 virtual LRESULT wproc_(UINT msg, WPARAM wp, LPARAM lp) =0;
170
179 static LRESULT CALLBACK winproc_(HWND hw, UINT msg, WPARAM wp, LPARAM lp);
180
181 static std::wstring registerWC_(const WC_& wc);
182
183
184public:
185 ~BasicWindow() = default;
186
192 void update()
193 {
194 if (hw_)
195 ::UpdateWindow(hw_);
196 }
197
203 {
204 if (hw_)
205 ::InvalidateRect(hw_, nullptr, FALSE);
206 }
207
213 void invalidate(const eunomia::Rect& rect)
214 {
215 if (hw_) {
216 ::RECT r;
217 r.left = rect.left;
218 r.right = rect.right;
219 r.top = rect.top;
220 r.bottom = rect.bottom;
221
222 ::InvalidateRect(hw_, &r, FALSE);
223 }
224 }
225
229 std::unique_ptr<PaintDevice> getPaintDevice();
230
242 template<class PT_> LRESULT onPaint(PT_&& proc, WPARAM wp, LPARAM lp);
243
246 {
247 if (!hw_)
248 return 0;
249 RECT rc;
250 ::GetClientRect(hw_, &rc);
251 return rc.right;
252 }
253
256 {
257 if (!hw_)
258 return 0;
259 RECT rc;
260 ::GetClientRect(hw_, &rc);
261 return rc.bottom;
262 }
263
264
269 bool getClientWidthAndHeight(int& w, int& h)
270 {
271 if (!hw_)
272 return false;
273 RECT rc;
274 ::GetClientRect(hw_, &rc);
275 w = rc.right;
276 h = rc.bottom;
277 return true;
278 }
279
280 /* 仕樣に疑義あり 一端削除
281 * 削除を取り消す場合でも、getClientRectに改名するのが適當か?
282 polymnia::Rect getClientWidthAndHeight()
283 {
284 polymnia::Rect re(0, 0, 0, 0);
285 if (hw_)
286 {
287 RECT rc;
288 ::GetClientRect(hw_, &rc);
289 re.x2 = rc.right;
290 re.y2 = rc.bottom;
291 }
292 return re;
293 }
294 */
295
299 void resize(int w, int h)
300 {
301 if (hw_)
302 ::SetWindowPos(hw_, nullptr, 0, 0, w, h, SWP_NOMOVE | SWP_NOZORDER);
303 }
304
311 void resizeClientArea(int w, int h)
312 {
313 if (!hw_)
314 return;
315 RECT wr, cr;
316 ::GetWindowRect(hw_, &wr);
317 ::GetClientRect(hw_, &cr);
318 w += wr.right - wr.left - cr.right;
319 h += wr.bottom - wr.top - cr.bottom;
320 ::SetWindowPos(hw_, nullptr, 0, 0, w, h, SWP_NOMOVE | SWP_NOZORDER);
321 }
322
326 void setTimer(int id, int elapse) { ::SetTimer(hw_, id, elapse, nullptr); }
327
330 void killTimer(int id) { ::KillTimer(hw_, id); }
331
333 virtual LRESULT defHandler(UINT msg, WPARAM wp, LPARAM lp);
334};
335
336
337
338
339/*=================================================================
340 * BasicWindow::onPaint()
341 * WM_PAINTに対応するためのヘルパ
342 * PaintDeviceを生成してprocに渡す
343 * ユーザ定義のハンドラから必要に応じて明示的に呼び出すこと
344 *===============================================================*/
345template<class PT_>
346inline
347LRESULT urania::BasicWindow::onPaint(PT_&& proc, WPARAM wp, LPARAM lp)
348{
349 (void)wp;
350 (void)lp;
351
352 PAINTSTRUCT ps;
353 ::BeginPaint(hw_, &ps);
354 RECT rc;
355 ::GetClientRect(hw_, &rc);
356
357 auto pd = PaintDevice::create(ps.hdc, nullptr, nullptr, rc.right, rc.bottom);
358
359 proc(this, pd.get());
360
361 pd.reset(); // EndPaintを呼ぶ前にPaintDeviceを破棄する
362 ::EndPaint(hw_, &ps);
363
364 return 0;
365}
366
367
368
369
370#endif // INCLUDE_GUARD_URANIA_BASICWINDOW_H
Window管理・操作用基底クラス
Definition bwin.h:54
void update()
ウィンドウ更新(再描畫要求)
Definition bwin.h:192
void destroyWindow_() override
ウィンドウ破棄の實處理を實裝
Definition bwin.h:156
void unbindHWND_()
HWNDからBasicWindowオブジェクトへの結合を切斷
Definition bwin.h:149
bool getClientWidthAndHeight(int &w, int &h)
クライアント領域の幅と高さの取得
Definition bwin.h:269
void invalidate(const eunomia::Rect &rect)
ウィンドウの再描畫領域の設定
Definition bwin.h:213
static std::wstring registerWC_(const WC_ &wc)
Definition bwin.cpp:50
void bindHWND_(HWND hw)
BasicWindowオブジェクトとHWNDの二重結合を形成
Definition bwin.h:142
void createWindow0_(const D0_ &de)
Definition bwin.cpp:144
void resizeClientArea(int w, int h)
ウィンドウの大きさを變更
Definition bwin.h:311
std::unique_ptr< PaintDevice > getPaintDevice()
ウィンドウ描畫用のPaintDeviceを取得
Definition bwin.cpp:255
void setTimer(int id, int elapse)
タイマーを設定
Definition bwin.h:326
static LRESULT CALLBACK winproc_(HWND hw, UINT msg, WPARAM wp, LPARAM lp)
ウィンドウプロシージャ
Definition bwin.cpp:205
virtual LRESULT wproc_(UINT msg, WPARAM wp, LPARAM lp)=0
ウィンドウプロシージャ(個別)
void killTimer(int id)
タイマーを破棄
Definition bwin.h:330
~BasicWindow()=default
virtual LRESULT defHandler(UINT msg, WPARAM wp, LPARAM lp)
デフォルトのメッセージ處理
Definition bwin.cpp:236
void resize(int w, int h)
ウィンドウの大きさを變更
Definition bwin.h:299
int getClientWidth()
クライアント領域の幅を取得
Definition bwin.h:245
void invalidate()
ウィンドウの再描畫領域の設定
Definition bwin.h:202
LRESULT onPaint(PT_ &&proc, WPARAM wp, LPARAM lp)
ウィンドウ再描畫
Definition bwin.h:347
int getClientHeight()
クライアント領域の高さを取得
Definition bwin.h:255
static std::unique_ptr< PaintDevice > create(HDC dc, DestProc dp, void *a, int w, int h)
Definition pdev.cpp:84
HWND管理用基底クラス
Definition wbase.h:55
void link_(HWND h)
HWNDの強固な連結
Definition wbase.h:73
HWND hw_
管理對象のHWND
Definition wbase.h:57
void kill_()
HWNDの切り離しと破棄
Definition wbase.h:84
@ BG_WHITE
Definition decl.h:107
@ DEFAULT_RC
Definition decl.h:102
Windows DC 描畫用クラス
Definition bwin.h:61
bool can_resize
Definition bwin.h:79
bool v_scrollbar
Definition bwin.h:76
int h
Definition bwin.h:67
unsigned cursor
Definition bwin.h:70
HWND pwnd
Definition bwin.h:63
int w
Definition bwin.h:67
bool maxbox
Definition bwin.h:73
HMENU hm
Definition bwin.h:64
std::wstring title
Definition bwin.h:62
unsigned icon
Definition bwin.h:69
int x
Definition bwin.h:67
bool border_only
Definition bwin.h:78
unsigned bkcolor
Definition bwin.h:71
bool popup
Definition bwin.h:77
D0_() noexcept
Definition bwin.h:81
int y
Definition bwin.h:67
bool minbox
Definition bwin.h:74
bool h_scrollbar
Definition bwin.h:75
WNDPROC winproc
Definition bwin.h:65
HWND管理基底クラス