2013-05-02 62 views
1

我proplem與窗口開羅,我不知道爲什麼它不能我的面板 上繪製,當我使用GDI一切都奧凱,但我可以」當我使用開羅繪製時,請幫助。 謝謝。爲什麼不能不能在winform借鑑面板開羅


總結開羅繪製

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(); 
     } 
    } 
} 

這是myForm的

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); 
    } 

} 

回答

0

也許deive背景不能獲得,直到所示的窗口,但我不知道。

當使用PaintEventArgs的在overrided OnPaint方法圖形,它的工作原理。

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 when the form is shown