using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Cards
{
/// <summary>
/// A class wrapper to the cards.dll. This misses only the crlAnimate functionaliy.
/// </summary>
public class Card32
{
#region API Declares
[DllImport("cards.dll")]
private static extern bool cdtInit([In] ref int width, [In] ref int height);
[DllImport("cards.dll")]
private static extern void cdtTerm();
[DllImport("cards.dll")]
private static extern int cdtDrawExt(IntPtr hDC, int x, int y, int dx, int dy,
int ecsCard, int ectDraw, int clr);
[DllImport("cards.dll")]
private static extern int cdtDraw(IntPtr hDC, int x, int y, int ecsCard,
int ectDraw, int clr);
[DllImport("cards.dll")]
private static extern int cdtAnimate(IntPtr hDC, int ecbCardBack, int x, int y, int iState);
#endregion
#region fields
private static int _standardWidth;
private static int _standardHeight;
#endregion
#region Constructor
static Card32()
{
bool ret = Card32.cdtInit(ref _standardWidth, ref _standardHeight);
if (!ret)
throw new ApplicationException("Can't load cards.dll !");
// free up library when process exits
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
}
#endregion
#region Public Static Members
public static void DrawFace(Graphics g, int x, int y, int width, int height, int faceValue, CardSuit suit, bool inverted)
{
int cardValue = (int)suit + faceValue * 4;
IntPtr hDc = g.GetHdc();
try
{
cdtDrawExt(hDc, x, y, width, height, cardValue, inverted ? 2 : 0, 0);
}
finally
{
g.ReleaseHdc(hDc);
}
}
public static void DrawFace(Graphics g, int x, int y, int faceValue, CardSuit suit, bool inverted)
{
int cardValue = (int)suit + faceValue * 4;
IntPtr hDc = g.GetHdc();
try
{
cdtDraw(hDc, x, y, cardValue, inverted ? 2 : 0, 0);
}
finally
{
g.ReleaseHdc(hDc);
}
}
public static void DrawFace(Graphics g, int x, int y, int faceValue, CardSuit suit)
{
Card32.DrawFace(g, x, y, faceValue, suit, false);
}
public static void DrawDeck(Graphics g, int x, int y, int width, int height, CardDeck deck)
{
IntPtr hDc = g.GetHdc();
try
{
cdtDrawExt(hDc, x, y, width, height, (int)deck, 1, 0);
}
finally
{
g.ReleaseHdc(hDc);
}
}
public static void DrawDeck(Graphics g, int x, int y, CardDeck deck)
{
IntPtr hDc = g.GetHdc();
try
{
cdtDraw(hDc, x, y, (int)deck, 1, 0);
}
finally
{
g.ReleaseHdc(hDc);
}
}
public static void DrawDeck(Graphics g, int x, int y, int width, int height, Color backgroundColor)
{
IntPtr hDc = g.GetHdc();
try
{
Color color = Color.FromArgb(0, backgroundColor);
cdtDrawExt(hDc, x, y, width, height, (int)CardDeck.CrossHatch, 1, color.ToArgb());
}
finally
{
g.ReleaseHdc(hDc);
}
}
public static void DrawDeck(Graphics g, int x, int y, Color backgroundColor)
{
IntPtr hDc = g.GetHdc();
try
{
Color color = Color.FromArgb(0, backgroundColor);
cdtDraw(hDc, x, y, (int)CardDeck.CrossHatch, 1, color.ToArgb());
}
finally
{
g.ReleaseHdc(hDc);
}
}
#endregion
#region private members
private static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
Card32.cdtTerm();
}
#endregion
}
#region enums
public enum CardSuit : int
{
Clubs = 0,
Diamonds = 1,
Hearts = 2,
Spades = 3
}
public enum CardDeck : int
{
CrossHatch = 53,
Weave1 = 54,
Weave2 = 55,
Robot = 56,
Flowers = 57,
Vine1 = 58,
Vine2 = 59,
Fish1 = 60,
Fish2 = 61,
Shells = 62,
Castle = 63,
Island = 64,
CardHand = 65,
The_X = 67,
The_O = 68
}
#endregion
}
