I have two applications:
A legacy one, written in .net
A modern web application with cool html5/js features
I want to make them coexist so i'm considering integrating a WebBrowser ActiveX control in my .net application. I want to understand how "deep" i can make them communicate and more specifically:
Which type of information can i send from the webapp to parent .net app?
In Microsoft documentation, several events are associated to the WebBrowser class like Navigated which seems to be triggered every time the webapp's url changes. So i can imagine a mechanism which:
change the url
without affecting the webapp content
but passes some info to the .net app.
example: navigating to http://my.web.app/#event=hello&content=you will tell me that event type hello was triggered with content you.
That sounds like a tedious process, so:
Is there something higher level that this idea? At the beginning, i expected something like the window.postMessage method?
If not, is there a library that provides a higher level api to perform such things more easily?
I think you can build a bridge between javascript code in modern application and your C# host application using this technique
https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/implement-two-way-com-between-dhtml-and-client
It allows to execute C# code from JS as well as invoke JS from C#. You need to set ObjectForScripting property of WebBrowser control to C# object, methods of which will be called and then invoke it like
window.external.YourCSharpObjectMethod('called from JS code');
Related
This is something I've been trying to find out if it is possible and how to do it. I would like to be able to embed a Gtk+ control/widget within a Webkit view, integrated as part of the webpage. For instance, I was hoping that it could be possible to integrate a specific control in the middle of a programmatically generated webpage loaded onto a Webkit instance in a Gtk+ application. I'd like to make it a part of the DOM, if possible.
My intent is to use the webview as a basis for some parts of the UI, but there are some specific controls that needed to be the original Gtk+ ones.
When dealing with controls in forms, for instance, one of two things happen, either Webkit-gtk renders them explicitly to look like the Gtk+ controls, or makes use of actual Gtk +controls and embeds them. If the reality is the second case, then it might actually be easy to do it with other controls as well.
I'm developing the application in C# (mono) with GTK#, but I'm also hoping that if it is possible to do it directly in Gtk+, then it might eventually be possible to do in GTK#.
This used to be possible in WebKitGTK 1.x, but since WebKitGTK has become multiprocess, the capability has been removed.
However, you may be in luck, since GTK# uses quite old versions of libraries. I can't tell what version of WebKitGTK you're using, but if it has the create-plugin-widget signal then you should be able to do this.
I've never used this signal before, but I believe it works like this: you put an <object> tag into your HTML, and then the signal will be emitted when the webview needs to embed your widget. In the signal handler, you create your widget, and pass it as the return value of the signal handler.
You may want to look into Broadway. This is the web backend for GTK+, and uses HTML5 to display GTK+ widgets. You may have to use entirely GTK+ in your webpage, but this seems to be the only way to make a webpage containing GTK+ elements.
Is there way to reference or open a page in the windows store app built using javascript template from a C# project template?
UI from C# and JavaScript apps aren't able to interact with one another directly on the project level. Although you can create a Windows Runtime Component in C# or C++ and use it from a JavaScript app, there still isn't the ability to share UI.
The only want to host HTML/CSS/JS in a C# app is through a webview control. You can interact with the JavaScript in the webview via its InvokeScriptAsync method, and the JS can raise events through window.external.notify, but the JS won't have access to the WinRT APIs like a JavaScript app does.
One app can launch another, of course, through either file or protocol association, but that's a different matter than building different parts of a single app with different technologies. Within that scope, a JS app can again use non-UI WinRT components, and a C# app can host a webview. Those are really the options.
I'm being slowly driven mad at work having to manually gather data contained within an ActiveX object running in Internet Explorer 8. It's essentially a ticket system containing some key customer details etc. which I must copy and paste a lot more than is healthy.
Where would I start looking for a solution that will allow me to grab the data I want and navigate the ActiveX system programmatically?
Ideally I'd be thinking of writing something in Java or C# but I'm open to any suggestions!
Unless the ActiveX you are interested in exposes information through public interface (in which case you can injected JavaScript on page to read data) you'll have to use some form of screen scraping. If the control has regular input controls it should not be too hard, but if everything rendered directly you may need OCR the images...
Injecting script:
use WebBrowser control and "eval" your script on the page - some communication sample is here http://msdn.microsoft.com/en-us/library/a0746166%28v=VS.85%29.aspx, eventually you'll need InvokeScript - http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.invokescript%28v=VS.85%29.aspx
use normal IE automation http://msdn.microsoft.com/en-us/library/aa752084%28v=vs.85%29.aspx.
for one-time use "javascript:" in address bar or just developer tools (F12).
utilize XSS vulnerabilities of the site (half kidding, don't forget to fix whatever you find afterwards if you own the site, as for reporting errors to third party consult your conscience)
I'm building a C# desktop app with a simple UI. Due to my familiarity with HTML/CSS, and a previous web-based iteration of a very similar app, it would be ideal if I could re-use some existing HTML/CSS pages I have as the UI for this new desktop app.
Is that do-able with C#? Can anyone show a simple example of using an HTML/CSS page as the UI of a C# desktop application?
Other answers are dated, new good options:
Electron + Electron-Edge - is the way to go for a new Electron app that communicates to a .NET backend (a DLL). Widely used and supported.
Neutronium - best for already-written .NET WPF apps that need a HTML UI. Requires using Vue. Less popular and less supported.
CefSharp (https://github.com/cefsharp/CefSharp) is a good way to embed chromium, but you still need a web server. Take a look at http://www.ultidev.com/products/Cassini/ to alternative for IIS.
You can use WebBrowser control to add Web browser functionality to your application.
You can create your own html(s) and set your WebBrowser control to display those pages. You can navigate by clicking hyperlinks or by using keyboard shortcuts to move backward and forward through navigation history. By default, you can access additional browser functionality through the right-click shortcut menu
You can even manage those pages centrally (for e.g. on LAN) and all clients on LAN can connect to this central location. This way you can modify pages centrally.
Check a walkthrough on MSDN:
How to: Add Web Browser Capabilities to a Windows Forms Application
You can try to use a Chromium-based control - DotNetBrowser. This control provides wide interactivity options, like DOM manipulation and DOM events, which can be useful in your case.
In addition, there is an article that demonstrates how to use a web page as HTML UI in a C# desktop application: https://www.infoq.com/articles/html-desktop-net
Affiliation disclosure: I work with the team, which created DotNetBrowser.
You could try adding a Web Browser control to a forms application to show your page. But this will need to be remotely hosted as the code will not be able to execute locally, except if you listen for the specific links in c# and then perform the actions.
You could also try your hand at WPF which is a new technology to create good looking gui's with mark-up much like html, which you'll pick up pretty quickly with your html experience.
Unfortunately neither WinForms or WPF is sufficiently like HTML/CSS to be of that kind of use. You'll be able to reuse the layout-concepts and art of course. And, with WPF being the closest thing to it, you might even keep a lot of the code-behind code.
But the short answer is: No
I have a WinForms program written on .NET 2 which hosts a webbrowser control and renders asp.net pages from a known server.
I would like to be able to drag, say, a tree node from a treeview in my winforms app into a specific location in the hosted web page and have it trigger a javascript event there.
Currently, I can implement the IDocHostUIHandler interface and getting drag\drop events on the browser control, then call Navigate("javascript:fire_event(...)") on the control to execute a script on the page. However, I want this to work only when I drop data on a specific part of the page.
One solution, I suppose, would be to bite the bullet and write a custom browser plugin in the form of an activex control, embed that in the location I want to drop to and let that implement the needed drag\drop interfaces.
Would that work?
Is there a cleaner approach? Can I take advantage of the fact that the browser control is hosted in my app and provide some further level of interaction?
Take a look at the BrowserPlus project at Yahoo.
It looks like they have built a toolkit so that you don't have to do the gritty work of writing the browser plugin yourself.
If you can find out the on screen position of the part of the page you are interested in, you could compare this with the position of the mouse when you receive the drop event. I'm not sure how practical this is if you can get the info out of the DOM or whatnot.
As an alternative could you implement the mouse events on the bit of the page using javascript?