зеркало из https://github.com/mono/mail-archives.git
201 строка
7.0 KiB
HTML
201 строка
7.0 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE> [Mono-osx] System.Drawing into Cocoa views?
|
|
</TITLE>
|
|
<LINK REL="Index" HREF="index.html" >
|
|
<LINK REL="made" HREF="mailto:mono-osx%40lists.ximian.com?Subject=%5BMono-osx%5D%20System.Drawing%20into%20Cocoa%20views%3F&In-Reply-To=32E65F0B-8C3B-4956-B327-DF0BB7585809%40tarkvara.org">
|
|
<META NAME="robots" CONTENT="index,nofollow">
|
|
<META http-equiv="Content-Type" content="text/html; charset=us-ascii">
|
|
<LINK REL="Previous" HREF="004435.html">
|
|
<LINK REL="Next" HREF="004438.html">
|
|
</HEAD>
|
|
<BODY BGCOLOR="#ffffff">
|
|
<H1>[Mono-osx] System.Drawing into Cocoa views?</H1>
|
|
<B>Alain Bocherens</B>
|
|
<A HREF="mailto:mono-osx%40lists.ximian.com?Subject=%5BMono-osx%5D%20System.Drawing%20into%20Cocoa%20views%3F&In-Reply-To=32E65F0B-8C3B-4956-B327-DF0BB7585809%40tarkvara.org"
|
|
TITLE="[Mono-osx] System.Drawing into Cocoa views?">alain at slide-effect.com
|
|
</A><BR>
|
|
<I>Mon Jul 18 04:02:28 EDT 2011</I>
|
|
<P><UL>
|
|
<LI>Previous message: <A HREF="004435.html">[Mono-osx] System.Drawing into Cocoa views?
|
|
</A></li>
|
|
<LI>Next message: <A HREF="004438.html">[Mono-osx] System.Drawing into Cocoa views?
|
|
</A></li>
|
|
<LI> <B>Messages sorted by:</B>
|
|
<a href="date.html#4436">[ date ]</a>
|
|
<a href="thread.html#4436">[ thread ]</a>
|
|
<a href="subject.html#4436">[ subject ]</a>
|
|
<a href="author.html#4436">[ author ]</a>
|
|
</LI>
|
|
</UL>
|
|
<HR>
|
|
<!--beginarticle-->
|
|
<PRE>Hi Eric,
|
|
|
|
I faced the exact same problem. I wrote a small class to handle this
|
|
case. It is derived from NSView and you only have to inherit from it and
|
|
wrote your own OnPaint method.
|
|
|
|
Here it is:
|
|
------------------------------------------------------------------------
|
|
using System;
|
|
using System.Drawing;
|
|
using MonoMac.AppKit;
|
|
using MonoMac.Foundation;
|
|
|
|
namespace SEForms.Forms
|
|
{
|
|
public class SECustomView
|
|
{
|
|
public class CustomView : NSView
|
|
{
|
|
private NSImage _buffer=null;
|
|
public override bool IsFlipped
|
|
{
|
|
get { return true; }
|
|
}
|
|
|
|
public NSImage Buffer
|
|
{
|
|
set
|
|
{
|
|
_buffer = value;
|
|
}
|
|
}
|
|
|
|
public override void DrawRect (RectangleF dirtyRect)
|
|
{
|
|
if(_buffer!=null)
|
|
{
|
|
_buffer.Draw(this.Bounds,new
|
|
RectangleF(0,0,_buffer.Size.Width,_buffer.Size.Height),NSCompositingOperation.Copy,1.0f);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private Bitmap _buffer = null;
|
|
private CustomView _innerControl = null;
|
|
|
|
public SECustomView ()
|
|
{
|
|
_innerControl = new CustomView();
|
|
|
|
}
|
|
|
|
public void Invalidate()
|
|
{
|
|
Graphics g = Graphics.FromImage(_buffer);
|
|
OnPaint(g);
|
|
g.Dispose();
|
|
_innerControl.Buffer = MacTools.ConvertToNSImage(_buffer);
|
|
_innerControl.NeedsDisplay = true;
|
|
}
|
|
|
|
public Bitmap InternalBuffer
|
|
{
|
|
get
|
|
{
|
|
return _buffer;
|
|
}
|
|
}
|
|
|
|
virtual public void OnPaint(Graphics g)
|
|
{
|
|
// just a test, but otherwrite this method to specialise
|
|
the drawing
|
|
g.DrawRectangle(Pens.HotPink,10,10,100,100);
|
|
}
|
|
|
|
|
|
|
|
public Size Size
|
|
{
|
|
get { return _innerControl.Frame.Size.ToSize(); }
|
|
set
|
|
{
|
|
_innerControl.Frame = new RectangleF
|
|
(_innerControl.Frame.Location, value);
|
|
if((_buffer==null) || (_buffer.Size != value))
|
|
{
|
|
if(_buffer!=null)
|
|
{
|
|
_buffer.Dispose();
|
|
}
|
|
//resize buffer
|
|
_buffer = new
|
|
Bitmap((int)value.Width,(int)value.Height);
|
|
}
|
|
Invalidate();
|
|
}
|
|
}
|
|
|
|
public static NSImage ConvertToNSImage(Image img)
|
|
{
|
|
System.IO.MemoryStream s = new System.IO.MemoryStream();
|
|
img.Save(s, System.Drawing.Imaging.ImageFormat.Png);
|
|
byte[] b = s.ToArray();
|
|
CGDataProvider dp = new CGDataProvider(b,0,(int)s.Length);
|
|
s.Flush();
|
|
s.Close();
|
|
CGImage img2 =
|
|
CGImage.FromPNG(dp,null,false,CGColorRenderingIntent.Default);
|
|
return new NSImage(img2, new SizeF(img2.Width,img2.Height));
|
|
}
|
|
}
|
|
}
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
--
|
|
Alain Bocherens
|
|
|
|
Slide Effect :: Create Amazing Presentations
|
|
<A HREF="http://www.slide-effect.com">http://www.slide-effect.com</A>
|
|
Banner Effect :: Flash Banners Made Simple.
|
|
<A HREF="http://www.banner-effect.com">http://www.banner-effect.com</A>
|
|
|
|
|
|
Le 17.07.2011 23:59, Eric J. M. Smith a écrit :
|
|
><i> Greetings,
|
|
</I>><i>
|
|
</I>><i> I'm new to MonoMac (and to Mono in general). So I apologise in advance for my ignorance.
|
|
</I>><i>
|
|
</I>><i> I'm exploring the feasibility of porting a .NET/Windows app to run on the Mac. I've resigned myself to the fact that we'll be rewriting any code which relies on Windows.Forms widgets. Our existing rendering code draws into a System.Drawing.Image, and it would be nice to not have to rewrite everything. Is there any way of drawing into a System.Drawing.Graphics and making the results available to Cocoa? Either by drawing directly into an NSView or by somehow getting the contents of a System.Drawing.Image into an NSImage.
|
|
</I>><i>
|
|
</I>><i> I did find some mention on this mailing-list of a "drawing bridge" to do libgdiplus drawing in a Cocoa custom view, but I can't find any documentation or samples for this (plus, it sounds like this approach might require X11).
|
|
</I>><i>
|
|
</I>><i> Thanks for your help,
|
|
</I>><i>
|
|
</I>><i> Eric Smith
|
|
</I>><i> Tarkvara Design Inc.
|
|
</I>><i> _______________________________________________
|
|
</I>><i> Mono-osx mailing list
|
|
</I>><i> <A HREF="http://lists.ximian.com/mailman/listinfo/mono-osx">Mono-osx at lists.ximian.com</A>
|
|
</I>><i> <A HREF="http://lists.ximian.com/mailman/listinfo/mono-osx">http://lists.ximian.com/mailman/listinfo/mono-osx</A>
|
|
</I></PRE>
|
|
|
|
|
|
|
|
<!--endarticle-->
|
|
<HR>
|
|
<P><UL>
|
|
<!--threads-->
|
|
<LI>Previous message: <A HREF="004435.html">[Mono-osx] System.Drawing into Cocoa views?
|
|
</A></li>
|
|
<LI>Next message: <A HREF="004438.html">[Mono-osx] System.Drawing into Cocoa views?
|
|
</A></li>
|
|
<LI> <B>Messages sorted by:</B>
|
|
<a href="date.html#4436">[ date ]</a>
|
|
<a href="thread.html#4436">[ thread ]</a>
|
|
<a href="subject.html#4436">[ subject ]</a>
|
|
<a href="author.html#4436">[ author ]</a>
|
|
</LI>
|
|
</UL>
|
|
|
|
<hr>
|
|
<a href="http://lists.ximian.com/mailman/listinfo/mono-osx">More information about the Mono-osx
|
|
mailing list</a><br>
|
|
</body></html>
|