c# - WPF: Drag and drop a file into the whole window (titlebar and window borders included) -
in winforms , other applications (e.g. windows notepad), can drag , drop (e.g. file) whole window – includes title bar , window borders.
in wpf, can drag file window canvas – trying drag on title bar or window borders results in “no” cursor.
how can create ordinary wpf window (singleborderwindow windowstyle, etc.) take drag , drop whole window?
the difference wpf not calling os dragacceptfiles api when set allowdrop="true". dragacceptfiles registers entire window drop target.
you'll need pinvoke , have little window procedure handle drop message.
here's little test programme made allow wpf window take drop anywhere.
public partial class mainwindow : window { public mainwindow() { initializecomponent(); } const int wm_dropfiles = 0x233; [dllimport("shell32.dll")] static extern void dragacceptfiles(intptr hwnd, bool faccept); [dllimport("shell32.dll")] static extern uint dragqueryfile(intptr hdrop, uint ifile, [out] stringbuilder filename, uint cch); [dllimport("shell32.dll")] static extern void dragfinish(intptr hdrop); protected override void onsourceinitialized(eventargs e) { base.onsourceinitialized(e); var helper = new windowinterophelper(this); var hwnd = helper.handle; hwndsource source = presentationsource.fromvisual(this) hwndsource; source.addhook(wndproc); dragacceptfiles(hwnd, true); } private intptr wndproc(intptr hwnd, int msg, intptr wparam, intptr lparam, ref bool handled) { if (msg == wm_dropfiles) { handled = true; homecoming handledropfiles(wparam); } homecoming intptr.zero; } private intptr handledropfiles(intptr hdrop) { this.info.text = "dropped!"; const int max_path = 260; var count = dragqueryfile(hdrop, 0xffffffff, null, 0); (uint = 0; < count; i++) { int size = (int) dragqueryfile(hdrop, i, null, 0); var filename = new stringbuilder(size + 1); dragqueryfile(hdrop, i, filename, max_path); debug.writeline("dropped: " + filename.tostring()); } dragfinish(hdrop); homecoming intptr.zero; } }
c# wpf drag-and-drop
No comments:
Post a Comment