LAPTOP-SNT8I5JK\Boounion
2025-07-08 f0f942498d6b0c9a92ee2d7b9ea62a592b8f4cce
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include "stdafx.h"
#include "MapPosWnd.h"
#include "ColorTransfer.h"
 
#define HT_NOWHERE        0x1
#define HT_INDICATOR    0x2
 
CMapPosWnd::CMapPosWnd()
{
    m_hWnd = NULL;
    m_crFrame = RGB(0, 0, 0);
    m_crBkgnd = RGB(255, 255, 255);
    m_crViewPort = RGB(185, 122, 87);
 
    m_nWndMaxSize = 200;
    m_nStageCx = 4000;
    m_nStageCy = 3000;
    m_rcViewPort = {200, 200, 800, 800};
}
 
CMapPosWnd::~CMapPosWnd()
{
 
}
 
BOOL CMapPosWnd::RegisterWndClass()
{
    WNDCLASS wc;
    wc.lpszClassName = MAPPOSWND_CLASS;
    wc.hInstance = AfxGetInstanceHandle();
    wc.lpfnWndProc = WindowProc;
    wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = 0;
    wc.lpszMenuName = NULL;
    wc.hbrBackground = NULL;
    wc.style = CS_GLOBALCLASS | CS_DBLCLKS;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
 
    // ×¢²á´°¿ÚÀà
    return (::RegisterClass(&wc) != 0);
}
 
CMapPosWnd * CMapPosWnd::FromHandle(HWND hWnd)
{
    CMapPosWnd *pMapPosWnd = (CMapPosWnd *)::GetProp(hWnd, MAPPOSWND_TAG);
    return pMapPosWnd;
}
 
CMapPosWnd* CMapPosWnd::Hook(HWND hWnd)
{
    CMapPosWnd* pMapPosWnd = (CMapPosWnd*)GetProp(hWnd, MAPPOSWND_TAG);
    if (pMapPosWnd == NULL) {
        pMapPosWnd = new CMapPosWnd();
        pMapPosWnd->m_hWnd = hWnd;
 
        SetProp(hWnd, MAPPOSWND_TAG, (HANDLE)pMapPosWnd);
    }
 
 
    return pMapPosWnd;
}
 
void CMapPosWnd::SetWndMaxSize(int nMaxSize)
{
    m_nWndMaxSize = nMaxSize;
}
 
void CMapPosWnd::SetStageSize(int cx, int cy, BOOL bInvalidata)
{
    m_nStageCx = cx;
    m_nStageCy = cy;
 
    float scale = max(m_nStageCx / (float)m_nWndMaxSize, m_nStageCy / (float)m_nWndMaxSize);
    int w = (int)(m_nStageCx / scale) +1;
    int h = (int)(m_nStageCy / scale) + 1;
    ::SetWindowPos(m_hWnd, NULL, 0, 0, w, h, SWP_NOMOVE);
 
    if (bInvalidata) {
        RECT rcClient;
        GetClientRect(m_hWnd, &rcClient);
        InvalidateRect(m_hWnd, &rcClient, TRUE);
    }
}
 
void CMapPosWnd::SetViewPort(LPRECT lpRect, BOOL bInvalidata)
{
    ::CopyRect(&m_rcViewPort, lpRect);
    if (bInvalidata) {
        RECT rcClient;
        GetClientRect(m_hWnd, &rcClient);
        InvalidateRect(m_hWnd, &rcClient, TRUE);
    }
}
 
void CMapPosWnd::GetViewPortRect(LPRECT lprcClient, LPRECT lprcDest)
{
    RECT rcClient;
    if (lprcClient == NULL) {
        GetClientRect(m_hWnd, &rcClient);
        lprcClient = &rcClient;
    }
 
 
    float scale = max(m_nStageCx / (float)(lprcClient->right - lprcClient->left), m_nStageCy / (float)(lprcClient->bottom - lprcClient->top));
    lprcDest->left = long(m_rcViewPort.left / scale);
    lprcDest->top = long(m_rcViewPort.top / scale);
    lprcDest->right = long(m_rcViewPort.right / scale);
    lprcDest->bottom = long(m_rcViewPort.bottom / scale);
}
 
