-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageStatic.cpp
More file actions
496 lines (371 loc) · 9.5 KB
/
Copy pathImageStatic.cpp
File metadata and controls
496 lines (371 loc) · 9.5 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// ImageStatic.cpp: 实现文件
//
#include "pch.h"
#include "FingerAreaMarker.h"
#include "ImageStatic.h"
// CImageStatic
IMPLEMENT_DYNAMIC(CImageStatic, CStatic)
CImageStatic::CImageStatic()
{
}
CImageStatic::~CImageStatic()
{
}
BEGIN_MESSAGE_MAP(CImageStatic, CStatic)
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONUP()
ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()
// CImageStatic 消息处理程序
void CImageStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CStatic::OnPaint()
DrawImage();
}
void CImageStatic::OnSize(UINT nType, int cx, int cy)
{
CStatic::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
}
BOOL CImageStatic::OnEraseBkgnd(CDC* pDC)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
return TRUE;
return CStatic::OnEraseBkgnd(pDC);
}
void CImageStatic::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (!m_bClipCursor)
{
GetClipCursor(&m_rtOldClipCursor);
#if 0
RECT rect;
GetClientRect(&rect);
ClientToScreen(&rect);
//GetWindowRect(&rect); //include title area
ClipCursor(&rect);
#else
RECT rect = m_rtImage;
//没有图的时候是空的
if (rect.left - rect.right == 0 && rect.top - rect.bottom == 0)
rect = m_rtFrame;
ClientToScreen(&rect);
//GetWindowRect(&rect); //include title area
ClipCursor(&rect);
#endif
m_bClipCursor = TRUE;
}
if (m_bIsSetFingerRgn)
{
Gfp_BeginMakePolygonRgn(point);
return;
}
if (m_bClipCursor)
{
ClipCursor(&m_rtOldClipCursor);
m_bClipCursor = FALSE;
}
CStatic::OnLButtonDown(nFlags, point);
}
void CImageStatic::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (m_bIsSetFingerRgn)
return;
if (m_bClipCursor)
{
ClipCursor(&m_rtOldClipCursor);
m_bClipCursor = FALSE;
}
ReleaseCapture();
CStatic::OnLButtonUp(nFlags, point);
}
void CImageStatic::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (m_bIsSetFingerRgn)
{
::SetClassLong(GetSafeHwnd(), GCL_HCURSOR, (LONG)LoadCursor(NULL, IDC_ARROW));
Gfp_MakePolygonRgn(point);
return;
}
CStatic::OnMouseMove(nFlags, point);
}
void CImageStatic::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (m_bClipCursor)
{
ClipCursor(&m_rtOldClipCursor);
m_bClipCursor = FALSE;
}
ReleaseCapture();
CStatic::OnRButtonUp(nFlags, point);
}
void CImageStatic::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (m_bIsSetFingerRgn)
{
Gfp_EndMakePolygonRgn(m_ptLast, TRUE);
m_bIsSetFingerRgn = FALSE;
return;
}
SetCapture();
CStatic::OnRButtonDown(nFlags, point);
}
#define VITICAL_DISP 2 //!< 在画图时,垂直方向从顶端(TOP)显示时,缺省留出2个像素
void CImageStatic::DrawWedge(CDC* pDC)
{
CPen pen, * poldpen;
CBrush brush, * poldbrush;
CRect rtClietn;
COLORREF cr;
GetClientRect(rtClietn);
cr = m_crFrame; //RGB(0, 0, 0);
pen.CreatePen(PS_SOLID, 1, cr);
brush.CreateStockObject(NULL_BRUSH);
poldpen = pDC->SelectObject(&pen);
poldbrush = pDC->SelectObject(&brush);
pDC->Rectangle(&rtClietn);
pDC->SelectObject(poldpen);
pDC->SelectObject(poldbrush);
pen.DeleteObject();
brush.DeleteObject();
}
void CImageStatic::DrawImage()
{
HWND hWndImg = GetSafeHwnd();
if (!hWndImg || !IsWindow(hWndImg)) return; //|| ( m_nWidth * m_nHeight ) < 2 ) return;
CDC* pDC = NULL;
pDC = GetDC();
if (NULL == pDC) return;
GetClientRect(&m_rtFrame);
if (m_bDrawNoScale)
DrawNoScale(hWndImg, pDC);
Gfp_DrawPolygonRgn(pDC);
ReleaseDC(pDC);
}
void CImageStatic::UTIL_DrawNoScaleData(CDC* pDC, CRect rtFrame, BITMAPINFO* pbmInfo, UCHAR* pData, CPoint* pptImgOff)
{
if (!pbmInfo || !pData) return;
//CBrush brush(RGB(224, 224, 224));
//pDC->FillRect(&rtFrame, &brush);
int nwImg, nhImg, nwFrm, nhFrm, nw, nh;
CPoint ptImgOff(0, 0), ptDispOff;
nwImg = pbmInfo->bmiHeader.biWidth;
nhImg = abs(pbmInfo->bmiHeader.biHeight);
nwFrm = rtFrame.Width();
nhFrm = rtFrame.Height();
if (((nwImg > nwFrm) || (nhImg > nhFrm)) && pptImgOff)
{
ptImgOff = *pptImgOff;
}
if (nwImg > nwFrm) nw = nwFrm;
else nw = nwImg;
if (nhImg > nhFrm) nh = nhFrm;
else nh = nhImg;
ptDispOff.x = (nwFrm - nwImg) / 2;
ptDispOff.y = (nhFrm - nhImg) / 2;
if (ptDispOff.x < 0)
{
ptImgOff.x = -ptDispOff.x;
ptDispOff.x = 0;
}
if (ptDispOff.y < 0)
{
ptImgOff.y = -ptDispOff.y;
ptDispOff.y = 0;
}
if (ptDispOff.y > VITICAL_DISP) ptDispOff.y = VITICAL_DISP;
if (pbmInfo->bmiHeader.biHeight > 0) ptImgOff.y = (nhImg - ptImgOff.y);
::SetDIBitsToDevice(pDC->GetSafeHdc(), rtFrame.left + ptDispOff.x, rtFrame.top + ptDispOff.y,
nw, nh, ptImgOff.x, ptImgOff.y, 0, nhImg, pData, pbmInfo, DIB_RGB_COLORS);
m_rtImage.left = rtFrame.left + ptDispOff.x;
m_rtImage.right = m_rtImage.left + nw;
m_rtImage.top = rtFrame.top + ptDispOff.y;
m_rtImage.bottom = m_rtImage.top + nh;
//brush.DeleteObject();
}
void CImageStatic::DrawNoScale(HWND hWndImg, CDC* pDC)
{
CRect rect;
GetClientRect(rect);
if (rect.Width() < 4 || rect.Height() < 4) return;
CDC dc;
CBitmap bmp, * poldbmp;
CBrush brush(m_crBackground);
// CPoint ptImageOff(0, 0);
dc.CreateCompatibleDC(pDC);
bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
poldbmp = dc.SelectObject(&bmp);
dc.FillRect(&rect, &brush);
UTIL_DrawNoScaleData(&dc, rect, m_pbmpInfo, m_pCaptData, NULL);
DrawWedge(&dc);
pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &dc, 0, 0, SRCCOPY);
if (poldbmp) dc.SelectObject(poldbmp);
brush.DeleteObject();
bmp.DeleteObject();
}
void CImageStatic::Gfp_DrawPolygonRgn(CDC* pDC)
{
//!< 仅支持非缩放时draw PolygonRgn, 未考虑显示比例不为1时的坐标转换
if (!m_bDrawNoScale) return;
int nsize = m_pointArray.GetSize();
if (nsize < 3 || !pDC || m_bDelFingerRgn)
return;
CPen newPen(PS_SOLID, 2, RGB(0, 255, 0));
CPen* pOldPen = pDC->SelectObject(&newPen);
CPoint ptStart, pt, ptOff;
// ptOff = m_rtFrame.TopLeft();
ptOff = *(CPoint*)&m_rtFrame; //这里其实不需要计算偏移
ptStart = m_pointArray.GetAt(0) - ptOff;
pDC->MoveTo(ptStart);
for (int i = 1; i < nsize; i++)
{
pt = m_pointArray.GetAt(i) - ptOff;
pDC->LineTo(pt);
}
if (pt != ptStart)
pDC->LineTo(ptStart);
pDC->SelectObject(pOldPen);
newPen.DeleteObject();
}
int CImageStatic::Gfp_BeginMakePolygonRgn(CPoint point)
{
if (!m_bMakeFingerRgn)
{
if (m_pointArray.GetSize() > 0)
{
m_pointArray.RemoveAll();
m_bDelFingerRgn = FALSE;
}
m_pointArray.Add(point);
m_ptLast = point;
m_ptPrev = point;
m_bMakeFingerRgn = TRUE;
}
if (m_ptLast != point)
{
m_pointArray.Add(point);
CClientDC dc(this);
CPen newPen(PS_SOLID, 2, RGB(0, 255, 0));
CPen* pOldPen = dc.SelectObject(&newPen);
dc.MoveTo(m_ptLast);
dc.LineTo(point);
dc.SelectObject(pOldPen);
newPen.DeleteObject();
m_ptLast = point;
m_ptPrev = point;
}
return 1;
}
int CImageStatic::Gfp_MakePolygonRgn(CPoint point)
{
if (!m_bMakeFingerRgn)
return 0;
CClientDC dc(this);
CPen newPen(PS_SOLID, 2, RGB(0, 255, 0));
CPen* pOldPen = dc.SelectObject(&newPen);
int oldROP2 = dc.SetROP2(R2_XORPEN);
// if(m_ptLast != m_ptPrev)
{
dc.MoveTo(m_ptLast);
dc.LineTo(m_ptPrev);
}
// if(m_ptPrev != point)
{
dc.MoveTo(m_ptLast);
dc.LineTo(point);
}
m_ptPrev = point;
dc.SelectObject(pOldPen);
dc.SetROP2(oldROP2);
newPen.DeleteObject();
return 1;
}
int CImageStatic::Gfp_EndMakePolygonRgn(CPoint point, BOOL bLastPt /* = FALSE */)
{
if (!m_bMakeFingerRgn)
return 0;
if (bLastPt)
{
m_bMakeFingerRgn = FALSE;
int nsize = m_pointArray.GetSize(), nret = -1;
if (nsize < 2)
{
m_pointArray.RemoveAll();
nret = 0;
}
if (nsize == 2 && (point == m_ptLast || point == m_pointArray.GetAt(0)))
{
m_pointArray.RemoveAll();
nret = 0;
}
if (nret != 0)
{
if (m_ptLast != point)
m_pointArray.Add(point);
nret = 1;
}
if (m_bClipCursor)
{
ClipCursor(&m_rtOldClipCursor);
m_bClipCursor = FALSE;
}
DrawImage();
return nret;
}
if (m_ptLast != point)
{
m_pointArray.Add(point);
CClientDC dc(this);
CPen newPen(PS_SOLID, 2, RGB(0, 255, 0));
CPen* pOldPen = dc.SelectObject(&newPen);
dc.MoveTo(m_ptLast);
dc.LineTo(point);
dc.SelectObject(pOldPen);
newPen.DeleteObject();
m_ptLast = point;
m_ptPrev = point;
}
return 1;
}
BOOL CImageStatic::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
return CStatic::PreTranslateMessage(pMsg);
}
BOOL CImageStatic::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此添加专用代码和/或调用基类
GetClipCursor(&m_rtOldClipCursor);
return CStatic::PreCreateWindow(cs);
}
int CImageStatic::GetFingerRgn(vct_rgn_t& vctRgns)
{
int nsize = m_pointArray.GetSize();
if (nsize < 3 || m_bDelFingerRgn)
return -1;
CPoint pt, ptOff;
ptOff.x = abs(m_rtImage.left - m_rtFrame.left);
ptOff.y = abs(m_rtImage.top - m_rtFrame.top);
for (int i = 0; i < nsize; i++)
{
pt = m_pointArray.GetAt(i) - ptOff;
if (pt.x < 0)
pt.x = 0;
if (pt.y < 0) //虽然限定了鼠标区域,但第一个点似乎不受影响,可以点在top=0处,m_rtImage.top通常为2,导致坐标在图像区域外
pt.y = 0;
vctRgns.push_back(std::make_pair(pt.x, pt.y));
}
return 0;
}