[32] | 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 | }
|
---|
| 30 |
|
---|
| 31 | WindowFinder::~WindowFinder(void)
|
---|
| 32 | {
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | HWND WindowFinder::find(WindowData * winData) {
|
---|
| 36 | currentWindow = winData;
|
---|
| 37 | maxScore = 0;
|
---|
| 38 | scores = new std::vector<std::pair<HWND, int>>();
|
---|
| 39 |
|
---|
| 40 | if( parentHandles==NULL ) {
|
---|
| 41 | EnumWindows(EnumChildren, (LPARAM) this);
|
---|
| 42 | } else {
|
---|
| 43 | if( winData->isModal ) {
|
---|
| 44 | evalPopup = true;
|
---|
| 45 | EnumWindows(EnumChildren, (LPARAM) this);
|
---|
| 46 | evalPopup = false;
|
---|
| 47 | } else {
|
---|
| 48 | for( size_t i=0 ; i<parentHandles->size() ; i++ ) {
|
---|
| 49 | EnumChildWindows((*parentHandles)[i], EnumChildren, (LPARAM) this);
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | delete parentHandles;
|
---|
| 53 | }
|
---|
| 54 | parentHandles = new std::vector<HWND>();
|
---|
| 55 | for( size_t i=0 ; i<scores->size() ; i++ ) {
|
---|
| 56 | if( (*scores)[i].second==maxScore ) {
|
---|
| 57 | parentHandles->push_back((*scores)[i].first);
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | delete scores;
|
---|
| 61 |
|
---|
| 62 | HWND result = NULL;
|
---|
| 63 |
|
---|
| 64 | if( winData->child!=NULL ) {
|
---|
| 65 | if( maxScore==0 ) {
|
---|
| 66 | std::wcerr << L"Warning: Score is zero." << std::endl;
|
---|
| 67 | }
|
---|
| 68 | result = find(winData->child);
|
---|
| 69 | } else {
|
---|
| 70 | if( parentHandles->size()==0 ) {
|
---|
| 71 | std::wcerr << L"Error: handle not found." << std::endl;
|
---|
| 72 | result = NULL;
|
---|
| 73 | }
|
---|
| 74 | else {
|
---|
| 75 | if( parentHandles->size()!=1 ) {
|
---|
| 76 | std::wcerr << L"Warning: more than one fitting window found." << std::endl;
|
---|
| 77 | }
|
---|
| 78 | if( maxScore==0 ) {
|
---|
| 79 | std::wcerr << L"Warning: Score is zero." << std::endl;
|
---|
| 80 | }
|
---|
| 81 | result = (*parentHandles)[0];
|
---|
| 82 | delete parentHandles;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | return result;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 | void WindowFinder::setEqualityScore(HWND hwnd, int resourceId, wchar_t * windowName, wchar_t * className) {
|
---|
| 90 | int score = 0;
|
---|
| 91 |
|
---|
| 92 | if( resourceId!=0 && currentWindow->resourceId==resourceId && currentWindow->name.compare(windowName)==0 && currentWindow->className.compare(className)==0 ) {
|
---|
| 93 | score = 6;
|
---|
| 94 | }
|
---|
| 95 | else if( resourceId!=0 && currentWindow->resourceId==resourceId && currentWindow->name.compare(windowName)==0 ) {
|
---|
| 96 | score = 5;
|
---|
| 97 | }
|
---|
| 98 | else if( resourceId!=0 && currentWindow->resourceId==resourceId ) {
|
---|
| 99 | score = 4;
|
---|
| 100 | }
|
---|
| 101 | else if( currentWindow->name.compare(windowName)==0 && currentWindow->className.compare(className)==0 ) {
|
---|
| 102 | score = 3;
|
---|
| 103 | }
|
---|
| 104 | else if( currentWindow->name.compare(windowName)==0 ) {
|
---|
| 105 | score = 2;
|
---|
| 106 | }
|
---|
| 107 | else if( currentWindow->className.compare(className)==0 ) {
|
---|
| 108 | score = 1;
|
---|
| 109 | }
|
---|
| 110 | if( evalPopup ) {
|
---|
| 111 | HWND parent = GetParent(hwnd);
|
---|
| 112 | if( std::find(parentHandles->begin(), parentHandles->end(), parent)==parentHandles->end() ) {
|
---|
| 113 | score = 0;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | scores->push_back(std::make_pair(hwnd, score));
|
---|
| 118 |
|
---|
| 119 | if( score>maxScore ) {
|
---|
| 120 | maxScore = score;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | bool WindowFinder::isCurrentPopup() {
|
---|
| 125 | return evalPopup;
|
---|
| 126 | } |
---|