Add file drop functionality

Adds the ability to drag and drop files from windows in order to attempt
to execute them
This commit is contained in:
Leighton Hancock
2016-11-14 16:50:58 +13:00
parent f5168c5768
commit 524ba0c88c
7 changed files with 51 additions and 0 deletions

View File

@@ -26,6 +26,18 @@ class UIEvent {
Window* target_ = nullptr;
};
class FileDropEvent : public UIEvent {
public:
FileDropEvent(Window* target, wchar_t* filename)
: UIEvent(target), filename_(filename) {}
~FileDropEvent() override = default;
wchar_t* filename() const { return filename_; }
private:
wchar_t* filename_ = nullptr;
};
class KeyEvent : public UIEvent {
public:
KeyEvent(Window* target, int key_code, int repeat_count, bool prev_state)

View File

@@ -213,6 +213,11 @@ void Window::OnPaint(UIEvent* e) {
}
}
void Window::OnFileDrop(FileDropEvent* e) {
on_file_drop(e);
ForEachListener([e](auto listener) { listener->OnFileDrop(e); });
}
void Window::OnVisible(UIEvent* e) {
ForEachListener([e](auto listener) { listener->OnVisible(e); });
}

View File

@@ -102,6 +102,7 @@ class Window {
Delegate<UIEvent*> on_painting;
Delegate<UIEvent*> on_paint;
Delegate<UIEvent*> on_painted;
Delegate<FileDropEvent*> on_file_drop;
Delegate<KeyEvent*> on_key_down;
Delegate<KeyEvent*> on_key_up;
@@ -128,6 +129,7 @@ class Window {
virtual void OnResize(UIEvent* e);
virtual void OnLayout(UIEvent* e);
virtual void OnPaint(UIEvent* e);
virtual void OnFileDrop(FileDropEvent* e);
virtual void OnVisible(UIEvent* e);
virtual void OnHidden(UIEvent* e);

View File

@@ -31,6 +31,7 @@ class WindowListener {
virtual void OnPainting(UIEvent* e) {}
virtual void OnPaint(UIEvent* e) {}
virtual void OnPainted(UIEvent* e) {}
virtual void OnFileDrop(UIEvent* e) {}
virtual void OnVisible(UIEvent* e) {}
virtual void OnHidden(UIEvent* e) {}

View File

@@ -101,6 +101,8 @@ bool Win32Window::OnCreate() {
// Enable DWM elevation.
EnableMMCSS();
// Enable file dragging from external sources
DragAcceptFiles(hwnd_, true);
ShowWindow(hwnd_, SW_SHOWNORMAL);
UpdateWindow(hwnd_);
@@ -378,6 +380,21 @@ LRESULT Win32Window::WndProc(HWND hWnd, UINT message, WPARAM wParam,
}
switch (message) {
case WM_DROPFILES: {
TCHAR lpszFile[MAX_PATH] = {0};
UINT uFiles = 0;
HDROP hDrop = (HDROP)wParam;
// Get number of files dropped
uFiles = DragQueryFile(hDrop, -1, NULL, NULL);
// Only getting first file dropped (other files ignored)
if (DragQueryFile(hDrop, 0, lpszFile, MAX_PATH)) {
auto e = FileDropEvent(this, lpszFile);
OnFileDrop(&e);
}
DragFinish(hDrop);
} break;
case WM_NCCREATE:
break;
case WM_CREATE: