Implementing drag-and-drop features of WebBrowser Control Winforms C #

I need to capture drag events of WebBrowser controls in WinForm C #. Is there any way?

+4
source share
5 answers

My worker:

use a boolean member variable to determine if you are invoking navigation from inside code, or redirects are invoked from ouside code. Introduce the BeforeNavigating event and see if it was an outside call.

Example:

the call moves from within:

private void NavigateLocal(string htmlFileName) { this.localNavigate = true; this.webBrowser.Navigate(htmlFileName); } 

event method:

  private void WebBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e) { if (this.localNavigate)//inside call { this.localNavigate = false; } else //call from outside { this.DoDragDrop(e.Url.ToString()); } } 
+2
source

The β€œfix” I am using is to let the browser tell you that it is dressed, taken a screenshot, and handles normal C # events.

 [ComVisible(true)] public class Communicator { private static frmMain m_mainWindow = null; public Communicator(frmMain mainWindow) { m_mainWindow = mainWindow; } public void exit() { m_mainWindow.Close(); } public void DragStarted(object e) { m_mainWindow.DoDragEnterFromScript(); } } private void frmMain_Load(object sender, EventArgs e) { webBrowser1.ObjectForScripting = new Communicator(this);; webBrowser1.Navigate(@"about:blank"); } public void DoDragEnterFromScript() { // Is this an executable, shortcut or batch? Rectangle rect = new Rectangle(0, 0, webBrowser1.Size.Width, webBrowser1.Size.Height); Bitmap bmp = new Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmp); Point ptCon; ptCon = webBrowser1.PointToScreen(new Point(0, 0)); g.CopyFromScreen(ptCon, new Point(0, 0), webBrowser1.Size, CopyPixelOperation.SourceCopy); pictureBox2.BackgroundImage = bmp; //pictureBox2.BackColor = Color.Red; webBrowser1.Visible = false; pictureBox2.Visible = true; pictureBox2.Location = new Point(0, 0); pictureBox2.Size = webBrowser1.Size; } public void frmMain_DragEnter(object sender, DragEventArgs e) { if (!e.Data.GetDataPresent(DataFormats.FileDrop)) return; string[] arrfiles = (string[])e.Data.GetData(DataFormats.FileDrop); // SUCCESS } 

And in the html file

 var holder = document.getElementsByTagName('body')[0]; holder.ondragover = function () { window.external.DragStarted(event); return true; }; 

Do not forget to hide the image window after DragLeave / DragDrop

+1
source

Doesn’t the standard drag and drop method work?

http://www.codeproject.com/KB/cs/dragdrop.aspx

0
source

Web Browser Settings You can also look at IDocHostUIHandler :: GetDropTarget if you want to control what the WebBrowser control does during drag and drop operations.

The easiest way to connect the implementation of IDocHostUIHandler is to connect the ICustomDoc interface and its entire SetUIHandler method (see http://www.codeproject.com/csharp/advhost.asp ). However, this method has a memory leak . Another way is to skip the Windows Form web browser class and directly use ActiveX host support .

0
source

The best way I've found is to handle the Navigating event in the control through the host. When you delete files in a control, this event fires, and you can select file names at this time. The trick is that you should know what legal navigation is (i.e. a document that moves directly against the file being deleted). Most applications that handle call forwarding events use a specific document or a set of known documents, which should not be a problem.

Another option is to handle the drop operation using window.ondrop inside the HTML document using JavaScript code, but you can only get the file data and file name, not the full path.

For more information on both approaches, take a look at this blog post: https://weblog.west-wind.com/posts/2017/Mar/10/Dragging-and-Dropping-Images-and-Files-into-the- Web browser control

0
source

All Articles