void CMapPosWnd::Init()
{
}
 
void CMapPosWnd::Release()
{
 
    // delete
    delete this;
}
 
void CMapPosWnd::Notify(int nCode, int dwData, int dwData1/* = 0*/, int dwData2/* = 0*/)
{
    HWND hParent;
    hParent = GetParent(m_hWnd);
    if (hParent != NULL) {
        MAPPOSWND_NMHDR nmhdr;
        nmhdr.nmhdr.hwndFrom = m_hWnd;
        nmhdr.nmhdr.idFrom = GetWindowLong(m_hWnd, GWL_ID);
        nmhdr.nmhdr.code = nCode;
        nmhdr.dwData = dwData;
        nmhdr.dwData1 = dwData1;
        nmhdr.dwData2 = dwData2;
        SendMessage(hParent, WM_NOTIFY, (WPARAM)nmhdr.nmhdr.idFrom, (LPARAM)&nmhdr);
    }
}
 
/*
 * ¼ì²â×ø±êµãËùÔÚµÄÏî
 * ·µ»ØÏîÀàÐÍ, ÈçHT_INDICATOR
 */
int CMapPosWnd::HighTest(POINT pt)
{
    // ¼ì²âÊÇ·ñÔÚij¸ö×ÓÏî
    int nRet = HT_NOWHERE;
 
    RECT rcClient, rcViewPort;
    GetClientRect(m_hWnd, &rcClient);
    GetViewPortRect(&rcClient, &rcViewPort);
    if (::PtInRect(&rcViewPort, pt)) {
        nRet = HT_INDICATOR;
    }
 
    return nRet;
}
 
/*
 * WindowProc£¬´°¿Ú¹ý³Ì
 */
LRESULT CALLBACK CMapPosWnd::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    CMapPosWnd* pMapPosWnd = (CMapPosWnd *)GetProp(hWnd, MAPPOSWND_TAG);
    if (pMapPosWnd == NULL && uMsg != WM_NCCREATE)
    {
        return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
 
 
    // ´¦Àí´°¿ÚÏûÏ¢
    ASSERT(hWnd);
    switch (uMsg)
    {
    case WM_NCCREATE:
        return CMapPosWnd::OnNcCreate(hWnd, wParam, lParam);
 
    case WM_DESTROY:
        return pMapPosWnd->OnDestroy(wParam, lParam);
 
    case WM_NCPAINT:
        return pMapPosWnd->OnNcPaint(wParam, lParam);
 
    case WM_PAINT:
        return pMapPosWnd->OnPaint(wParam, lParam);
 
    case WM_TIMER:
        return pMapPosWnd->OnTimer(wParam, lParam);
 
    case WM_MOUSEMOVE:
        return pMapPosWnd->OnMouseMove(wParam, lParam);
 
    case WM_LBUTTONDOWN:
        return pMapPosWnd->OnLButtonDown(wParam, lParam);
 
    case WM_LBUTTONDBLCLK:
        return pMapPosWnd->OnLButtonDblclk(wParam, lParam);
 
    case WM_MOUSEWHEEL:
        return pMapPosWnd->OnMouseWheel(wParam, lParam);
 
    case WM_KEYDOWN:
        return pMapPosWnd->OnKeyDown(wParam, lParam);
 
    case WM_SIZE:
        return pMapPosWnd->OnSize(wParam, lParam);
 
    case WM_GETDLGCODE:
        return DLGC_WANTALLKEYS;
 
    default:
        break;
    }
 
    return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
}
 
/*
 * WM_NCCREATE
 * ´°¿Ú´´½¨
 */
LRESULT CMapPosWnd::OnNcCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
    CMapPosWnd* pMapPosWnd = (CMapPosWnd *)GetProp(hWnd, MAPPOSWND_TAG);
    ASSERT(pMapPosWnd == NULL);
 
    Hook(hWnd)->Init();
    return ::DefWindowProc(hWnd, WM_NCCREATE, wParam, lParam);
}
 
