Sciter: F:/hsmile5/sdk/include/behaviors/behavior_drawing-gdi.cpp Source File

Sciter  3.3.2.5
Sciter API
behavior_drawing-gdi.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "sciter-x.h"
3 #include "sciter-x-behavior.h"
4 #include "sciter-x-graphics.hpp"
5 
6  namespace sciter
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 
18  class dib32
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 
31   dib32(unsigned w, unsigned h) :
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 
63   ~dib32() {
64  if( _dc )
65  {
66  ::SelectObject(_dc,_old_bitmap);
67  ::DeleteDC(_dc);
68  }
69  if( _bitmap )
70  ::DeleteObject(_bitmap);
71  }
72 
73   void set_white()
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; }
82   HDC DC() const
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 
98  struct gdi_drawing: public event_handler
99 {
100  // ctor
101   gdi_drawing() {}
102   virtual ~gdi_drawing() {}
103 
104   virtual bool subscription( HELEMENT he, UINT& event_groups )
105  {
106  event_groups = HANDLE_DRAW; // it does drawing
107  return true;
108  }
109 
110   virtual void attached (HELEMENT he ) { }
111   virtual void detached (HELEMENT he ) { delete this; }
112 
113   virtual bool handle_draw (HELEMENT he, DRAW_PARAMS& params )
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 
120  dib32 buffer(w,h);
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 
134   void do_draw( HDC hdc, unsigned width, unsigned height ) {
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 
148  struct gdi_drawing_factory: public behavior_factory {
149 
150   gdi_drawing_factory(): behavior_factory("gdi-drawing") { }
151 
152  // the only behavior_factory method:
153   virtual event_handler* create(HELEMENT he) {
154  return new gdi_drawing();
155  }
156 
157 };
158 
159 // instantiating and attaching it to the global list
160  gdi_drawing_factory gdi_drawing_factory_instance;
161 
162 
163 }
unsigned width() const
REAL POS
virtual bool subscription(HELEMENT he, UINT &event_groups)
Behaiviors support (a.k.a windowless scriptable controls)
gdi_drawing_factory gdi_drawing_factory_instance
void * bits() const
virtual void detached(HELEMENT he)
dib32(unsigned w, unsigned h)
void do_draw(HDC hdc, unsigned width, unsigned height)
unsigned height() const
virtual event_handler * create(HELEMENT he)
virtual bool handle_draw(HELEMENT he, DRAW_PARAMS &params)
void * HELEMENT
Definition: sciter-x-dom.h:40
const BITMAPINFO & get_info() const
BYTE * bytes() const
virtual void attached(HELEMENT he)

AltStyle によって変換されたページ (->オリジナル) /