WebKit.NET C# Custom Context Menu - winforms

I am implementing a Webkit Browser control in my windows app.
I need to use a custom context menu (right click) that only has copy/cut/paste as its options regardless of what element is right clicked. I need kind of a step-by-step as to how to implement it

Customizing the context menu for the WebKitBrowser supposes that you get a reference to the WebViewClass and then, setting a IWebUIDelegate for it by calling the setUIDelegate() method.
void MyWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webView = this.GetWebView() as WebKit.Interop.WebViewClass;
webView.setUIDelegate(new MyWebUIDelegate(this));
}
In the IWebUIDelegate implementation you may intercept the contextMenuItemsForElement method and trigger the display of the context menu of the browser.
Here is a working sample:
public partial class Form1 : Form
{
MyWebBrowser webKitBrowser;
public Form1()
{
InitializeComponent();
webKitBrowser = new MyWebBrowser();
webKitBrowser.Dock = DockStyle.Fill;
this.Controls.Add(webKitBrowser);
webKitBrowser.Navigate("http://www.google.com");
}
}
class MyContextMenu : ContextMenu
{
public MyContextMenu()
{
var cutMenuItem = new MenuItem("Cut");
var copyMenuItem = new MenuItem("Copy");
var pasteMenuItem = new MenuItem("Paste");
cutMenuItem.Click += cutMenuItem_Click;
MenuItems.Add(cutMenuItem);
MenuItems.Add(copyMenuItem);
MenuItems.Add(pasteMenuItem);
}
void cutMenuItem_Click(object sender, EventArgs e)
{
//TODO: implement functionality
MessageBox.Show("Cut was selected");
}
}
class MyWebBrowser : WebKitBrowser
{
public event EventHandler ShowContextMenu = new EventHandler(OnFireShowContextMenu);
public MyWebBrowser()
{
DocumentCompleted += MyWebBrowser_DocumentCompleted;
var myContextMenu = new MyContextMenu();
ContextMenu = myContextMenu;
}
void MyWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webView = this.GetWebView() as WebKit.Interop.WebViewClass;
webView.setUIDelegate(new MyWebUIDelegate(this));
}
public static void OnFireShowContextMenu(object sender, EventArgs e)
{
var webBrowser = (Control)sender;
var webView = (WebKit.Interop.WebViewClass)((MyWebBrowser)sender).GetWebView();
var originalPoint = webBrowser.PointToScreen(new Point(0, 0));
var currentPoint = new Point(Cursor.Position.X - originalPoint.X, Cursor.Position.Y - originalPoint.Y);
((WebKitBrowser)sender).ContextMenu.Show((Control)sender, currentPoint);
}
public void FireShowContextMenu()
{
this.ShowContextMenu(this, null);
}
}
class MyWebUIDelegate : IWebUIDelegate
{
private MyWebBrowser owner;
public MyWebUIDelegate(MyWebBrowser browser)
{
this.owner = browser;
}
//trigger the browser's FireShowContextMenu() method
public int contextMenuItemsForElement(WebView sender, CFDictionaryPropertyBag element, int defaultItemsHMenu)
{
owner.FireShowContextMenu();
return defaultItemsHMenu;
}
//return 1, true
public int hasCustomMenuImplementation()
{
return 1;
}
//the rest of the IWebUIDelegate interface implementation
}
For more insight, probably you would want to study some other customizations, such as open-webkit-sharp.

Related

How to set PopUpWindow to be on the same monitor as the application? (xaf)