/*
 * WM_DESTROY
 * ´°¿ÚÏú»Ù
 */
LRESULT CMapPosWnd::OnDestroy(WPARAM wParam, LPARAM lParam)
{
    Release();
    return ::DefWindowProc(m_hWnd, WM_DESTROY, wParam, lParam);
}
 
 
/*
 * WM_TIMER
 */
LRESULT CMapPosWnd::OnTimer(WPARAM wParam, LPARAM lParam)
{
 
    return ::DefWindowProc(m_hWnd, WM_TIMER, wParam, lParam);
}
 
/*
 * WM_MOUSEMOVE
 * Êó±ê¹ö¶¯
 */
LRESULT CMapPosWnd::OnMouseMove(WPARAM wParam, LPARAM lParam)
{
    return ::DefWindowProc(m_hWnd, WM_MOUSEMOVE, wParam, lParam);
}
 
/*
 * WM_LBUTTONDOWN
 * Êó±ê×ó¼ü°´ÏÂ
 */
LRESULT CMapPosWnd::OnLButtonDown(WPARAM wParam, LPARAM lParam)
{
    POINT pt, ptNew, ptPox;
    pt.x = LOWORD(lParam);
    pt.y = HIWORD(lParam);
 
    RECT rcClient, rcLastViewPort;
    GetClientRect(m_hWnd, &rcClient);
    float scale = max(m_nStageCx / (float)(rcClient.right - rcClient.left), m_nStageCy / (float)(rcClient.bottom - rcClient.top));
    CopyRect(&rcLastViewPort, &m_rcViewPort);
 
 
    // ¼ì²âµã»÷×ø±êÊÇ·ñÔÚijһ×ÓÏîÉÏ£¬ÈçÊÇ£¬Ôò¸ßÁÁÏÔʾ
    int nRet = HighTest(pt);
    SetFocus(m_hWnd);
 
 
    // ²¶×½Êó±êÏûÏ¢£¬¼ì²âÊÇ·ñÍ϶¯
    if (nRet == HT_INDICATOR) {
    
        if (::GetCapture() == NULL) {
            SetCapture(m_hWnd);
            ASSERT(m_hWnd == GetCapture());
            AfxLockTempMaps();
            for (;;) {
                MSG msg;
                VERIFY(::GetMessage(&msg, NULL, 0, 0));
 
                if (GetCapture() != m_hWnd) break;
 
                switch (msg.message)
                {
                case WM_MOUSEMOVE:
                    ptNew = msg.pt;
                    ::ScreenToClient(m_hWnd, &ptNew);
                    ptPox.x = long(rcLastViewPort.left + (ptNew.x - pt.x) * scale);
                    ptPox.x = max(0, min(ptPox.x, m_nStageCx - (rcLastViewPort.right - rcLastViewPort.left)));
                    ptPox.y = long(rcLastViewPort.top + (ptNew.y - pt.y) * scale);
                    ptPox.y = max(0, min(ptPox.y, m_nStageCy - (rcLastViewPort.bottom - rcLastViewPort.top)));
                    Notify(MAPPOSWND_POSCHANGED, ptPox.x, ptPox.y);
                    break;
 
                case WM_LBUTTONUP:
                    ptNew = msg.pt;
                    ::ScreenToClient(m_hWnd, &ptNew);
                    ptPox.x = long(rcLastViewPort.left + (ptNew.x - pt.x) * scale);
                    ptPox.x = max(0, min(ptPox.x, m_nStageCx - (rcLastViewPort.right - rcLastViewPort.left)));
                    ptPox.y = long(rcLastViewPort.top + (ptNew.y - pt.y) * scale);
                    ptPox.y = max(0, min(ptPox.y, m_nStageCy - (rcLastViewPort.bottom - rcLastViewPort.top)));
                    Notify(MAPPOSWND_POSCHANGED, ptPox.x, ptPox.y);
 
                    ReleaseCapture();
                    ::InvalidateRect(m_hWnd, &rcClient, TRUE);
                    goto ExitLoop;
 
                case WM_KEYDOWN:
                    if (msg.wParam != VK_ESCAPE)
                        break;
 
                default:
                    DispatchMessage(&msg);
                    break;
                }
            }
 
            ReleaseCapture();
        ExitLoop:
            AfxUnlockTempMaps(FALSE);
        }
    }
 
 
    return ::DefWindowProc(m_hWnd, WM_LBUTTONDOWN, wParam, lParam);
}
 
