0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
| #include <windows.h>
#include <iostream>
template <class object, class type>
class property
{
public:
typedef type (object::*get_proc)() const;
typedef void (object::*set_proc)(type const&);
property(object& object, get_proc getpr, set_proc setpr)
: obj (object)
, getter(getpr)
, setter(setpr)
{
}
operator type ()
{
return get();
}
type const& operator = (type const& value)
{
set(value);
return value;
}
property& operator = (property const& prt)
{
set(prt.get());
return *this;
}
private:
object& obj;
get_proc getter;
set_proc setter;
inline type get() const
{
return (obj.*getter)();
}
inline void set(type const& value)
{
(obj.*setter)(value);
}
};
class window
{
public:
property<window, std::string> title;
window(std::string const& title_text)
: title(*this, &window::get_title, &window::set_title)
{
set_title(title_text);
}
private:
HWND handle;
std::string get_title() const
{
char buffer[1024] = {0};
GetWindowTextA(handle, buffer, sizeof(buffer));
return buffer;
}
void set_title(const std::string& new_title)
{
SetWindowTextA(handle, new_title.c_str());
}
};
int main()
{
window w1("Toolbar");
window w2("About box");
w1.title = "Help page";
std::string title = w2.title;
w2.title = w1.title;
return 0;
} | |