CSharpMath icon indicating copy to clipboard operation
CSharpMath copied to clipboard

Support for Windows Forms apps

Open pavledev opened this issue 5 years ago • 21 comments

Is your feature request related to a problem? Please describe. Can you please add support for Windows Forms Apps?

pavledev avatar Oct 07 '20 12:10 pavledev

@FoggyFinder Probably using WpfMath would be more suitable for WinForms/WPF?

Happypig375 avatar Oct 07 '20 14:10 Happypig375

Yep

@pavledev Btw, why? I believe WinForms is outdated.

FoggyFinder avatar Oct 07 '20 14:10 FoggyFinder

Merge into https://github.com/ForNeVeR/wpf-math/issues/281?

Happypig375 avatar Oct 07 '20 14:10 Happypig375

Windows forms apps are already supported. CSharpMath supports everything that SkiaSharp supports: https://github.com/mono/SkiaSharp

charlesroddie avatar Oct 07 '20 14:10 charlesroddie

The control isn't there though. Additional code to wire up CSharpMath.SkiaSharp and Windows Forms is needed for even the most basic of usages.

Happypig375 avatar Oct 07 '20 14:10 Happypig375

@Happypig375 WpfMath only supports WPF. @FoggyFinder Because WinForms are easier for designing

pavledev avatar Oct 07 '20 14:10 pavledev

@pavledev As written in https://github.com/ForNeVeR/wpf-math/issues/281#issuecomment-704955818, there is a Windows Forms control that can wrap WPF controls.

Happypig375 avatar Oct 07 '20 14:10 Happypig375

@Happypig375 Can you please help me to wrap it?

pavledev avatar Oct 07 '20 15:10 pavledev

Additional code to wire up

The three lines of code in https://github.com/verybadcat/CSharpMath#2-csharpmathskiasharp?

charlesroddie avatar Oct 07 '20 15:10 charlesroddie

@pavledev https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.integration.elementhost?view=netcore-3.1#examples @charlesroddie Compared to a full view, that is not enough.

Happypig375 avatar Oct 07 '20 15:10 Happypig375

@Happypig375 Which additional code is required to use CSharpMath.SkiaSharp in WinForm?

pavledev avatar Oct 07 '20 16:10 pavledev

@pavledev To use SkiaSharp in WinForms: Get https://www.nuget.org/packages/SkiaSharp.Views/ , create an SKControl and add to its paint event, or inherit from it and override its paint event. To draw CSharpMath things on it: get a MathPainter or TextPainter and Draw with it on the SKCanvas.

charlesroddie avatar Oct 07 '20 19:10 charlesroddie

var painter = new CSharpMath.SkiaSharp.MathPainter(); painter.LaTeX = @"\frac\sqrt23"; painter.Draw(skControl1);

@charlesroddie I get error: argument 1: cannot convert from 'SkiaSharp.Views.Desktop.SKControl' to 'SkiaSharp.SKCanvas'

pavledev avatar Oct 07 '20 21:10 pavledev

I'm not at my computer but in the paint event eventArgs you should get access to the canvas. Maybe e.Canvas or e.Surface.Canvas or something similar.

charlesroddie avatar Oct 07 '20 22:10 charlesroddie

private void skControl1_PaintSurface(object sender, SkiaSharp.Views.Desktop.SKPaintSurfaceEventArgs e) { var painter = new CSharpMath.SkiaSharp.MathPainter(); painter.LaTeX = @"\frac\sqrt23"; painter.Draw(e.Surface.Canvas); }

@charlesroddie It's working now. Thank you.

pavledev avatar Oct 08 '20 05:10 pavledev

@charlesroddie I can also add formula setting LaTeX property to MathPainter object and by calling skControl1.Invalidate(); from button click event right?

pavledev avatar Oct 09 '20 08:10 pavledev

Yes an extremely simple control would be

type MathView() =
    inherit SKControl()
    let mathPainter = MathPainter()
    member this.LaTeX
        with set tex =
            mathPainter.LaTeX <- tex
            this.InvalidateSurface()
    override _.OnPaintSurface(e) =
        e.Surface.Canvas.Clear()
        mathPainter.Draw(e.Surface.Canvas)

charlesroddie avatar Oct 09 '20 09:10 charlesroddie

@charlesroddie How I can add scrollbar to SKControl?

pavledev avatar Oct 14 '20 16:10 pavledev

Use scroll bars from Windows Forms: https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/hscrollbar-and-vscrollbar-controls-overview-windows-forms?view=netframeworkdesktop-4.8#:~:text=Windows%20Forms%20ScrollBar%20controls%20are%20used%20to%20provide,horizontally%20or%20vertically%20within%20an%20application%20or%20control.

Happypig375 avatar Oct 14 '20 16:10 Happypig375

@Happypig375 Can you please help me to add them to SKControl?

pavledev avatar Oct 14 '20 16:10 pavledev

@pavledev Consider asking WinForms - related Qs in a some kind of Q&A community. Your Q isn't related to this issue. Moreover it's not even related to CSharpMath itself.

Here is a quick and dirty sample:

using SkiaSharp.Views.Desktop;
using System.Windows.Forms;

namespace CSharpMathSample
{
    public partial class Form1 : Form
    {
        private readonly Panel panel;
        private readonly SKControl skControl;
        private readonly string sample = @"Here are some text.
This text is made to be long enough to have the TextPainter of CSharpMath add a line break to this text automatically.
To demonstrate the capabilities of the TextPainter,
here are some math content:
First, a fraction in inline mode: $\frac34$
Next, a summation in inline mode: $\sum_{i=0}^3i^i$
Then, a summation in display mode: $$\sum_{i=0}^3i^i$$
After that, an integral in display mode: $$\int^6_{-56}x\ dx$$
Finally, an escaped dollar sign \$ that represents the start/end of math mode when it is unescaped.
Colors can be achieved via \backslash color{color}{content}, or \backslash \textit{color}{content},
where \textit{color} stands for one of the LaTeX standard colors.
\red{Colored text in text mode are able to automatically break up when spaces are inside the colored text, which the equivalent in math mode cannot do.}
\textbf{Styled} \texttt{text} can be achieved via the LaTeX styling commands.
The SkiaSharp version of this is located at CSharpMath.SkiaSharp.TextPainter;
and the Xamarin.Forms version of this is located at CSharpMath.Forms.TextView.
Was added in 0.1.0-pre4; working in 0.1.0-pre5; fully tested in 0.1.0-pre6. \[\frac{Display}{maths} \sqrt\text\mathtt{\ at\ the\ end}^\mathbf{are\ now\ incuded\ in\ Measure!} \]";
        public Form1()
        {
            InitializeComponent();
            panel = new Panel()
            {
                AutoScroll = true,
                Dock = DockStyle.Fill
            };
            skControl = new SKControl()
            {
                Width = Width,
                Height = Height
            };
            
            panel.Controls.Add(skControl);
            this.Controls.Add(panel);
            skControl.PaintSurface += SkControl_PaintSurface;
        }

        private void SkControl_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            e.Surface.Canvas.Clear();
            var painter = new CSharpMath.SkiaSharp.TextPainter
            {
                LaTeX = sample
            };
            painter.Draw(e.Surface.Canvas);
            var r = painter.Measure(Width);
            skControl.Width = (int)r.Width;
            skControl.Height = (int)r.Height;
        }
    }
}

FoggyFinder avatar Oct 16 '20 19:10 FoggyFinder