Created
May 1, 2017 16:48
-
-
Save itshaadi/cb3876dfdd55938ac09b230969c63a6e to your computer and use it in GitHub Desktop.
workaround for drag and drop in CefSharp (winfrom)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using Gma.System.MouseKeyHook; // for simulating OnDragDrop https://github.com/gmamaladze/globalmousekeyhook | |
using System.Windows.Forms; | |
using CefSharp; | |
public partial class MainForm : Form | |
{ | |
private IList<string> dropFiles; | |
private ChromiumWebBrowser Browser; | |
public class DragDropHandler : IDragHandler | |
{ | |
public bool OnDragEnter(IWebBrowser browserControl, IBrowser browser, IDragData dragData, DragOperationsMask mask) | |
{ | |
this.dropFiles = dragData.FileNames; | |
/* | |
for example drag and drop a photo, | |
if this method returns false then browser will show the photo, | |
and if returns true browser will not handle files by default (you can still drop files in and do whatever you want) | |
*/ | |
return true; | |
} | |
public void OnDraggableRegionsChanged(IWebBrowser browserControl, IBrowser browser, IList<DraggableRegion> regions) | |
{ | |
} | |
} | |
public MainForm() | |
{ | |
InitializeComponent(); | |
Subscribe(); | |
Browser = new ChromiumWebBrowser("URL"); | |
Browser.Dock = DockStyle.Fill; | |
Browser.DragHandler = new DragDropHandler(); | |
this.Controls.Add(Browser); | |
} | |
/* global mouse Drag hook */ | |
public void Subscribe() | |
{ | |
m_GlobalHook = Hook.GlobalEvents(); | |
m_GlobalHook.MouseDragFinished += GlobalHookDragFinished; | |
} | |
private void GlobalHookDragFinished(object sender, MouseEventArgs args) | |
{ | |
/* | |
Detect if cursor is within the bounds of a control, | |
so we are actually listening for any Drag Finished event | |
and if cursor is inside our window then this event belongs to our program | |
this.DesktopBounds.Contains(Cursor.Position) | |
*/ | |
if (this.DesktopBounds.Contains(Cursor.Position) && this.dropFiles != null) | |
{ | |
this.OnDragDrop(); | |
/* You have to clear the results to prevent endless loop */ | |
this.dropFiles = null; | |
} | |
else if(!this.DesktopBounds.Contains(Cursor.Position) && this.dropFiles != null ) | |
{ | |
/* also avoid using MessageBox becasue it will interrupt this hook and cause endless loop */ | |
this.Text = "drop in canceld"; | |
this.dropFiles = null; | |
} | |
} | |
private void Unsubscribe() | |
{ | |
m_GlobalHook.MouseUpExt -= GlobalHookDragFinished; | |
m_GlobalHook.Dispose(); | |
} | |
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) | |
{ | |
Unsubscribe(); | |
} | |
/* now you can do whaterver you want */ | |
private void OnDragDrop() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment