一般 std::function 成员
Generic std::function member
我有一个使用 EnumWindows 的 class。因为这需要回调,所以我将它包装成一个不错的小工具 class 类似于:
Class Tools
{
public:
template<typename WinFunctor>
std::vector<HWND> FindWindow(WinFunctor Functor);
private:
BOOL EnumWindowMsgProc(HWND hWnd);
static BOOL CALLBACK FreeWndProc(HWND hWnd, LPARAM lParam)
{
Tools* pThis = (Tools*)lParam;
return pThis->EnumWindowMsgProc(hWnd);
}
std::vector<HWND> m_Windows;
/*Need to store WinFunctor Here*/
}
BOOL Tools::EnumWindowMsgProc(HWND hWnd)
{
if(/*Call WinFunctor Member here*/)
{
m_Windows.push_back(hWnd);
}
return TRUE;
}
template<typename WinFunctor>
std::vector<HWND> Tools::FindWindow(WinFunctor Functor)
{
m_Windows.clear();
EnumWindows(FreeWndProc, (LPARAM)this);
return m_Windows;
}
/*Windows Callbacks must be free (not a class member),
so I define a static method (free) and forward to my
member function(not free)*/
WinFunctor 示例:
bool EnumByWindowName(HWND WinHandle,const std::wstring& WinName)
{
wchar_t Temp[1024]{L'[=12=]'};
GetWindowText(WinHandle, Temp, 1024);
if (std::wstring(Temp).compare(WinName.c_str()) == 0)
return true;
return false;
}
所需界面示例
Tools ToolInst;
auto Windows=ToolsInst.FindWindow(EnumByWindowName(std::placeholders::_1,"Notepad-Untitled"));
我需要以某种方式将 Functor 存储为成员,以便稍后在回调中调用它,但我不能只模板化 class 因为这需要我创建一个新的工具实例每次我想搜索不同的 window(并且工具 class 拥有比 EnumWindows 更多的功能)。Functor 必须始终接受一个 hWnd,然后可以使用它想要的任何东西对该数据进行操作,并且可能会传递对其采取行动所需的其他参数(例如 WindowName sting)。是否有存储仿函数的方法,而不必每次都创建 class 的新实例。感谢您的帮助
Piotr S 发布的解决方案
Class Tools
{
public:
typedef std::function<bool(HWND)> WinFunctor;
std::vector<HWND> FindWindow(const WinFunctor& Functor);
private:
BOOL EnumWindowMsgProc(HWND hWnd);
static BOOL CALLBACK FreeWndProc(HWND hWnd, LPARAM lParam)
{
Tools* pThis = (Tools*)lParam;
return pThis->EnumWindowMsgProc(hWnd);
}
std::vector<HWND> m_Windows;
WinFunctor m_WinFunctor;
}
BOOL Tools::EnumWindowMsgProc(HWND hWnd)
{
if(m_WinFunctor(hWnd))
m_Windows.push_back(hWnd);
return TRUE;
}
std::vector<HWND> Tools::FindWindow(const WinFunctor& Functor)
{
m_Windows.clear();
m_WinFunctor=Functor;
EnumWindows(FreeWndProc, (LPARAM)this);
return m_Windows;
}
接口:
auto Windows = m_Tools.FindParentWindow(std::bind(&WinEnumFunctors::EnumByWindowName, std::placeholders::_1, L"Some WindowName"));
我有一个使用 EnumWindows 的 class。因为这需要回调,所以我将它包装成一个不错的小工具 class 类似于:
Class Tools
{
public:
template<typename WinFunctor>
std::vector<HWND> FindWindow(WinFunctor Functor);
private:
BOOL EnumWindowMsgProc(HWND hWnd);
static BOOL CALLBACK FreeWndProc(HWND hWnd, LPARAM lParam)
{
Tools* pThis = (Tools*)lParam;
return pThis->EnumWindowMsgProc(hWnd);
}
std::vector<HWND> m_Windows;
/*Need to store WinFunctor Here*/
}
BOOL Tools::EnumWindowMsgProc(HWND hWnd)
{
if(/*Call WinFunctor Member here*/)
{
m_Windows.push_back(hWnd);
}
return TRUE;
}
template<typename WinFunctor>
std::vector<HWND> Tools::FindWindow(WinFunctor Functor)
{
m_Windows.clear();
EnumWindows(FreeWndProc, (LPARAM)this);
return m_Windows;
}
/*Windows Callbacks must be free (not a class member),
so I define a static method (free) and forward to my
member function(not free)*/
WinFunctor 示例:
bool EnumByWindowName(HWND WinHandle,const std::wstring& WinName)
{
wchar_t Temp[1024]{L'[=12=]'};
GetWindowText(WinHandle, Temp, 1024);
if (std::wstring(Temp).compare(WinName.c_str()) == 0)
return true;
return false;
}
所需界面示例
Tools ToolInst;
auto Windows=ToolsInst.FindWindow(EnumByWindowName(std::placeholders::_1,"Notepad-Untitled"));
我需要以某种方式将 Functor 存储为成员,以便稍后在回调中调用它,但我不能只模板化 class 因为这需要我创建一个新的工具实例每次我想搜索不同的 window(并且工具 class 拥有比 EnumWindows 更多的功能)。Functor 必须始终接受一个 hWnd,然后可以使用它想要的任何东西对该数据进行操作,并且可能会传递对其采取行动所需的其他参数(例如 WindowName sting)。是否有存储仿函数的方法,而不必每次都创建 class 的新实例。感谢您的帮助
Piotr S 发布的解决方案
Class Tools
{
public:
typedef std::function<bool(HWND)> WinFunctor;
std::vector<HWND> FindWindow(const WinFunctor& Functor);
private:
BOOL EnumWindowMsgProc(HWND hWnd);
static BOOL CALLBACK FreeWndProc(HWND hWnd, LPARAM lParam)
{
Tools* pThis = (Tools*)lParam;
return pThis->EnumWindowMsgProc(hWnd);
}
std::vector<HWND> m_Windows;
WinFunctor m_WinFunctor;
}
BOOL Tools::EnumWindowMsgProc(HWND hWnd)
{
if(m_WinFunctor(hWnd))
m_Windows.push_back(hWnd);
return TRUE;
}
std::vector<HWND> Tools::FindWindow(const WinFunctor& Functor)
{
m_Windows.clear();
m_WinFunctor=Functor;
EnumWindows(FreeWndProc, (LPARAM)this);
return m_Windows;
}
接口:
auto Windows = m_Tools.FindParentWindow(std::bind(&WinEnumFunctors::EnumByWindowName, std::placeholders::_1, L"Some WindowName"));