The user often moves my XAF Winforms app between monitors.
However dialogs invoked by the PopUpWindowShowAction remain on the old monitor
This issue can be repeated with Dev Express's Main Demo ( although to debug it there I think I would need to get into the API code).
Run Main Demo and move the application window to the second monitor.
Select Users, right click a user and select Print Preview.
The Print Preview will appear on the first monitor.
In my similar case, my controller is
public partial class JobHeadFilterController : ViewController, IClassicController
{
public JobHeadFilterController()
{
InitializeComponent();
TargetViewNesting = Nesting.Root;
}
protected override void OnActivated()
{
base.OnActivated();
popupWindowShowFilterAction.QuickAccess = true;
}
private void popupWindowShowFilterAction_CustomizePopupWindowParams(object sender,
CustomizePopupWindowParamsEventArgs e)
{
var holder = new JobHeadFilterHolder();
var npProvider = new NonPersistentObjectSpaceProvider(XafTypesInfo.Instance, null);
var os = (NonPersistentObjectSpace)npProvider.CreateObjectSpace();
var view = Application.CreateDetailView(os, holder);
e.View = view;
}
I have tried browsing the properties of CustomizePopupWindowParamsEventArgs but cant see what to set.
I can get the main form using
var mainform = Application.MainWindow.Template as Form;
I think I need to do something like the following inside popupWindowShowFilterAction_CustomizePopupWindowParams
var template = e.Application.MainWindow.Template;
var window = new Window(Application, ); // having trouble figuring out the parameters
e.DialogController.SetWindow(window);
Studying the docs
[Update]
I tried
var window = e.Application.MainWindow;
e.DialogController.SetWindow(window);
but got an error
The 'DevExpress.ExpressApp.SystemModule.DialogController' controller
is active.
I tried making my own dialog controller inheriting from the Dev DevExpress.ExpressApp.SystemModule.DialogController but got the same error.
using System;
using System.Windows.Forms;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Templates;
using DevExpress.ExpressApp.Win.Templates;
namespace MyNameSpace;
public partial class CustomizeFormsWindowController : WindowController
{
public CustomizeFormsWindowController()
{
InitializeComponent();
TargetWindowType = WindowType.Child;
}
protected override void OnActivated()
{
base.OnActivated();
Window.TemplateChanged += WindowsTemplateChanged;
}
private void WindowsTemplateChanged(object sender, EventArgs e)
{
if (Window.Template is Form &&
Window.Template is ISupportStoreSettings)
((ISupportStoreSettings)Window.Template).SettingsReloaded +=
OnFormReadyForCustomizations;
}
private void OnFormReadyForCustomizations(object sender, EventArgs e)
{
if (sender is not PopupForm popupForm) return;
var mainForm = Application.MainWindow.Template as Form;
var X = mainForm.Location.X + (mainForm.Width - popupForm.Width) / 2;
var Y = mainForm.Location.Y + (mainForm.Height - popupForm.Height) / 2;
popupForm.SetDesktopLocation(X, Y);
}
protected override void OnDeactivated()
{
Window.TemplateChanged -= WindowsTemplateChanged;
base.OnDeactivated();
}
}

Getting issue with the dependency injection asp core 6 with winforms creation?

Github link to sample project
static void Main()
{
ApplicationConfiguration.Initialize();
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddTransient<Form1>();
services.AddTransient<Form2>();
});
var host = builder.Build();
using (var serviceScope = host.Services.CreateScope())
{
IServiceProvider services = serviceScope.ServiceProvider;
Application.Run(services.GetRequiredService<Form1>());
}
}
Form1 is MDI MdiParent where i am injecting Form 2
public partial class Form1 : Form
{
private readonly Form2 form2;
public Form1(Form2 form2)
{
InitializeComponent();
this.form2 = form2;
}
private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.form2.MdiParent = this;
this.form2.Show();
}
}
When I Open Form2 by clicking from Menu it opens and close it by using [X] button
When i reopen it i am getting error
The form is disposed when closed.
I would suggest using a factory
static void Main() {
ApplicationConfiguration.Initialize();
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) => {
services.AddTransient<Form1>();
services.AddTransient<Form2>();
//Form2 factory delegate
services.AddSingleton<Func<Form2>>(sp => () => sp.GetRequiredService<Form2>());
});
var host = builder.Build();
using (var serviceScope = host.Services.CreateScope()) {
IServiceProvider services = serviceScope.ServiceProvider;
Application.Run(services.GetRequiredService<Form1>());
}
}
to initialize a new form every time the button is clicked.
public partial class Form1 : Form {
private readonly Func<Form2> factory;
public Form1(Func<Form2> factory) {
InitializeComponent();
this.factory = factory;
}
private void form2ToolStripMenuItem_Click(object sender, EventArgs e) {
Form2 form2 = factory();
form2.MdiParent = this;
form2.Show();
}
}

How to automatically refresh listbox when add or remove the item in WPF?

