//-----------------------------------------------------------------------------
// wx.NET - Icon.cs
//
// The wxIcon wrapper class.
//
// Written by Jason Perkins (jason@379.com)
// (C) 2003 by 379, Inc.
// Licensed under the wxWidgets license, see LICENSE.txt for details.
//
// $Id: Icon.cs,v 1.10 2007/11/18 19:33:48 harald_meyer Exp $
//-----------------------------------------------------------------------------
using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace wx{
public class Icon : Bitmap
{
[DllImport("wx-c")] static extern IntPtr wxIcon_ctor();
[DllImport("wx-c")] static extern void wxIcon_CopyFromBitmap(IntPtr self, IntPtr bitmap);
[DllImport("wx-c")] static extern bool wxIcon_LoadFile(IntPtr self, IntPtr name, BitmapType type);
//---------------------------------------------------------------------
/** CTor for ZipResource support.
* This will load an image \c resourceName from ZipResource file or directory \c archiveName.
* \code
wx.ZipResource.AddCatalogLookupPrefix(@"..\Utils\MemLogDisplay");
wx.Icon icon=new wx.Icon("archiveName.zrs", "iconname.png");
\endcode
*/
public Icon(string archiveName, string resourceName)
{
System.IO.Stream s=ZipResource.FindResource(archiveName, resourceName);
if (s==null)
throw new ArgumentException(string.Format("file 'zrs:{0}//{1}' not found", archiveName, resourceName));
int length = (int)s.Length;
byte[] buffer = new byte[length];
s.Read(buffer, 0, length);
Image img = new Image(buffer, BitmapType.wxBITMAP_TYPE_ANY);
Bitmap bmp = new Bitmap(img);
wxIcon_CopyFromBitmap(wxObject, bmp.wxObject);
}
/** This loads the icon from a local file.
* Add a BitmapType to specify alternative ways of loading.
*/
public Icon(string name)
: this()
{
Image img = new Image();
if (!img.LoadFile(name))
throw new ArgumentException("file '" + name + "' not found");
Bitmap bmp = new Bitmap(img);
wxIcon_CopyFromBitmap(wxObject, bmp.wxObject);
}
/** Loads a bitmap of the provided type.
* Use wx.BitmapType.wxBITMAP_TYPE_ANY if you have no information on the type
* of the bitmap. Use wx.BitmapType.wxBITMAP_TYPE_RESOURCE to load a bitmap
* from the .NET resource named \c name.
*/
public Icon(string name, BitmapType type)
: this()
{
if (type == BitmapType.wxBITMAP_TYPE_RESOURCE)
{
Assembly ResourceAssembly = Assembly.GetCallingAssembly();
Image img = new Image(name, ResourceAssembly);
Bitmap bmp = new Bitmap(img);
wxIcon_CopyFromBitmap(wxObject, bmp.wxObject);
}
else
{
wxString wxName = wxString.SafeNew(name);
if (!wxIcon_LoadFile(wxObject, Object.SafePtr(wxName), type))
throw new ArgumentException();
}
}
/** Convenience CTor for reading the resource of the provided name from the provided assembly.
*/
public Icon(string resourceName, Assembly resourceAssembly)
: this()
{
Bitmap bmp=new Bitmap(resourceName, resourceAssembly);
wxIcon_CopyFromBitmap(wxObject, bmp.wxObject);
}
public Icon()
: base(wxIcon_ctor())
{
}
public Icon(IntPtr wxObject)
: base(wxObject) { }
//---------------------------------------------------------------------
public void CopyFromBitmap(Bitmap bitmap)
{
wxIcon_CopyFromBitmap(wxObject, Object.SafePtr(bitmap));
}
//---------------------------------------------------------------------
}
}
|