/*
 * WM_LBUTTONDBLCLK
 * Êó±ê×ó¼üË«»÷
 */
LRESULT CMapPosWnd::OnLButtonDblclk(WPARAM wParam, LPARAM lParam)
{
    POINT pt, ptDest;
    pt.x = LOWORD(lParam);
    pt.y = HIWORD(lParam);
 
    RECT rcClient, rcLast;
    GetClientRect(m_hWnd, &rcClient);
    rcLast = { 0, 0, 0, 0 };
 
    // ¼ì²âµã»÷×ø±êÊÇ·ñÔÚ¿Õ°×´¦
    // Ð¡´°×ø±êת»»Îª¶ÔӦ׸±ê
    int nRet = HighTest(pt);
    if (nRet == HT_NOWHERE || nRet == HT_INDICATOR) {
        float scale = max(m_nStageCx / (float)(rcClient.right - rcClient.left), m_nStageCy / (float)(rcClient.bottom - rcClient.top));
        ptDest.x = (int)(scale * pt.x) - (m_rcViewPort.right - m_rcViewPort.left) / 2;
        ptDest.x = max(0, min(ptDest.x, m_nStageCx - (m_rcViewPort.right - m_rcViewPort.left)));
        ptDest.y = (int)(scale * pt.y) - (m_rcViewPort.bottom - m_rcViewPort.top) / 2;
        ptDest.y = max(0, min(ptDest.y, m_nStageCy - (m_rcViewPort.bottom - m_rcViewPort.top)));
        Notify(MAPPOSWND_POSCHANGED, ptDest.x, ptDest.y);
    }
 
 
    return ::DefWindowProc(m_hWnd, WM_LBUTTONDBLCLK, wParam, lParam);
}
 
/*
 * WM_MOUSEWHEEL
 * Êó±ê¹ö¶¯
 */
LRESULT CMapPosWnd::OnMouseWheel(WPARAM wParam, LPARAM lParam)
{
    return ::DefWindowProc(m_hWnd, WM_MOUSEWHEEL, wParam, lParam);
}
 
/*
 * WM_KEYDOWN
 * ¼üÅÌÏûÏ¢£¬°´Ï°´¼ü
 */
LRESULT CMapPosWnd::OnKeyDown(WPARAM wParam, LPARAM lParam)
{
    BOOL bChanged = FALSE;
    if (wParam == VK_DELETE) {
 
    }
 
 
    if (bChanged) {
        RECT rcClient;
        GetClientRect(m_hWnd, &rcClient);
        ::InvalidateRect(m_hWnd, &rcClient, TRUE);
    }
 
    return ::DefWindowProc(m_hWnd, WM_KEYDOWN, wParam, lParam);
}
 
/*
 * WM_NCPAINT
 */
