- Running Windows Forms on Linux with Mono
- Step 1 — Install Mono
- Step 2 — Create an Application
- Step 3 — Compile and Run
- Taking it Further
- About Mark Heath
- WinForms
- Quick Resources
- Code Status
- About System.Windows.Forms
- Contributing
- Drivers
- Accessibility
- Sample Code
- Theming
- Why Not Use Native Widgets?
- Fitting In
- Maturity of the Theming Interface
- History
- Webbrowser Control
- System windows forms linux
- Sound Code
- Friday, 6 June 2014
- Running Windows Forms on Linux with Mono
- Step 1 — Install Mono
- Step 2 — Create an Application
- Step 3 — Compile and Run
- Taking it Further
Running Windows Forms on Linux with Mono
Although WinForms may be “dead”, it does have one trick up its sleeve that WPF doesn’t, and that is you can run WinForms apps on mono. Here’s a simple guide to running a Windows Forms application on Ubuntu
Step 1 — Install Mono
Open a terminal window, and make sure everything is up to date with the following commands:
Now you can install mono with the following command:
Step 2 — Create an Application
Now we need to create our C# source file. You can use any text editor you like, but if like me you aren’t familiar with Linux text editors like vi or emacs, gedit is a simple notepad-like application which is easy to use. Launch it with the following command: (the ampersand at the end tells the terminal not to wait for gedit to close before letting us continue)
Now let’s create a very simple application:
Step 3 — Compile and Run
Now we’re ready to compile. The C# compiler in mono is gmcs. We’ll need to tell it we’re referencing the Windows Forms DLL:
To run the application, simply call mono, passing in the executable:
And that’s all there is to it! We have a WinForms app running on Linux.
Although mono doesn’t support everything in WinForms, you can use most standard controls, so you can easily add further UI elements:
Taking it Further
Obviously writing applications by hand like this is a bit cumbersome, but there is an IDE you can use for Linux called monodevelop. You install it like this:
This then gives you a nice editing environment, allowing you to debug, and manage project references (you’d usually add System.Windows.Forms and System.Drawing). Unfortunately it doesn’t offer a WinForms designer – for desktop apps it prefers you to use GTK#. Nevertheless, it’s a nice free IDE allowing you to experiment with getting your existing Windows Forms applications working cross-platform on Linux. (It seems this will also work on OS X with mono installed but I don’t have a Mac so I haven’t tried it out)
About Mark Heath
I’m a Microsoft MVP and software developer based in Southampton, England, currently working as a Software Architect for NICE Systems. I create courses for Pluralsight and am the author of several open source libraries. I currently specialize in architecting Azure based systems and audio programming. You can find me on:
Источник
WinForms
Quick Resources
Code Status
Support for Windows Forms 2.0 is complete. At this point, we are largely just fixing bugs and polishing our code.
About System.Windows.Forms
System.Windows.Forms (aka Managed.Windows.Forms, MWF, Winforms) is one of the many GUI Toolkits for use with Mono and is compatible with Microsoft’s System.Windows.Forms. Support for Winforms 1.1 and 2.0 has been completed, and is now in a maintenance/bug fixing state.
System.Windows.Forms in Mono is implemented using System.Drawing. All controls are natively drawn through System.Drawing. System.Windows.Forms implements its own driver interface to communicate with the host OS windowing system. Currently, we have drivers for X11, Win32, and macOS. These drivers translate the native window messages into WndProc compatible messages, to provide as much compatibility with native .Net as possible.
In terms of integrating visually with the desktop, we currently ship with a classic Win32 theme.
Contributing
Currently, all of the controls are pretty much complete. We are now mainly fixing bugs in our implementation. See Bugs.
If you would like to contribute, you can:
- If you find a bug in one of our methods, you can file a bug with a small test case.
- You can fix a bug in one of our methods, either that you found or from our bugs page.
Please note there are a few rules for contributing to Winforms:
- The general rules from Contributing, especially the Special Notes at the bottom.
- Controls must be drawn using System.Drawing code.
- Drawing code must be tied into the theming interface.
- If your method can be tested by an automated NUnit test, please include a test as well.
If you are working on a large feature, you may wish to post your intent on GitHub to prevent duplication of effort.
Drivers
There is a per-windowing system driver which performs the following tasks:
- Keyboard input
- Window creation
- Event translation
Mono’s Windows.Forms implementation translates the native system events such as X11 into Win32 WM_ messages and dispatches them using the WndProc mechanism. This allows applications that depend on overriding WndProc to get some features not exposed by the APIs to still run.
Accessibility
Currently Windows.Forms does not support accessibility, this is being worked on in a separate project that started in January of 2008. See the Accessibility page for details about how this will integrate into the system.
Sample Code
Sample applications for Mono’s Windows.Forms are available in winforms on github
Theming
This section discusses the theming issues in Mono’s implementation of Windows Forms as these questions are asked frequently:
Why Not Use Native Widgets?
It is not feasible to use native widgets on all operating systems. Each OS/Windowing system has different behavior/properties/features for what on the surface looks like the same widget. A RadioButton in Gnome is different from a RadioButton in Win32, which is different from a RadioButton in macOS. To use the native widgets means to reduce the functionality of MWF to the least common denominator of all supported operating systems. If we were designing our own GUI toolkit, this might even be acceptable, however we are implementing an already defined API with defined behavior (and even with application relied-upon side-effects). A RadioButton has to behave exactly like it behaves on Win32 with MS.Net, or else applications written for it may not work properly anymore. And that’s the whole point of Winforms: to allow existing .Net SWF apps to run on Mono. For other uses, there are other choices that may be more appropriate, such as Gtk#.
Fitting In
Although we cannot use native widgets, we still would like to make a reasonable effort to achieve a more native “look and feel” on different platforms. To accomplish this, we have created a theming interface with the hope that in the future it will facilitate our controls looking more native. Having a separate theme class from the get-go forces those who are writing the controls to design code where the drawing code is not integrated and intermingled with the control code.
Maturity of the Theming Interface
Currently, we have the default managed Win32 Classic theme and a native Windows VisualStyles theme. We would like to have native theming on Linux and macOS.
The best option we currently have for theming is the VisualStyles theme. On Windows, this calls the native Windows API and allows it to draw natively for us. Unfortunately, the System.Windows.Forms.VisualStyles namespace is not currently implemented on Linux or macOS. The best way forward would be to implement this namespace on those platforms. (Alternatively, one could port Wine’s uxtheme.dll and we could ship Clearlooks and Aqua themes that people have done.)
The VisualStyles namespace allows you to focus simply on drawing one element, like a “selected button” instead of having to try to sift through the theming code to find out how it draws a button.
History
There were two previous attempts to implement System.Windows.Forms in Mono. These initial attempts were done on top of other toolkits: first Gtk, then Wine. Each one had its share of problems, causing it to be abandoned.
The Gtk effort had the following problems:
- Mapping one toolkit’s semantics to another was very hard.
- It would be very cumbersome to map all events into WndProc events, the messaging system at the core of Windows.Forms.
- The development effort happened early in the Mono life, with this very sporadic development effort, the code quickly became obsolete or bit-rotted.
- Using this on other systems required a Gtk+ installation on the target system (macOS and embedded devices would have suffered the most).
The Wine effort could have been a successful approach, however there were several technical obstacles:
- Multithreading support. Wine has a specific thread setup that is not compatible with Mono. While this was solvable, it would have required larger patches to Wine.
- Dependencies. To have Mono and Wine interoperate, a glue library was required. This library required to know where Wine was installed to (we had a Wine patch that eased this requirement, but it was not accepted into Wine).
- Moving target. Key functions in Wine changed often enough that with every Wine release we had to start over making our glue code work again, sometimes becoming incompatible with previous Wine versions.
- Wine/Gdi+ interactions: The way System.Drawing had to be made compatible with Wine was extremely inefficient. Wine uses the GDI model (raster painting), while our System.Drawing implementation, built on top of Cairo, uses the GDI+ model (compositing model). Making them both talk was extremely inefficient as every transition from one model to the other required the bitmaps to be copied.
The current approach is to implement all controls fully in managed code, and uses an abstract theme interface to paint the widgets. The default theme interfaces renders the widgets using System.Drawing.
Webbrowser Control
Information about the WebBrowser control can be found on the WebBrowser page.
Источник
System windows forms linux
System.Windows.Forms for .Net Core running on Windows and Linux
This is a early stage and experimental port of Mono’s System.Windows.Forms and a few additional libraries to .Net Core 3.1. This allows Winforms code to run on both Windows and Linux (and possibly MacOS in the future).
Rather then porting Mono’s implementation of System.Drawing to .Net Core 3.1 I ported Mono’s System.Windows.Forms onto System.Drawing.Common. Note that on Linux you will need some native libraries.
Even though this is highly experimental and unsupported it does work (at least for me).
A Demo on my Raspberry Pi 4, built and running on the Pi using .Net Core 3.1.
Windows — None known
Linux — Several native libraries, at least libx11 (libx11-dev on Ubuntu) and libgdiplus (libgdiplus on Ubuntu)
Mac — Not currently working. The Mono code that this is based on was 32 bit only. .Net Core requires 64 bit so until that is done this will not work.
There are three samples: SimpleTest (which uses my .Net Core Winforms), SimpleTest.NetCore (which uses the official .Net Core Winforms) and SimpleTest.NetFX which uses Net Framework. This also shows how to use the same source code with all three different platforms as the code is added as a link to the NetCore and NetFX projects. This way you can use the officially supported and nice looking Winforms on Windows.
To test this on Linux simply dotnet run in the SimpleTest folder.
More samples are coming soon TM. This will include more complex forms and more demonstrations of using the different libraries for different platforms.
I tested the SimpleTest example on Windows, Raspbian Buster and Ubuntu 20.04 using .Net Core 3.1 Release.
Unknown right now. I will likely add more samples and fix issues as I go. If Mono adds support for Mac 64 bit I will port that over.
I will add better Linux documentation as I have time.
Источник
Sound Code
a place for me to talk about programming and .NET . because my friends and family aren’t interested
Friday, 6 June 2014
Running Windows Forms on Linux with Mono
Although WinForms may be “dead”, it does have one trick up its sleeve that WPF doesn’t, and that is you can run WinForms apps on mono. Here’s a simple guide to running a Windows Forms application on Ubuntu
Step 1 — Install Mono
Open a terminal window, and make sure everything is up to date with the following commands:
Now you can install mono with the following command:
Step 2 — Create an Application
Now we need to create our C# source file. You can use any text editor you like, but if like me you aren’t familiar with Linux text editors like vi or emacs, gedit is a simple notepad-like application which is easy to use. Launch it with the following command: (the ampersand at the end tells the terminal not to wait for gedit to close before letting us continue)
Now let’s create a very simple application:
Step 3 — Compile and Run
Now we’re ready to compile. The C# compiler in mono is gmcs. We’ll need to tell it we’re referencing the Windows Forms DLL:
gmcs wf.cs –r:System.Windows.Forms.dll
To run the application, simply call mono, passing in the executable:
And that’s all there is to it! We have a WinForms app running on Linux.
Although mono doesn’t support everything in WinForms, you can use most standard controls, so you can easily add further UI elements:
Taking it Further
Obviously writing applications by hand like this is a bit cumbersome, but there is an IDE you can use for Linux called monodevelop. You install it like this:
Источник