1 #include "stdafx.h"
5
7 {
8 /*
9 BEHAVIOR: gdi-drawing
10 - draws content layer using GDI primitives.
11
12 SAMPLE:
13 See: samples/behaviors/gdi-drawing.htm
14 */
15
16 // dib (bitmap) combined with DC
17
19 {
20 const unsigned _width;
21 const unsigned _height;
22 void* _bits;
23 mutable HBITMAP _old_bitmap;
24 mutable HDC _dc;
25 HBITMAP _bitmap;
26
27 BITMAPINFO _bitmap_info;
28
29 public:
30
32 _width(w),
33 _height(h),
34 _bits(0),
35 _old_bitmap(0),
36 _dc(0) {
37
38 memset(&_bitmap_info,0,sizeof(_bitmap_info));
39 _bitmap_info.bmiHeader.biSize = sizeof(_bitmap_info.bmiHeader);
40 _bitmap_info.bmiHeader.biWidth = _width;
41 _bitmap_info.bmiHeader.biHeight = 0 - int(_height);
42 _bitmap_info.bmiHeader.biPlanes = 1;
43 _bitmap_info.bmiHeader.biBitCount = 32;
44 _bitmap_info.bmiHeader.biCompression = BI_RGB;
45
46 _bitmap = ::CreateDIBSection(
47 NULL, // device context
48 &_bitmap_info,
49 DIB_RGB_COLORS,
50 &_bits,
51 0, // file mapping object
52 0); // file offset
53
54 if (0 == _bits) {
55 return;
56 //throw std::bad_alloc();
57 }
58
59 memset(_bits,0, _width * _height * 4);
60
61 }
62
64 if( _dc )
65 {
66 ::SelectObject(_dc,_old_bitmap);
67 ::DeleteDC(_dc);
68 }
69 if( _bitmap )
70 ::DeleteObject(_bitmap);
71 }
72
74 {
75 memset(_bits,0xff, _width * _height * 4);
76 }
77
78 unsigned width()
const {
return _width; }
79 unsigned height()
const {
return _height; }
80 void*
bits()
const {
return _bits; }
81 BYTE*
bytes()
const {
return (BYTE*)_bits; }
83 {
84 if(!_dc) {
85 _dc = ::CreateCompatibleDC(NULL);
86 if (_dc)
87 _old_bitmap = (HBITMAP)::SelectObject(_dc,_bitmap);
88 }
89 return _dc;
90 }
91
92 HBITMAP
detach() {
auto r = _bitmap; _bitmap = 0;
return r; }
93
94 const BITMAPINFO&
get_info()
const {
return _bitmap_info; }
95 };
96
97
99 {
100 // ctor
103
105 {
107 return true;
108 }
109
112
114 {
115 if( params.
cmd !=
DRAW_CONTENT )
return false;
// drawing only content layer 116
117 unsigned w = unsigned(params.
area.right - params.
area.left);
118 unsigned h = unsigned(params.
area.bottom - params.
area.top);
119
121
122 do_draw(buffer.
DC(), w,h);
123
124 sciter::image img = sciter::image::create(w,h,
false,buffer.
bytes());
125
126 sciter::graphics gfx(params.
gfx);
127
128 gfx.draw_image(&img,
POS(params.
area.left),
POS(params.
area.top));
129
130 return true;
131
132 }
133
135 RECT rc = { 0,0,
width,height };
136 HBRUSH hbr1 = ::CreateSolidBrush(0x000000);
137 HBRUSH hbr2 = ::CreateSolidBrush(0x0000FF);
138 // fill it by black color:
139 ::FillRect(hdc,&rc,hbr1);
140 ::InflateRect(&rc,-40,-40);
141 ::FillRect(hdc,&rc,hbr2);
142 ::DeleteObject(hbr1);
143 ::DeleteObject(hbr2);
144 }
145
146 };
147
149
151
152 // the only behavior_factory method:
155 }
156
157 };
158
159 // instantiating and attaching it to the global list
161
162
163 }
virtual bool subscription(HELEMENT he, UINT &event_groups)
Behaiviors support (a.k.a windowless scriptable controls)
gdi_drawing_factory gdi_drawing_factory_instance
virtual void detached(HELEMENT he)
dib32(unsigned w, unsigned h)
void do_draw(HDC hdc, unsigned width, unsigned height)
virtual event_handler * create(HELEMENT he)
virtual bool handle_draw(HELEMENT he, DRAW_PARAMS ¶ms)
const BITMAPINFO & get_info() const
virtual void attached(HELEMENT he)