LRESULT CMapPosWnd::OnNcPaint(WPARAM wParam, LPARAM lParam)
{
    LRESULT lRet = ::DefWindowProc(m_hWnd, WM_NCPAINT, wParam, lParam);
 
 
    long styleEx = GetWindowLong(m_hWnd, GWL_EXSTYLE);
    if ((styleEx & WS_EX_CLIENTEDGE) == WS_EX_CLIENTEDGE) {
 
        RECT rect, rcClient;
        GetClientRect(m_hWnd, &rcClient);
        ::ClientToScreen(m_hWnd, (LPPOINT)&rcClient.left);
        ::ClientToScreen(m_hWnd, (LPPOINT)&rcClient.right);
        GetWindowRect(m_hWnd, &rect);
        rcClient.right = rect.right - 1;
        rcClient.bottom = rect.bottom - 1;
        ::OffsetRect(&rcClient, -rect.left, -rect.top);
 
        rect.right -= rect.left;
        rect.bottom -= rect.top;
        rect.left = 0;
        rect.top = 0;
 
        HRGN hRgnWnd = CreateRectRgnIndirect(&rect);
        HRGN hRgnClient = CreateRectRgnIndirect(&rcClient);
 
        HBRUSH hBrushBK, hBrushFrame;
        HDC hDC = ::GetWindowDC(m_hWnd);
        ::SelectClipRgn(hDC, hRgnWnd);
        ::ExtSelectClipRgn(hDC, hRgnClient, RGN_DIFF);
 
        hBrushBK = CreateSolidBrush(m_crBkgnd);
        ::FillRect(hDC, &rect, hBrushBK);
        DeleteObject(hBrushBK);
 
        hBrushFrame = CreateSolidBrush(m_crFrame);
        ::FrameRect(hDC, &rect, hBrushFrame);
 
        ::DeleteObject(hRgnWnd);
        ::DeleteObject(hRgnClient);
        DeleteObject(hBrushFrame);
        ::ReleaseDC(m_hWnd, hDC);
    }
 
    return lRet;
}
 
/*
 * WM_PAINT
 */
LRESULT CMapPosWnd::OnPaint(WPARAM wParam, LPARAM lParam)
{
    HDC hDC, hMemDC;
    HBITMAP hBitmap;
    RECT rcClient;
    CString strText;
    HBRUSH hBrushBK;
 
 
    // BeginPaint
    PAINTSTRUCT ps;
    hDC = BeginPaint(m_hWnd, &ps);
    GetClientRect(m_hWnd, &rcClient);
 
    hMemDC = ::CreateCompatibleDC(hDC);
    hBitmap = ::CreateCompatibleBitmap(hDC, rcClient.right - rcClient.left,
        rcClient.bottom - rcClient.top);
    ::SelectObject(hMemDC, hBitmap);
 
 
    // ±³¾°ÑÕÉ«
    hBrushBK = CreateSolidBrush(m_crBkgnd);
    ::FillRect(hMemDC, &rcClient, hBrushBK);
    DeleteObject(hBrushBK);
 
 
    // ±êÌâ
    {
        char szTitle[256];
        GetWindowText(m_hWnd, szTitle, 256);
        RECT rcTitle;
        rcTitle.left = rcClient.left + 2;
        rcTitle.top = rcClient.top + 2;
        rcTitle.bottom = rcClient.bottom - 2;
        rcTitle.right = rcClient.right - 2;
        ::DrawText(hMemDC, szTitle, (int)strlen(szTitle), &rcTitle, DT_LEFT | DT_TOP);
    }
 
 
    // View port
    RECT rcViewPort;
    GetViewPortRect(&rcClient, &rcViewPort);
    HBRUSH hBrushFrame = CreateSolidBrush(m_crViewPort);
    ::FrameRect(hMemDC, &rcViewPort, hBrushFrame);
    ::DeleteObject(hBrushFrame);
 
 
    // EndPaint
    ::BitBlt(hDC, 0, 0, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top,
        hMemDC, 0, 0, SRCCOPY);
    EndPaint(m_hWnd, &ps);
    ::DeleteObject(hBitmap);
    ::DeleteDC(hMemDC);
 
 
    return 1;
}
 
/*
 * WM_SIZE
 */
LRESULT CMapPosWnd::OnSize(WPARAM wParam, LPARAM lParam)
{
    LRESULT lRet = ::DefWindowProc(m_hWnd, WM_SIZE, wParam, lParam);
 
    return lRet;
}
 
/*
 * ÉèÖñ³¾°ÑÕÉ«
 * color -- ±³¾°É«
 */
void CMapPosWnd::SetBkgndColor(COLORREF color)
{
    m_crBkgnd = color;
}
 
/*
 * ±ß¿òÑÕÉ«
 * color -- ±ß¿òÉ«
 */
void CMapPosWnd::SetFrameColor(COLORREF color)
{
    m_crFrame = color;
}