Home  |  FAQ  |  About  |  Contact  |  View Source   
 
SEARCH:
 
BROWSE:
    My Hood
Edit My Info
View Events
Read Tutorials
Training Modules
View Presentations
Download Tools
Scan News
Get Jobs
Message Forums
School Forums
Member Directory
   
CONTRIBUTE:
    Sign me up!
Post an Event
Submit Tutorials
Upload Tools
Link News
Post Jobs
   
   
Home >  Tutorials >  C# >  Drawing Cards with C#
Add to MyHood
Drawing Cards with C#   [ printer friendly ]
Stats
  Rating: 2.89 out of 5 by 9 users
  Submitted: 04/29/02
Dan Goldstein ()

 
Card games are fun. Not all of them, but a good card game is classic. Drawing cards could be tricky if you're not an artist. Fortunately, Microsoft includes a whole bunch of cards with Windows. Cards, card backs, inverted cards, they're all there. Here's how to use them.

First we need to access the DLL. A wrapper isn't very hard. Personally, I prefer to use a lot of functions that all have specific purposes instead of passing obscure parameters.
The DLL specification is available on an old MSDN article.
using System;
using System.Runtime.InteropServices;
using System.Drawing;

namespace cards
{
    public class cardsdll
    {
        /// <summary>
        /// Initiates cards.
        /// </summary>
        /// <param name="width">no idea</param>
        /// <param name="height">no idea</param>
        /// <returns></returns>
        [DllImport("cards.dll")]
        private static extern bool cdtInit(ref int width, ref int height);

        [DllImport("cards.dll")]
        private static extern void cdtTerm ();

        /// <summary>
        /// Draws a card
        /// </summary>
        /// <param name="hdc">use Graphics.getHdc()</param>
        /// <param name="x">x location to draw card</param>
        /// <param name="y">y location to draw card</param>
        /// <param name="card">which card or card back to draw</param>
        /// <param name="type">0 draws front, 1 draws back</param>
        /// <param name="color">background for crosshatch back</param>
        /// <returns>dunno</returns>
        [DllImport("cards.dll")]
        private static extern bool cdtDraw (IntPtr hdc, int x, int y, int card, int type, long color);

        /// <summary>
        /// Draws a card
        /// </summary>
        /// <param name="hdc">use Graphics.getHdc()</param>
        /// <param name="x">x location to draw card</param>
        /// <param name="y">y location to draw card</param>
        /// <param name="dx">width of card</param>
        /// <param name="dy">height of card</param>
        /// <param name="card">which card or card back to draw</param>
        /// <param name="type">0: front, 1: back, 2: hilite, 3: ghost, 4: remove, 5: invisible ghost, 6: X, 7: O</param>
        /// <param name="color">background for crosshatch back</param>
        /// <returns></returns>
        [DllImport("cards.dll")]
        private static extern bool cdtDrawExt (IntPtr hdc, int x, int y, int dx, int dy, int card, int suit, long color);

        /// <summary>
        /// Animates card backs. Effects: blinking lights on robot, sun donning sunglasses, bats flying across the castle, and card sliding out of sleeve. Only for cards of normal size drawn with cdtDraw. 
        /// </summary>
        /// <param name="hdc">drawing context</param>
        /// <param name="cardback">which cardback</param>
        /// <param name="x">x location of card</param>
        /// <param name="y">x location of card</param>
        /// <param name="frame">frame of cardback.  Increment until function returns false.</param>
        /// <returns></returns>
        [DllImport("cards.dll")]
        private static extern bool cdtAnimate (IntPtr hdc, int cardback, int x, int y, int frame);
        
        public cardsdll()
        {
            int width = 71, height = 95;
            if (!cdtInit(ref width, ref height))
                throw new Exception("could not load cards");
        }

        public void Dispose()
        {
            cdtTerm();
        }

        public bool drawanimatedcardback(IntPtr canvas, int x, int y, int design, int frame)
        {
            return cdtAnimate(canvas, design, x, y, frame);
        }
        public bool drawanimatedcardback(IntPtr canvas, Point p, int design, int frame)
        {
            return cdtAnimate(canvas, design, p.X, p.Y, frame);
        }
        
