windows_manager.cpp 2.68 KB
Newer Older
刘乐's avatar
刘乐 committed
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
#include "stdafx.h"
#include "windows_manager.h"

namespace nim_comp
{
WindowsManager::WindowsManager()
{
	stop_register_ = false;
	windows_map_.clear();
}

WindowsManager::~WindowsManager()
{
	windows_map_.clear();
}

bool WindowsManager::RegisterWindow(const std::wstring wnd_class_name, const std::wstring wnd_id, WindowEx *wnd)
{
	if (IsStopRegister())
	{
		assert(!stop_register_);
		return false;
	}
	WindowsMap::iterator it = windows_map_.find(wnd_class_name);
	if (it != windows_map_.end())
	{
		std::map<std::wstring, WindowEx*>::iterator it2 = it->second.find(wnd_id);
		it->second[wnd_id] = wnd;
	}
	else
	{
		std::map<std::wstring, WindowEx*> id_win;
		id_win[wnd_id] = wnd;
		windows_map_[wnd_class_name] = id_win;
	}
	return true;
}

void WindowsManager::UnRegisterWindow(const std::wstring &wnd_class_name, const std::wstring &wnd_id, WindowEx *wnd)
{
	WindowsMap::iterator it = windows_map_.find(wnd_class_name);
	if (it != windows_map_.end())
	{
		std::map<std::wstring, WindowEx*>::iterator it2 = it->second.find(wnd_id);
		if (it2 != it->second.end())
		{
			it->second.erase(it2);
		}
	}
}

WindowList WindowsManager::GetAllWindows()
{
	WindowList list;
	WindowsMap::iterator it = windows_map_.begin();
	for (; it != windows_map_.end(); ++it)
	{
		std::map<std::wstring, WindowEx*>::iterator it2 = it->second.begin();
		for (; it2 != it->second.end(); ++it2)
		{
			WindowEx *wnd = (WindowEx *)(it2->second);
			if (wnd && ::IsWindow(wnd->GetHWND()))
			{
				list.push_back(wnd);
			}
		}
	}
	return list;
}

WindowEx* WindowsManager::GetWindow(const std::wstring &wnd_class_name, const std::wstring &wnd_id)
{
	WindowsMap::iterator it = windows_map_.find(wnd_class_name);
	if (it != windows_map_.end())
	{
		std::map<std::wstring, WindowEx*>::iterator it2 = it->second.find(wnd_id);
		if (it2 != it->second.end())
		{
			WindowEx* wnd = (WindowEx*)(it2->second);
			if (wnd && ::IsWindow(wnd->GetHWND()))
			{
				return wnd;
			}
		}
	}
	return NULL;
}

WindowList WindowsManager::GetWindowsByClassName(LPCTSTR classname)
{
	WindowList wnd_list;
	WindowsMap::iterator it = windows_map_.find(classname);
	if (it != windows_map_.end())
	{
		std::map<std::wstring, WindowEx*>::iterator it2 = it->second.begin();
		for (; it2 != it->second.end(); it2++)
		{
			WindowEx* wnd = (WindowEx*)(it2->second);
			if (wnd && ::IsWindow(wnd->GetHWND()))
			{
				wnd_list.push_back(wnd);
			}
		}
	}
	return wnd_list;
}

void WindowsManager::DestroyAllWindows()
{
	SetStopRegister();

	WindowList lst_wnd = GetAllWindows();
	WindowList::iterator it = lst_wnd.begin();
	for (; it != lst_wnd.end(); ++it)
	{
		WindowEx *wnd = (WindowEx *)*it;
		if (wnd && ::IsWindow(wnd->GetHWND()))
		{
			::DestroyWindow(wnd->GetHWND());
		}
	}
}
}