3

In my WPF application, I implemented drag & drop to open files from Windows Explorer using

  • AllowDrop="True"
  • DragOver="MainWindow_DragOver"
  • Drop="MainWindow_Drop"

no problem, everything works fine.

But if a dialog box is open I can always do Drag/Drop files from Windows Explorer into the main window of my application. Have you ever encountered this problem? Can you help me solve it? Thank you

chriga
  • 798
  • 2
  • 12
  • 29
Cogito
  • 31
  • 1

2 Answers2

1

If your dialog box is a modal one, it disables the parent window. This can be checked using the IsWindowEnabled API call. (I am no WPF expert, but the .IsEnabled or .Focusable properties do not seem to work this way.)

void MainWindow_DragOver(object sender, DragEventArgs e)
{
    bool isEnabled = NativeMethods.IsWindowEnabled(new WindowInteropHelper(this).Handle);
    e.Effects = isEnabled ? DragDropEffects.Copy :  DragDropEffects.None;
    e.Handled = true;
}

class NativeMethods
{
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsWindowEnabled(IntPtr hWnd);
}

see also https://stackoverflow.com/a/6363095/9156214

Cornix
  • 19
  • 4
  • This works for me. Bad thing that MS-Windows allows drag drop on disabled windows ... any idea how I could automate that in the whole application without checking the enabled state in each dragover function? – Lumo Mar 16 '18 at 14:13
0

When the dialog box opens, disallow dropping on your main form. Make sure you open a modal dialog (ShowDialog instead of Show).

Bas
  • 26,772
  • 8
  • 53
  • 86