        public bool drawcardback(IntPtr canvas, int x, int y, int design)
        {
            return cdtDraw(canvas, x, y, design, 1, 0);
        }
        public bool drawcardback(IntPtr canvas, Point p, int design)
        {
            return cdtDraw(canvas, p.X, p.Y, design, 1, 0);
        }

        public bool drawcard(IntPtr canvas, int x, int y, int card)
        {
            return cdtDraw(canvas, x, y, card, 0, 0);
        }
        public bool drawcard(IntPtr canvas, Point p, int card)
        {
            return cdtDraw(canvas, p.X, p.Y, card, 0, 0);
        }

        public bool drawinvertedcard(IntPtr canvas, int x, int y, int card)
        {
            return cdtDraw(canvas, x, y, card, 2, 0);
        }
        public bool drawinvertedcard(IntPtr canvas, Point p, int card)
        {
            return cdtDraw(canvas, p.X, p.Y, card, 2, 0xffffff);
        }

        public bool drawemptycard(IntPtr canvas, int x, int y, int background)
        {
            return cdtDraw(canvas, x, y, 1, 3, background);
        }
        public bool drawemptycard(IntPtr canvas, Point p, int background)
        {
            return cdtDraw(canvas, p.X, p.Y, 1, 6, background);
        }

        /// <summary>
        /// Draws a card of the specified size
        /// </summary>
        /// <param name="canvas">hdc</param>
        /// <param name="p">origin</param>
        /// <param name="d">size</param>
        /// <param name="card">card</param>
        /// <returns></returns>
        public bool drawcard(IntPtr canvas, Point p, Size s, int card)
        {
            return cdtDrawExt(canvas, p.X, p.Y, s.Width, s.Height, card, 0, 0);
        }

        /// <summary>
        /// Draws a card of a certain size
        /// </summary>
        /// <param name="canvas">hdc</param>
        /// <param name="x">origin x</param>
        /// <param name="y">origin y</param>
        /// <param name="dx">width</param>
        /// <param name="dy">height</param>
        /// <param name="card">card</param>
        /// <returns></returns>
        public bool drawcard(IntPtr canvas, int x, int y, int dx, int dy, int card)
        {
            return cdtDrawExt(canvas, x, y, dx, dy, card, 0, 0);
        }
    }

}


Here's code to use it.
public enum suits : int
{ 
    CLUBS, DIAMONDS, HEARTS, SPADES 
}
        
public enum ranks : int 
{ 
    ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING 
}

private void draw (object sender, PaintEventArgs e)
{
   cardsdll dll;
   IntPtr hdc = e.Graphics.GetHdc();
   dll.drawcard(hdc, x, y, ranks.FIVE*4+suits.CLUBS);
   e.Graphics.ReleaseHdc(hdc);
}


Enjoy.

Return to Browsing Tutorials

Email this Tutorial to a Friend

Rate this Content:  
low quality  1 2 3 4 5  high quality

Reader's Comments Post a Comment
 
A pretty good example of how to use the [DllImport] attribute. Perhaps you should have indicated that in the title of the tutorial somehow.

Overall, I felt like I was looking at too much code, and not enough "tutorial." Perhaps this should have been posted as a code-sample in the tools section?
-- John Gallardo, April 30, 2002
 
Thanks for the tip!
-- Lance Robinson, May 01, 2002
 
this is a good tutorial...but i agree that this would be better in the tools section as a code sample. but it was really well written. i was wanting to write a card program and now i can use this code as an example on how to do a few thing. thanks.
-- J J, May 01, 2002
 
This was more of a tool rather than a tutorial. There is no explanation of any of the included code.
-- Michael S, May 02, 2002
 
Useful stuff, but as the others said, too much time reading code. Just explain more, i know it sometimes seems obvious when you're the author of the code.
-- Lee B, May 03, 2002
 
i really like this. great job
-- Hiranya Maru, May 05, 2002
 
thanks this was just whar i was looking for. I am starting to develop a big 2 game.. yeah i know its been done... but i m just learning.
-- Eric Yip, May 14, 2002
 
Copyright © 2001 DevHood® All Rights Reserved