I'm trying to get files path through the drag and drop functionality offered by the windows.h
header but I encounter a problem when I want to encapsulate my code inside a class :
Work correctly :
LONG_PTR originalsfmlcallback;
LRESULT CALLBACK callback(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
{
if(message == WM_DROPFILES)
{
HDROP hdrop = reinterpret_cast<HDROP>(wParam);
WORD a = 0;
POINT p;
p.x = 0;
p.y = 0;
if(DragQueryPoint(hdrop, &p))
{
auto size = DragQueryFile(hdrop, a, (LPSTR) NULL, 0);
char fichier[size+1];
DragQueryFile(hdrop, a, fichier, sizeof(fichier));
std::cout << "FILE : [" << size << "] " << fichier << std::endl;
}
else
{
std::cout << "Failed to get point" << std::endl;
}
DragFinish(hdrop);
}
return CallWindowProcW(reinterpret_cast<WNDPROC>(originalsfmlcallback), handle, message, wParam, lParam);
}
int main()
{
const int w = 1000;
const int h = 800;
sf::RenderWindow window(sf::VideoMode(w, h), "SFML window");
DragAcceptFiles(handle, TRUE);
originalsfmlcallback = SetWindowLongPtrW(handle, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(callback));
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
window.clear();
//...draw something
window.display();
}
return EXIT_SUCCESS;
}
Get error :
class Dragger
{
Dragger(HWND handle)
{
DragAcceptFiles(handle, TRUE);
originalsfmlcallback = SetWindowLongPtrW(handle, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(callback)); //HERE THE ERROR : Reference to non-static...
}
private:
LRESULT CALLBACK callback(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
{
if(message == WM_DROPFILES)
{
HDROP hdrop = reinterpret_cast<HDROP>(wParam);
WORD a = 0;
POINT p;
p.x = 0;
p.y = 0;
if(DragQueryPoint(hdrop, &p))
{
auto size = DragQueryFile(hdrop, a, (LPSTR) NULL, 0);
char fichier[size+1];
DragQueryFile(hdrop, a, fichier, sizeof(fichier));
std::cout << "FILE : [" << size << "] " << fichier << std::endl;
}
else
{
std::cout << "Failed to get point" << std::endl;
}
DragFinish(hdrop);
}
return CallWindowProcW(reinterpret_cast<WNDPROC>(originalsfmlcallback), handle, message, wParam, lParam);
}
private:
LONG_PTR originalsfmlcallback;
};
int main()
{
const int w = 1000;
const int h = 800;
sf::RenderWindow window(sf::VideoMode(w, h), "SFML window");
Dragger dragger(window.getSystemHandle());
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
window.clear();
//...draw something
window.display();
}
return EXIT_SUCCESS;
}
I am not an expert in c++ and even less in winapi, there are certain features that I have difficulty understanding, such as preprocessor directives or macros. Thank you for your help !