Disable drag and drop from webbrowser control c# - winforms

I am trying to prevent users from dragging images that display in my webbrowser control.
I tried setting webbrowser.Document.MouseOver e.Cursor = Cursors.No Still not working.
I tried a few other ways.
I am unable to prevent images being dragged on the desktop.
Is it possible to prevent dragging of images from webbrowser control to desktop?

There is a way to prevent the body element of the HTML document to handle drag operation.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
this.webBrowser1.Url = new Uri("https://www.google.bg/search?q=stackoverflow&biw=1920&bih=950&source=lnms&tbm=isch&sa=X&sqi=2&ved=0ahUKEwjw2cWH4oTQAhVG8RQKHWCWB4AQ_AUIBigB");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.webBrowser1.Document.Body.Drag += new HtmlElementEventHandler(Body_Drag);
}
private void Body_Drag(object sender, HtmlElementEventArgs e)
{
e.ReturnValue = false;
}
}

Related

How to hide the Download dialog in WebView2 while downloading page content or printing with "Save as PDF" option?

I know how to subscribe to the CoreWebview2.DownloadStarting event and use handled = true to stop the Download dialog from showing while a download (i.e. an image) is being made, but the problem is the DownloadStarting event never fires if you right click on a web page and choose "Save as" or "Print > Save as PDF", even though the Download dialog will appear as if a regular download was being made. Does anyone know any workaround for this?
My code:
public Form1()
{
InitializeComponent();
}
private async void button1_Click(object sender, EventArgs e)
{
if (webView != null)
webView.Dispose();
webView = new WebView2();
await webView.EnsureCoreWebView2Async();
webView.CoreWebView2.DownloadStarting += CoreWebView2_DownloadStarting;
panel1.Controls.Add(webView);
webView.Dock = DockStyle.Fill;
webView.Source = new Uri("https://www.microsoft.com");
}
private void CoreWebView2_DownloadStarting(object sender, CoreWebView2DownloadStartingEventArgs e)
{
e.Handled = true;
}
I just found a solution:
subscribe to CoreWebView2.IsDefaultDownloadDialogOpenChanged event:
webView.CoreWebView2.IsDefaultDownloadDialogOpenChanged += webView_CoreWebView2_IsDefaultDownloadDialogOpenChanged;
Close the Download dialog if it's open:
private void webView_CoreWebView2_IsDefaultDownloadDialogOpenChanged(object sender, object e)
{
if (webView.CoreWebView2.IsDefaultDownloadDialogOpen) webView.CoreWebView2.CloseDefaultDownloadDialog();
}

dynamically add webbrowser control in windows chat application:

I am developing windows form chat application and i am writing message in richtextbox but when i click on send button and every message show in webbrowser control. .i want to add webbrowser control dynamically for individual message is send.i am using following code:
namespace WindowsFormsApplication1
{
[ComVisible(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// this.w1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(w1_DocumentCompleted);
}
WebBrowser w1 = new WebBrowser();
private void button1_Click(object sender, EventArgs e)
{
this.Controls.Add(w1);
HtmlElement div = w1.Document.GetElementById("abc");
div.InnerHtml = richTextBox1.Text;
}
private void Form1_Load(object sender, EventArgs e)
{
w1.Navigate(Path.Combine(Environment.CurrentDirectory, "HTMLPageForScripting.htm"));
}
}
}
Here is the case you asked for:
Add a Panel to form to put browser in and set its autoscroll property to true;
In button1 Click event, create browser, set its width and height, and navigate to the document you want.
In button1 Click event subscribe for DocumentCompleted event and add content there. We do add here because we should be sure all contents containing <div id="abc"></div>has been loaded.
Here is code:
private void button1_Click(object sender, EventArgs e)
{
var browser = new WebBrowser();
browser.Height = 100;
browser.Dock = DockStyle.Top;
browser.Navigate(#"D:\test.html");
browser.DocumentCompleted += browser_DocumentCompleted;
this.panel1.Controls.Add(browser);
//Just do it to put it at the end of list and scroll to it
browser.BringToFront();
this.panel1.ScrollControlIntoView(browser);
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var browser = (WebBrowser)sender;
HtmlElement div = browser.Document.GetElementById("abc");
div.InnerHtml = richTextBox1.Text;
}

Access to the Ok/Accept Or Cancel Button of a RepositoryItemTimeSpanEdit?

I need to know if there is a way to access to the events of the buttons inside a RepositoryItemTimeSpanEdit.
Image to see the buttons I need the events for: Click
Image
I have tried to access in the PopUp event and QueryPopUp, however I can't get the button in any way yet.
You can get this form through Form.OwnedForms property in Popup event. The type of this form is DevExpress.XtraEditors.Popup.TimeSpanEditDropDownForm, so you need just to find the form of this type. After that you can access buttons by using TimeSpanEditDropDownForm.OkButton property and TimeSpanEditDropDownForm.CloseButton property.
Here is example:
private void repositoryItemTimeSpanEdit1_Popup(object sender, EventArgs e)
{
var popupForm = (TimeSpanEditDropDownForm)OwnedForms.FirstOrDefault(item => item is TimeSpanEditDropDownForm);
if (popupForm == null)
return;
popupForm.OkButton.Click += OkButton_Click;
popupForm.CloseButton.Click += CloseButton_Click;
}
private void OkButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Ok");
}
private void CloseButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Cancel");
}

New tab in WPF - items.add from buttons vs methods

OK, so I have a window called PictureWindow which displays pictures (I've cut out the code not related to making tabs). The TabControl is named "itemsTab". Using a button press, I can make a new tab no problem. But using the same operations inside a called method doesn't work. Using the buttonTab_Click method makes a new tab, the newTab method does not.
The only real difference I can see is due to the sender and RoutedEventArgs objects - how do these effect the operation here? Or is there something else I'm missing?
Thanks in advance.
Edit To make things even stranger, the newTab method does make a new tab, but only if it is called in the PictureWindow constructor method. If I have the following a new tab is made.
public PictureWindow(string current)
{
InitializeComponent();
newTab(current);
}
But if I call the method anywhere else it doesn't work.
public partial class PictureWindow : Window
{
public PictureWindow(string current)
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void buttonClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void buttonTab_Click(object sender, RoutedEventArgs e)
{
TabItem newTab = new TabItem();
newTab.Header = "New Tab!";
itemsTab.Items.Add(newTab);
}
public void newTab(string current)
{
TabItem newTab = new TabItem();
itemsTab.Items.Add(newTab);
}
}

WinForms - WPF like painting

We know very well how easy is too create a WPF application where user can paint a rectangle using the mouse. To do this you just create a Rectangle control and set its coordinates, you don't worry about DoubleBuffering, repainting and such stuff. Well, I'd be very happy yo use WPF for the application where user can paint different shapes, but the clients insists to be a WinForms application. So the solution here is to use the XOR or ROP operation like in old good WinAPI years and I don't really like this. This doesn't give me a nice option to move a text while in XOR mode.
So I was thinking how can I achieve same smooth painting experience in a WinForms application like I'd have in WPF. Put together such a code, where I wanted to create a separate layer where I'd paint the current shape, while leaving intact the rest of the objects. I used same technique in an iPad application and worked pretty well.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace TestPainting
{
public partial class Form1 : Form
{
private bool _isMouseDown;
private Graphics _bufferGraphics;
private Point _startPos;
private TransparentPanel _paintBuffer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
_isMouseDown = true;
_paintBuffer = new TransparentPanel
{
Size = Size,
};
Controls.Add(_paintBuffer);
_paintBuffer.BringToFront();
_bufferGraphics = Graphics.FromHwnd(_paintBuffer.Handle);
_startPos = e.Location;
Capture = true;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (!_isMouseDown)
return;
_bufferGraphics.Clear(Color.Transparent);
_bufferGraphics.DrawRectangle(Pens.Green, _startPos.X, _startPos.Y, e.X - _startPos.X, e.Y - _startPos.Y);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
_isMouseDown = false;
Capture = false;
_bufferGraphics.Dispose();
Controls.Remove(_paintBuffer);
_paintBuffer.Dispose();
}
}
public class TransparentPanel : Panel
{
public TransparentPanel()
{
DoubleBuffered = true;
}
[Browsable(false)]
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// Do Nothing
}
}
}
which if course doesn't work as needed. I'm getting a black panel when pressing the mouse instead of a transparent one. Plus the rectangle while is painted flickers a lot, even though I did set the DoubleBuffering stuff.
Can someone provide some better ideas of such an implementation or maybe there some other open source project where I can see how other people are doing. I'd need to have same experience as in Paint.NET, just too bad is not open source anymore. (I know I can use Reflector, and I did, but man, there is tons of code over there :) )
Thx for any ideas.
Try this (see FIX #1 and FIX #2):
private void Form1_MouseDown( object sender, MouseEventArgs e )
{
_isMouseDown = true;
_paintBuffer = new TransparentPanel
{
Size = Size,
};
Controls.Add( _paintBuffer );
_paintBuffer.BringToFront();
// FIX #1:
//
this.Refresh();
_bufferGraphics = Graphics.FromHwnd( _paintBuffer.Handle );
_startPos = e.Location;
Capture = true;
}
private void Form1_MouseMove( object sender, MouseEventArgs e )
{
if ( !_isMouseDown )
return;
//FIX #2:
// _bufferGraphics.Clear( Color.Transparent );
_bufferGraphics.Clear( this.BackColor );
_bufferGraphics.DrawRectangle( Pens.Green, _startPos.X, _startPos.Y, e.X - _startPos.X, e.Y - _startPos.Y );
}

Resources