1

我在窗口上有 cairo 的问题,我不知道为什么它无法在我的面板上绘制当我使用 GDI 时一切正常,但是当我使用 cairo 时我无法绘制,请帮助。谢谢。


包裹开罗来画画

using System;
using Cairo;
using Gtk;  
namespace BT.LibExtend
{
    public class CairoExt : BTGraphicLibExt 
    {
        Surface s;
        Context c;
        public CairoExt(IntPtr hdc)
        {
            s = new Win32Surface(hdc);
            c = new Context(s);
        }
        public override void DrawLine(double x1, double y1, double x2, double y2)
        {
            c.MoveTo(x1, y1);
            c.LineTo(x2, y2);
            c.Stroke();
        }
    }
}

这是我的表格

   public partial class FigureDraw : Form
{
    GraphicLibExt glip;

    public FigureDraw()
    {
        InitializeComponent();
        glip = new CairoExt(pnMainDraw.CreateGraphics().GetHdc());

    }

    private void btnLine_Click(object sender, EventArgs e)
    {
        glip.DrawLine(20, 20, 100, 100);
    }

}
4

1 回答 1

0

也许在显示窗口之前无法获取 deive 上下文,但我不确定。

在重写的 OnPaint 方法中使用 PaintEventArgs 的图形时,它可以工作。

using System.Windows.Forms;
using Cairo;
using Color = Cairo.Color;
using Graphics = System.Drawing.Graphics;

public partial class Form1 : Form
{
    public Graphics Graphics1 { get; private set; }
    public Context Context1 { get; set; }
    public Win32Surface Surface1 { get; private set; }

    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        Graphics1 = e.Graphics;
        Surface1 = new Win32Surface(Graphics1.GetHdc());
        Context1 = new Context(Surface1);

        var p1 = new PointD(10, 10);
        var p2 = new PointD(100, 10);
        var p3 = new PointD(100, 100);
        var p4 = new PointD(10, 100);

        Context1.MoveTo(p1);
        Context1.LineTo(p2);
        Context1.LineTo(p3);
        Context1.LineTo(p4);
        Context1.LineTo(p1);
        Context1.ClosePath();
        Context1.Fill();

        Context1.MoveTo(140, 110);
        Context1.SetFontSize(32);
        Context1.SetSourceColor(new Color(0,0,0.8,1));
        Context1.ShowText("Hello Cairo!");

        Graphics1.Dispose();
        Context1.Dispose();
        Surface1.Dispose();
    }
}

或者在显示表单时使用本机方法 GetDc 来获取 hdc 。

于 2015-09-17T17:48:35.300 回答