3 #include "WindowSelect.h"
4 #include "../resource.h"
5 #include <utility> // for std::pair
7 using namespace Dasher
;
9 CWinSel::CWinSel(HWND Parent
, CEdit
*m_pEdit
)
10 :m_hwnd(0), m_pEdit(m_pEdit
)
12 g_hRectanglePen
= CreatePen(PS_SOLID
, 3, RGB(256, 0, 0));
13 g_bStartSearchWindow
= FALSE
;
14 g_hBitmapFinderToolFilled
= LoadBitmap(WinHelper::hInstApp
, MAKEINTRESOURCE(IDB_FINDER_FULL
));
15
g_hBitmapFinderToolEmpty
= LoadBitmap(WinHelper::hInstApp
, MAKEINTRESOURCE(IDB_FINDER_EMPTY
));
16 g_hCursorSearchWindow
= LoadCursor(WinHelper::hInstApp
, MAKEINTRESOURCE(IDC_SEARCH_CURSOR
));
17 DialogBoxParam(WinHelper::hInstApp
, (LPCTSTR
) IDD_WINSELECT
, Parent
, (DLGPROC
) WinWrapMap::WndProc
, (LPARAM
) this);
20 long CWinSel::RefreshWindow(HWND hwndWindowToBeRefreshed
)
{
long lRet
= 0;
InvalidateRect(hwndWindowToBeRefreshed
, NULL
, TRUE
);
UpdateWindow(hwndWindowToBeRefreshed
);
RedrawWindow(hwndWindowToBeRefreshed
, NULL
, NULL
, RDW_FRAME
| RDW_INVALIDATE
| RDW_UPDATENOW
| RDW_ALLCHILDREN
);
22
long CWinSel::SearchWindow(HWND hwndDialog
) {
23 long lRet
= 0;
g_bStartSearchWindow
= TRUE
;
24 // Set the screen cursor to the BullsEye cursor.
25 if(g_hCursorSearchWindow
) {
g_hCursorPrevious
= SetCursor(g_hCursorSearchWindow
);
}
26 else {
g_hCursorPrevious
= NULL
;
}
SetCapture(hwndDialog
);
SetFinderToolImage(hwndDialog
, FALSE
);
return lRet
;
}
28 long CWinSel::DoMouseMove(HWND hwndDialog
, UINT message
, WPARAM wParam
, LPARAM lParam
) {
POINT screenpoint
;
HWND hwndFoundWindow
= NULL
;
long lRet
= 0;
29 // Must use GetCursorPos() instead of calculating
// from "lParam".
GetCursorPos(&screenpoint);
30 // Determine the window that lies underneath
// the mouse cursor.
hwndFoundWindow = WindowFromPoint(screenpoint);
31 // We have just found a new window.
// If there was a previously found window, we must
// instruct it to refresh itself.
// This is done to remove any highlighting
// effects drawn by us.
32 if(g_hwndFoundWindow
) {
RefreshWindow(g_hwndFoundWindow
);
}
34 // Indicate that this found window is now
35 // the current global found window.
36 g_hwndFoundWindow
= hwndFoundWindow
;
38 // We now highlight the found window.
39 HighlightFoundWindow(hwndDialog
, g_hwndFoundWindow
);
return lRet
;
}
41 long CWinSel::HighlightFoundWindow(HWND hwndDialog
, HWND hwndFoundWindow
) {
42 // The DC of the found window.
45 // Handle of the existing pen in the DC of the found window.
46 HGDIOBJ hPrevPen
= NULL
;
47 // Handle of the existing brush in the DC of the found window.
48 HGDIOBJ hPrevBrush
= NULL
;
RECT rect
; // Rectangle area of the found window.
50 // Get the screen coordinates of the rectangle
51 // of the found window.
52 GetWindowRect(hwndFoundWindow
, &rect
);
53 // Get the window DC of the found window.
54 hWindowDC
= GetWindowDC(hwndFoundWindow
);
if(hWindowDC
) {
55 // Select our created pen into the DC and
// backup the previous pen.
hPrevPen = SelectObject(hWindowDC, g_hRectanglePen);
56 // Select a transparent brush into the DC and
// backup the previous brush.
hPrevBrush = SelectObject(hWindowDC,
GetStockObject(HOLLOW_BRUSH));
57 // Draw a rectangle in the DC covering
// the entire window area of the found window.
Rectangle(hWindowDC, 0, 0,
rect.right - rect.left, rect.bottom - rect.top);
58 // Reinsert the previous pen and brush
// into the found window's DC.
SelectObject(hWindowDC, hPrevPen);
SelectObject(hWindowDC, hPrevBrush);
59 // Finally release the DC.
ReleaseDC(hwndFoundWindow, hWindowDC);
}
return lRet;
}
61 long CWinSel::DoMouseUp(HWND hwndDialog
, UINT message
, WPARAM wParam
, LPARAM lParam
) {
long lRet
= 0;
62 // If we had a previous cursor, set the
// screen cursor to the previous one.
// The cursor is to stay exactly where it
// is currently located when the
// left mouse button is lifted.
if(g_hCursorPrevious) {
SetCursor(g_hCursorPrevious);
}
63 // If there was a found window, refresh
// it so that its highlighting is erased.
if(g_hwndFoundWindow) {
RefreshWindow(g_hwndFoundWindow);
65 // Very important : must release the mouse capture.
ReleaseCapture();
// Set the global search window flag to FALSE.
g_bStartSearchWindow = FALSE;
return lRet;
}
67 void CWinSel::SetFinderToolImage(HWND hwndDialog
, BOOL full
) {
68 HBITMAP hBmpToSet
= NULL
;
70 // Set a FILLED image.
71 hBmpToSet
= g_hBitmapFinderToolFilled
;
}
73 // Set an EMPTY image.
hBmpToSet = g_hBitmapFinderToolEmpty;
75 SendDlgItemMessage
(
(HWND
) hwndDialog
, // handle of dialog box
76 (int)IDC_DRAGCONTROL
, // identifier of control
77 (UINT
) STM_SETIMAGE
, // message to send
78 (WPARAM
) IMAGE_BITMAP
, // first message parameter
79 (LPARAM
) hBmpToSet
// second message parameter
83 LRESULT
CWinSel::WndProc(HWND Window
, UINT message
, WPARAM wParam
, LPARAM lParam
) {
85 case WM_MOUSEMOVE
: {
if(g_bStartSearchWindow
) {
86 // Only when we have started the
87 // Window Searching operation will we
88 // track mouse movement.
89 DoMouseMove(Window
, message
, wParam
, lParam
);
}
return TRUE
;
break;
}
90 case WM_LBUTTONUP
: {
if(g_bStartSearchWindow
)
{
91 // Only when we have started the
92 // window searching operation will we
93 // be interested when the user lifts
94 // up the left mouse button.
95 DoMouseUp(Window
, message
, wParam
, lParam
);
SetFinderToolImage(Window
, TRUE
);
}
return TRUE
;
break;
}
98 if(!m_hwnd
) { // If this is the initial dialog for the first time
105 switch (LOWORD(wParam
)) {
107 if(HIWORD(wParam
) == EN_CHANGE
) {
108 HWND Control
= GetDlgItem(Window
, IDC_DISPLAY
);
109 LRESULT BufferLength
= SendMessage(Control
, WM_GETTEXTLENGTH
, 0, 0) + 1; // +1 to allow for terminator
110 TCHAR
*Buffer
= new TCHAR
[BufferLength
];
111 SendMessage(Control
, WM_GETTEXT
, BufferLength
, (LPARAM
) Buffer
);
112 std::string ItemName
;
113 SendMessage(GetDlgItem(Window
, IDC_TEXT
), WM_SETTEXT
, 0, (LPARAM
) Buffer
);
118 m_pEdit
->SetWindow(g_hwndFoundWindow
);
122 EndDialog(Window
, LOWORD(wParam
));
125 case IDC_DRAGCONTROL
:
127 SearchWindow(Window
);