mirror of
https://github.com/Ryujinx/GtkSharp.git
synced 2025-01-11 02:15:37 +00:00
Add samples that use System.Drawing
svn path=/trunk/gtk-sharp/; revision=28384
This commit is contained in:
parent
bc3ad7a1bd
commit
977c36bd98
81
sample/DrawingSample.cs
Normal file
81
sample/DrawingSample.cs
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
//
|
||||||
|
// Sample program demostrating using Cairo with Gtk#
|
||||||
|
//
|
||||||
|
using Gtk;
|
||||||
|
using System;
|
||||||
|
using System.Drawing;
|
||||||
|
|
||||||
|
class X {
|
||||||
|
static DrawingArea a, b;
|
||||||
|
|
||||||
|
static void Main ()
|
||||||
|
{
|
||||||
|
Application.Init ();
|
||||||
|
Gtk.Window w = new Gtk.Window ("Hello");
|
||||||
|
|
||||||
|
// Custom widget sample
|
||||||
|
a = new PrettyGraphic ();
|
||||||
|
|
||||||
|
// Event-based drawing
|
||||||
|
b = new DrawingArea ();
|
||||||
|
b.ExposeEvent += ExposeHandler;
|
||||||
|
b.SizeAllocated += SizeAllocatedHandler;
|
||||||
|
|
||||||
|
Box box = new HBox (true, 0);
|
||||||
|
box.Add (a);
|
||||||
|
//box.Add (b);
|
||||||
|
w.Add (box);
|
||||||
|
|
||||||
|
w.ShowAll ();
|
||||||
|
Application.Run ();
|
||||||
|
}
|
||||||
|
|
||||||
|
static Gdk.Rectangle rect;
|
||||||
|
|
||||||
|
static void SizeAllocatedHandler (object obj, SizeAllocatedArgs args)
|
||||||
|
{
|
||||||
|
rect = args.Allocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ExposeHandler (object obj, ExposeEventArgs args)
|
||||||
|
{
|
||||||
|
Gdk.EventExpose ev = args.Event;
|
||||||
|
Gdk.Window window = ev.Window;
|
||||||
|
|
||||||
|
using (Graphics g = Gdk.Graphics.FromDrawable (window)){
|
||||||
|
Console.WriteLine ("{0} and {1}", -ev.Area.X, -ev.Area.Y);
|
||||||
|
g.TranslateTransform (ev.Area.X, ev.Area.Y);
|
||||||
|
using (Pen p = new Pen (Color.Red)){
|
||||||
|
g.DrawPie (p, 0, 0, rect.Width, rect.Height, 50, 90);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// A sample using inheritance to draw
|
||||||
|
//
|
||||||
|
class PrettyGraphic : DrawingArea {
|
||||||
|
|
||||||
|
protected override bool OnExposeEvent (Gdk.EventExpose args)
|
||||||
|
{
|
||||||
|
Gdk.Window win = args.Window;
|
||||||
|
Gdk.Rectangle area = args.Area;
|
||||||
|
|
||||||
|
using (Graphics g = Gdk.Graphics.FromDrawable (args.Window)){
|
||||||
|
//Console.WriteLine ("{0} and {1}", -args.Area.X, -args.Area.Y);
|
||||||
|
//g.TranslateTransform (-args.Area.X, -args.Area.Y);
|
||||||
|
Pen p = new Pen (Color.Blue, 1.0f);
|
||||||
|
Pen q = new Pen (Color.Red, 1.0f);
|
||||||
|
|
||||||
|
g.DrawLine (p, 0, 0, 100, 100);
|
||||||
|
g.DrawLine (q, 0, 0, 100, 100);
|
||||||
|
return true;
|
||||||
|
|
||||||
|
for (int i = 0; i < 600; i += 60)
|
||||||
|
for (int j = 0; j < 600; j += 60)
|
||||||
|
g.DrawLine (p, i, 0, 0, j);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
6
sample/DrawingSample.exe.config
Normal file
6
sample/DrawingSample.exe.config
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<configuration>
|
||||||
|
<dllmap dll="libglib-2.0-0.dll" target="libglib-2.0.so.0"/>
|
||||||
|
<dllmap dll="libgobject-2.0-0.dll" target="libgobject-2.0.so.0"/>
|
||||||
|
<dllmap dll="libgdk-win32-2.0-0.dll" target="libgdk-x11-2.0.so.0"/>
|
||||||
|
<dllmap dll="libgdk_pixbuf-2.0-0.dll" target="libgdk_pixbuf-2.0.so.0"/>
|
||||||
|
</configuration>
|
52
sample/sysdraw.cs
Normal file
52
sample/sysdraw.cs
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
//
|
||||||
|
// System.Drawing integration with Gtk#
|
||||||
|
//
|
||||||
|
// Miguel de Icaza
|
||||||
|
//
|
||||||
|
// API issues:
|
||||||
|
// Maybe make the translation `out' parameters so they are explicit and the user knows about it?
|
||||||
|
// Add a way to copy a Graphics into a drawable?
|
||||||
|
//
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace Gdk {
|
||||||
|
public class Graphics {
|
||||||
|
|
||||||
|
[DllImport("libgdk-win32-2.0-0.dll")]
|
||||||
|
internal static extern IntPtr gdk_x11_drawable_get_xdisplay (IntPtr raw);
|
||||||
|
|
||||||
|
[DllImport("libgdk-win32-2.0-0.dll")]
|
||||||
|
internal static extern IntPtr gdk_x11_drawable_get_xid (IntPtr raw);
|
||||||
|
|
||||||
|
public static System.Drawing.Graphics FromDrawable (Gdk.Drawable drawable)
|
||||||
|
{
|
||||||
|
IntPtr x_drawable;
|
||||||
|
int x_off = 0, y_off = 0;
|
||||||
|
|
||||||
|
|
||||||
|
if (drawable is Gdk.Window){
|
||||||
|
((Gdk.Window) drawable).GetInternalPaintInfo(out drawable, out x_off, out y_off);
|
||||||
|
}
|
||||||
|
x_drawable = drawable.Handle;
|
||||||
|
|
||||||
|
IntPtr display = gdk_x11_drawable_get_xdisplay (x_drawable);
|
||||||
|
|
||||||
|
Type graphics = typeof (System.Drawing.Graphics);
|
||||||
|
MethodInfo mi = graphics.GetMethod ("FromXDrawable", BindingFlags.Static | BindingFlags.NonPublic);
|
||||||
|
if (mi == null)
|
||||||
|
throw new NotImplementedException ("In this implementation I can not get a graphics from a drawable");
|
||||||
|
object [] args = new object [2] { (IntPtr) gdk_x11_drawable_get_xid (drawable.Handle), (IntPtr) display };
|
||||||
|
object r = mi.Invoke (null, args);
|
||||||
|
System.Drawing.Graphics g = (System.Drawing.Graphics) r;
|
||||||
|
|
||||||
|
Console.WriteLine ("-> {0} / {1}", x_off, y_off);
|
||||||
|
g.TranslateTransform (-x_off, -y_off);
|
||||||
|
|
||||||
|
return g;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue