I am trying to associate a non-static member of a class with the standard WNDPROC function. I know that I can just do this by setting the class member to static. But, as a student of C ++ 11 STL, I am very interested in this, using the tools under the <functional> heading.
My code is as follows.
class MainWindow { public: void Create() { WNDCLASSEXW WindowClass; WindowClass.cbSize = sizeof(WNDCLASSEX); WindowClass.style = m_ClassStyles; WindowClass.lpfnWndProc = std::function<LRESULT(HWND, UINT, WPARAM, LPARAM)> ( std::bind(&MainWindow::WindowProc, *this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); WindowClass.cbClsExtra = 0; WindowClass.cbWndExtra = 0; WindowClass.hInstance = m_hInstance; WindowClass.hIcon = LoadIconW(m_hInstance, MAKEINTRESOURCEW(IDI_WINDOW)); WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW); WindowClass.hbrBackground = (HBRUSH) COLOR_WINDOW; WindowClass.lpszMenuName = MAKEINTRESOURCEW(IDR_MENU); WindowClass.lpszClassName = m_ClassName.c_str(); WindowClass.hIconSm = LoadIconW(m_hInstance, MAKEINTRESOURCEW(IDI_WINDOW_SMALL)); RegisterClassExW(&WindowClass); m_hWnd = CreateWindowEx( ExtendedStyles, m_ClassName.c_str(), m_WindowTitle.c_str(), m_Styles, m_x, m_y, m_Width, m_Height, HWND_DESKTOP, NULL, WindowClass.hInstance, NULL); } private: LRESULT CALLBACK WindowProc(_In_ HWND hwnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam) { return DefWindowProc(hwnd, uMsg, wParam, lParam); } };
When I run it as is, it gives an error message:
Error: no suitable conversion function from "std::function<LRESULT(HWND, UINT, WPARAM, LPARAM)>" to "WNDPROC".