I have WPF application that uses web service (asmx). The web service uses EF to get the data from MS SQL Server.
The code looks as following:
1) WPF:
public partial class MainWindow : Window
{
LetterWebServiceSoapClient _client = new LetterWebServiceSoapClient();
private ObservableCollection<Letter> _letters;
public MainWindow()
{
InitializeComponent();
}
private void cmdGetLetters_Click(object sender, RoutedEventArgs e)
{
lstLetters.ItemsSource = null;
_letters = _client.GetAllLetters();
lstLetters.ItemsSource = _letters;
}
private void cmdDeleteLetter_Click(object sender, RoutedEventArgs e)
{
_client.DeleteLetter((Letter)lstLetters.SelectedItem);
}
private void cmdAddLetter_Click(object sender, RoutedEventArgs e)
{
var newLetter = new Letter
{
Name = "Letter3",
Date = DateTime.Now,
Recipient = "John",
Sender = "David",
Content = "cccc"
};
_client.AddNewLetter(newLetter);
}
}
2) The web service:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class LetterWebService : System.Web.Services.WebService
{
LetterDbEntities _letterDbEntities = new LetterDbEntities();
[WebMethod]
public ObservableCollection<Letter> GetAllLetters()
{
return new ObservableCollection<Letter>(_letterDbEntities.Letters.ToList());
}
[WebMethod]
public void AddNewLetter(Letter newLetter)
{
_letterDbEntities.Letters.Add(newLetter);
_letterDbEntities.SaveChanges();
}
[WebMethod]
public void DeleteLetter(Letter letter)
{
var letterToBeDeleted = _letterDbEntities.Letters.First(l => l.Id == letter.Id);
_letterDbEntities.Letters.Remove(letterToBeDeleted);
_letterDbEntities.SaveChanges();
}
}
When I add new letter or remove existing one they are added or removed on database level, but it doesn't reflect in UI that is in list box. What I'm missing?
You need to change the _letters collection. Try this:
private void cmdDeleteLetter_Click(object sender, RoutedEventArgs e)
{
var selectedItem = (Letter)lstLetters.SelectedItem;
_client.DeleteLetter(selectedItem);
_letters.Remove(selectedItem);
}
private void cmdAddLetter_Click(object sender, RoutedEventArgs e)
{
var newLetter = new Letter
{
Name = "Letter3",
Date = DateTime.Now,
Recipient = "John",
Sender = "David",
Content = "cccc"
};
_client.AddNewLetter(newLetter);
_letters.Add(newLetter);
}

How to create and use WebBrowser in background thread?

How can I create System.Windows.Forms.WebBrowser in background STA thread? I try use some code like this:
var tr = new Thread(wbThread);
tr.SetApartmentState(ApartmentState.STA);
tr.Start();
private void wbThread()
{
CWebBrowser browser = new CWebBrowser();
var text = browser.Navigate("http://site.com", CWebBrowser.EventType.loadCompleted).Body.InnerHtml;
}
CWebBrowser - custom class, wich delegate System.Windows.Forms.WebBrowser object Navigate method and wait until page completed loads. The problem is LoadCompleted event on System.Windows.Forms.WebBrowser object never raises. I found some solution here, but it does not work (can't find method Application.Run() on my WPF app).
public class CWebBrowser : ContentControl
{
public readonly System.Windows.Forms.WebBrowser innerWebBrowser;
private readonly AutoResetEvent loadCompletedEvent;
private readonly AutoResetEvent navigatedEvent;
public enum EventType
{
navigated, loadCompleted
}
public CWebBrowser()
{
innerWebBrowser = new System.Windows.Forms.WebBrowser();
loadCompletedEvent = new AutoResetEvent(false);
navigatedEvent = new AutoResetEvent(false);
System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
host.Child = innerWebBrowser;
Content = host;
innerWebBrowser.DocumentCompleted +=new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(innerWebBrowser_DocumentCompleted);
innerWebBrowser.Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(innerWebBrowser_Navigated);
}
void innerWebBrowser_Navigated(object sender, System.Windows.Forms.WebBrowserNavigatedEventArgs e)
{
navigatedEvent.Set();
}
void innerWebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
if (((sender as System.Windows.Forms.WebBrowser).ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) || innerWebBrowser.IsBusy)
return;
var doc = innerWebBrowser.Document;
loadCompletedEvent.Set();
}
public System.Windows.Forms.HtmlDocument Navigate(string url, EventType etype)
{
if (etype == EventType.loadCompleted)
loadCompletedEvent.Reset();
else if (etype == EventType.navigated)
navigatedEvent.Reset();
innerWebBrowser.Navigate(url);
if (etype == EventType.loadCompleted)
loadCompletedEvent.WaitOne();
else if (etype == EventType.navigated)
navigatedEvent.WaitOne();
System.Windows.Forms.HtmlDocument doc = null;
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(
delegate
{
doc = innerWebBrowser.Document;
}));
return doc;
}
}
Thansk for all advices and sorry for my bad english :o(
Why don't you use the default WebBrowser control like this?
public MainPage()
{
InitializeComponent();
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(startNavigate);
}
void startNavigate()
{
WebBrowser wb = new WebBrowser();
wb.LoadCompleted += new LoadCompletedEventHandler(wb_LoadCompleted);
wb.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(wb_Navigated);
wb.Navigate(new Uri("http://www.google.com"));
}
void wb_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
// e.Content
}
void wb_LoadCompleted(object sender, NavigationEventArgs e)
{
// e.Content when the document finished loading.
}
Edit: You are using old System.Windows.Forms.WebBrowser control, instead System.Windows.Controls.WebBrowser which is part of WPF.

