1 | #include "StdAfx.h"
|
---|
2 | #include "WindowFinder.h"
|
---|
3 |
|
---|
4 | #include <algorithm>
|
---|
5 | #include <iostream>
|
---|
6 |
|
---|
7 | BOOL CALLBACK EnumChildren(HWND hwnd, LPARAM lParam) {
|
---|
8 | int resourceId = 0;
|
---|
9 | int windowTextLen = 128;
|
---|
10 | wchar_t * windowText = new wchar_t[128];
|
---|
11 | int classNameLen = 128;
|
---|
12 | wchar_t * className = new wchar_t[128];
|
---|
13 |
|
---|
14 | WindowFinder * finder = (WindowFinder*) lParam;
|
---|
15 |
|
---|
16 | resourceId = GetDlgCtrlID(hwnd);
|
---|
17 | windowTextLen = GetWindowText(hwnd, windowText, windowTextLen);
|
---|
18 | classNameLen = GetClassName(hwnd, className, classNameLen);
|
---|
19 |
|
---|
20 | finder->setEqualityScore(hwnd,resourceId, windowText, className);
|
---|
21 |
|
---|
22 | return TRUE;
|
---|
23 | }
|
---|
24 |
|
---|
25 | WindowFinder::WindowFinder()
|
---|
26 | {
|
---|
27 | evalPopup = false;
|
---|
28 | parentHandles = NULL;
|
---|
29 | success = true;
|
---|
30 | }
|
---|
31 |
|
---|
32 | WindowFinder::~WindowFinder(void)
|
---|
33 | {
|
---|
34 | }
|
---|
35 |
|
---|
36 | HWND WindowFinder::find(WindowData * winData) {
|
---|
37 | currentWindow = winData;
|
---|
38 | maxScore = 0;
|
---|
39 | scores = new std::vector<std::pair<HWND, int>>();
|
---|
40 |
|
---|
41 | if( parentHandles==NULL ) {
|
---|
42 | EnumWindows(EnumChildren, (LPARAM) this);
|
---|
43 | } else {
|
---|
44 | if( winData->isModal ) {
|
---|
45 | evalPopup = true;
|
---|
46 | EnumWindows(EnumChildren, (LPARAM) this);
|
---|
47 | evalPopup = false;
|
---|
48 | } else {
|
---|
49 | for( size_t i=0 ; i<parentHandles->size() ; i++ ) {
|
---|
50 | EnumChildWindows((*parentHandles)[i], EnumChildren, (LPARAM) this);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | delete parentHandles;
|
---|
54 | }
|
---|
55 | parentHandles = new std::vector<HWND>();
|
---|
56 | for( size_t i=0 ; i<scores->size() ; i++ ) {
|
---|
57 | if( (*scores)[i].second==maxScore ) {
|
---|
58 | parentHandles->push_back((*scores)[i].first);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | delete scores;
|
---|
62 |
|
---|
63 | HWND result = NULL;
|
---|
64 |
|
---|
65 | if( winData->child!=NULL ) {
|
---|
66 | if( maxScore==0 ) {
|
---|
67 | std::wcerr << L"Warning: Score is zero." << std::endl;
|
---|
68 | errorMessage = L"score zero";
|
---|
69 | success = false;
|
---|
70 | }
|
---|
71 | result = find(winData->child);
|
---|
72 | } else {
|
---|
73 | if( parentHandles->size()==0 ) {
|
---|
74 | std::wcerr << L"Error: handle not found." << std::endl;
|
---|
75 | errorMessage = L"handle not found";
|
---|
76 | result = NULL;
|
---|
77 | success = false;
|
---|
78 | }
|
---|
79 | else {
|
---|
80 | if( parentHandles->size()!=1 ) {
|
---|
81 | std::wcerr << L"Warning: more than one fitting window found." << std::endl;
|
---|
82 | errorMessage = L"multiple matches";
|
---|
83 | success = false;
|
---|
84 | }
|
---|
85 | if( maxScore==0 ) {
|
---|
86 | std::wcerr << L"Warning: Score is zero." << std::endl;
|
---|
87 | errorMessage = L"score zero";
|
---|
88 | success = false;
|
---|
89 | }
|
---|
90 | result = (*parentHandles)[0];
|
---|
91 | delete parentHandles;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | return result;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | void WindowFinder::setEqualityScore(HWND hwnd, int resourceId, wchar_t * windowName, wchar_t * className) {
|
---|
99 | int score = 0;
|
---|
100 |
|
---|
101 | if( resourceId!=0 && currentWindow->resourceId==resourceId && currentWindow->name.compare(windowName)==0 && currentWindow->className.compare(className)==0 ) {
|
---|
102 | score = 6;
|
---|
103 | }
|
---|
104 | else if( resourceId!=0 && currentWindow->resourceId==resourceId && currentWindow->name.compare(windowName)==0 ) {
|
---|
105 | score = 5;
|
---|
106 | }
|
---|
107 | else if( resourceId!=0 && currentWindow->resourceId==resourceId ) {
|
---|
108 | score = 4;
|
---|
109 | }
|
---|
110 | else if( currentWindow->name.compare(windowName)==0 && currentWindow->className.compare(className)==0 ) {
|
---|
111 | score = 3;
|
---|
112 | }
|
---|
113 | else if( currentWindow->name.compare(windowName)==0 ) {
|
---|
114 | score = 2;
|
---|
115 | }
|
---|
116 | else if( currentWindow->className.compare(className)==0 ) {
|
---|
117 | score = 1;
|
---|
118 | }
|
---|
119 | if( evalPopup ) {
|
---|
120 | HWND parent = GetParent(hwnd);
|
---|
121 | if( std::find(parentHandles->begin(), parentHandles->end(), parent)==parentHandles->end() ) {
|
---|
122 | score = 0;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | scores->push_back(std::make_pair(hwnd, score));
|
---|
127 |
|
---|
128 | if( score>maxScore ) {
|
---|
129 | maxScore = score;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | bool WindowFinder::isCurrentPopup() {
|
---|
134 | return evalPopup;
|
---|
135 | }
|
---|
136 |
|
---|
137 | bool WindowFinder::successfull() {
|
---|
138 | return success;
|
---|
139 | }
|
---|
140 |
|
---|
141 | std::wstring WindowFinder::getErrorMessage() {
|
---|
142 | return errorMessage;
|
---|
143 | } |
---|