1 #include "stdafx.h"
2 #include "vba.h"
3 #include "BitmapControl.h"
4 
5 #ifdef _DEBUG
6 #define new DEBUG_NEW
7 #undef THIS_FILE
8 static char THIS_FILE[] = __FILE__;
9 #endif
10 
11 bool BitmapControl::isRegistered = false;
12 
13 /////////////////////////////////////////////////////////////////////////////
14 // BitmapControl
15 
IMPLEMENT_DYNCREATE(BitmapControl,CScrollView)16 IMPLEMENT_DYNCREATE(BitmapControl, CScrollView)
17 
18   BitmapControl::BitmapControl()
19 {
20   w = 0;
21   h = 0;
22   data = NULL;
23   bmpInfo = NULL;
24   stretch = false;
25   registerClass();
26   CSize sizeTotal;
27   sizeTotal.cx = sizeTotal.cy = 0;
28   SetScrollSizes(MM_TEXT, sizeTotal);
29 }
30 
~BitmapControl()31 BitmapControl::~BitmapControl()
32 {
33 }
34 
35 
BEGIN_MESSAGE_MAP(BitmapControl,CScrollView)36 BEGIN_MESSAGE_MAP(BitmapControl, CScrollView)
37   //{{AFX_MSG_MAP(BitmapControl)
38   ON_WM_ERASEBKGND()
39   ON_WM_SIZE()
40   ON_WM_LBUTTONDOWN()
41   //}}AFX_MSG_MAP
42   END_MESSAGE_MAP()
43 
44   /////////////////////////////////////////////////////////////////////////////
45 // BitmapControl drawing
46 
47 void BitmapControl::OnInitialUpdate()
48 {
49   CScrollView::OnInitialUpdate();
50 
51   CSize sizeTotal;
52   // TODO: calculate the total size of this view
53   sizeTotal.cx = sizeTotal.cy = 100;
54   SetScrollSizes(MM_TEXT, sizeTotal);
55 }
56 
OnDraw(CDC * dc)57 void BitmapControl::OnDraw(CDC* dc)
58 {
59   RECT r;
60   GetClientRect(&r);
61   int w1 = r.right - r.left;
62   int h1 = r.bottom - r.top;
63   CDC memDC;
64   memDC.CreateCompatibleDC(dc);
65   if(!stretch) {
66     if(w > w1)
67       w1 = w;
68     if(h > h1)
69       h1 = h;
70   }
71   CBitmap bitmap, *pOldBitmap;
72   bitmap.CreateCompatibleBitmap(dc, w1, h1);
73   pOldBitmap = memDC.SelectObject(&bitmap);
74   if(stretch) {
75     bmpInfo->bmiHeader.biWidth = w;
76     bmpInfo->bmiHeader.biHeight = -h;
77 
78     StretchDIBits(memDC.GetSafeHdc(),
79                   0,
80                   0,
81                   w1,
82                   h1,
83                   0,
84                   0,
85                   w,
86                   h,
87                   data,
88                   bmpInfo,
89                   DIB_RGB_COLORS,
90                   SRCCOPY);
91   } else {
92     FillOutsideRect(&memDC, CBrush::FromHandle(GetSysColorBrush(COLOR_BTNFACE)));
93 
94     bmpInfo->bmiHeader.biWidth = w;
95     bmpInfo->bmiHeader.biHeight = -h;
96     SetDIBitsToDevice(memDC.GetSafeHdc(),
97                       0,
98                       0,
99                       w,
100                       h,
101                       0,
102                       0,
103                       0,
104                       h,
105                       data,
106                       bmpInfo,
107                       DIB_RGB_COLORS);
108   }
109 
110   dc->BitBlt(0,0,w1,h1,
111              &memDC,0,0,SRCCOPY);
112 
113   if (boxreigon.right != 0 && boxreigon.bottom != 0)
114   {
115 	  CBrush br = CBrush(RGB(255, 0, 0));
116 	  dc->FrameRect(&boxreigon, &br);
117   }
118   memDC.SelectObject(pOldBitmap);
119 
120   bitmap.DeleteObject();
121   memDC.DeleteDC();
122 }
123 
124 /////////////////////////////////////////////////////////////////////////////
125 // BitmapControl diagnostics
126 
127 #ifdef _DEBUG
AssertValid() const128 void BitmapControl::AssertValid() const
129 {
130   CScrollView::AssertValid();
131 }
132 
Dump(CDumpContext & dc) const133 void BitmapControl::Dump(CDumpContext& dc) const
134 {
135   CScrollView::Dump(dc);
136 }
137 #endif //_DEBUG
138 
139 /////////////////////////////////////////////////////////////////////////////
140 // BitmapControl message handlers
141 
OnEraseBkgnd(CDC * pDC)142 BOOL BitmapControl::OnEraseBkgnd(CDC* pDC)
143 {
144   return TRUE;
145 }
146 
OnSize(UINT nType,int cx,int cy)147 void BitmapControl::OnSize(UINT nType, int cx, int cy)
148 {
149   if(!stretch)
150     CScrollView::OnSize(nType, cx, cy);
151 }
152 
OnLButtonDown(UINT nFlags,CPoint pt)153 void BitmapControl::OnLButtonDown(UINT nFlags, CPoint pt)
154 {
155 	GetParent()->SendMessage(WM_BITMAPCLICK,
156 		GetDlgCtrlID(),
157 		0);
158 
159   if(!data)
160     return;
161   int x = pt.x;
162   int y = pt.y;
163 
164   WPARAM point;
165 
166   if(stretch) {
167     RECT rect;
168     GetClientRect(&rect);
169 
170     int height = rect.bottom - rect.top;
171     int width = rect.right - rect.left;
172 
173     int xx = (x * w) / width;
174     int yy = (y * h) / height;
175 
176     point = xx | (yy<<16);
177 
178     int xxx = xx / 8;
179     int yyy = yy / 8;
180 
181     for(int i = 0; i < 8; i++) {
182       memcpy(&colors[i*3*8], &data[xxx * 8 * 3 +
183                                    w * yyy * 8 * 3 +
184                                    i * w * 3], 8 * 3);
185     }
186   } else {
187     POINT p;
188     p.x = GetScrollPos(SB_HORZ);
189     p.y = GetScrollPos(SB_VERT);
190 
191     p.x += x;
192     p.y += y;
193 
194     if(p.x >= w ||
195        p.y >= h)
196       return;
197 
198     point = p.x | (p.y<<16);
199 
200     int xxx = p.x / 8;
201     int yyy = p.y / 8;
202 
203     for(int i = 0; i < 8; i++) {
204       memcpy(&colors[i*3*8], &data[xxx * 8 * 3 +
205                                    w * yyy * 8 * 3 +
206                                    i * w * 3], 8 * 3);
207     }
208   }
209 
210   GetParent()->SendMessage(WM_MAPINFO,
211                            point,
212                            (LPARAM)colors);
213 
214 }
215 
setBmpInfo(BITMAPINFO * info)216 void BitmapControl::setBmpInfo(BITMAPINFO *info)
217 {
218   bmpInfo = info;
219 }
220 
setData(u8 * d)221 void BitmapControl::setData(u8 *d)
222 {
223   data = d;
224 }
225 
setSize(int w1,int h1)226 void BitmapControl::setSize(int w1, int h1)
227 {
228   if(w != w1 || h != h1) {
229     w = w1;
230     h = h1;
231     SIZE s;
232     s.cx = w;
233     s.cy = h;
234     SetScrollSizes(MM_TEXT, s);
235   }
236 }
setSelectedRectangle(int x,int y,int width,int height)237 void BitmapControl::setSelectedRectangle(int x, int y, int width, int height)
238 {
239 	boxreigon.left = x;
240 	boxreigon.right = x + width;
241 	boxreigon.top = y;
242 	boxreigon.bottom = y + height;
243 }
refresh()244 void BitmapControl::refresh()
245 {
246   Invalidate();
247 }
248 
249 
registerClass()250 void BitmapControl::registerClass()
251 {
252   if(!isRegistered) {
253     WNDCLASS wc;
254     ZeroMemory(&wc, sizeof(wc));
255     wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
256     wc.lpfnWndProc = (WNDPROC)::DefWindowProc;
257     wc.hInstance = AfxGetInstanceHandle();
258     wc.hIcon = NULL;
259     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
260     wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
261     wc.lpszMenuName = NULL;
262     wc.lpszClassName = "VbaBitmapControl";
263     AfxRegisterClass(&wc);
264     isRegistered = true;
265   }
266 }
267 
setStretch(bool b)268 void BitmapControl::setStretch(bool b)
269 {
270   if(b != stretch) {
271     stretch = b;
272     Invalidate();
273   }
274 }
275 
getStretch()276 bool BitmapControl::getStretch()
277 {
278   return stretch;
279 }
280 
PostNcDestroy()281 void BitmapControl::PostNcDestroy()
282 {
283 }
284