How do I capture key down in WPF?

How do I capture a key down event in WPF even if my application is not focused?
For me, the best way is this:
public MainWindow()
{
InitializeComponent();
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
}
void CompositionTarget_Rendering(object sender, EventArgs e)
{
if ((Keyboard.GetKeyStates(Key.W) & KeyStates.Down) > 0)
{
player1.walk();
}
}
The rendering event runs every time.
Global keyboard hook can slow down your debugging.
I prefer to use this approach:
Create KeyboardListener class
public class KeyboardListener : IDisposable
{
private readonly Thread keyboardThread;
//Here you can put those keys that you want to capture
private readonly List<KeyState> numericKeys = new List<KeyState>
{
new KeyState(Key.D0),
new KeyState(Key.D1),
new KeyState(Key.D2),
new KeyState(Key.D3),
new KeyState(Key.D4),
new KeyState(Key.D5),
new KeyState(Key.D6),
new KeyState(Key.D7),
new KeyState(Key.D8),
new KeyState(Key.D9),
new KeyState(Key.NumPad0),
new KeyState(Key.NumPad1),
new KeyState(Key.NumPad2),
new KeyState(Key.NumPad3),
new KeyState(Key.NumPad4),
new KeyState(Key.NumPad5),
new KeyState(Key.NumPad6),
new KeyState(Key.NumPad7),
new KeyState(Key.NumPad8),
new KeyState(Key.NumPad9),
new KeyState(Key.Enter)
};
private bool isRunning = true;
public KeyboardListener()
{
keyboardThread = new Thread(StartKeyboardListener) { IsBackground = true };
keyboardThread.Start();
}
private void StartKeyboardListener()
{
while (isRunning)
{
Thread.Sleep(15);
if (Application.Current != null)
{
Application.Current.Dispatcher.Invoke(() =>
{
if (Application.Current.Windows.Count > 0)
{
foreach (var keyState in numericKeys)
{
if (Keyboard.IsKeyDown(keyState.Key) && !keyState.IsPressed) //
{
keyState.IsPressed = true;
KeyboardDownEvent?.Invoke(null, new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromDependencyObject(Application.Current.Windows[0]), 0, keyState.Key));
}
if (Keyboard.IsKeyUp(keyState.Key))
{
keyState.IsPressed = false;
}
}
}
});
}
}
}
public event KeyEventHandler KeyboardDownEvent;
/// <summary>
/// Состояние клавиши
/// </summary>
private class KeyState
{
public KeyState(Key key)
{
this.Key = key;
}
public Key Key { get; }
public bool IsPressed { get; set; }
}
public void Dispose()
{
isRunning = false;
Task.Run(() =>
{
if (keyboardThread != null && !keyboardThread.Join(1000))
{
keyboardThread.Abort();
}
});
}
}
Subscribe to KeyboardDownEvent in code-behind (or where you need it).
public partial class MainWindow : Window
{
private KeyboardListener listener;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
listener = new KeyboardListener();
listener.KeyboardDownEvent += ListenerOnKeyPressed;
}
private void ListenerOnKeyPressed(object sender, KeyEventArgs e)
{
// TYPE YOUR CODE HERE
}
private void Window_OnUnloaded(object sender, RoutedEventArgs e)
{
listener.KeyboardDownEvent -= ListenerOnKeyPressed;
}
}
Done
See this questions for hooking the keyboard Using global keyboard hook (WH_KEYBOARD_LL) in WPF / C#

Resources