ATTENTION READERS! Lucky's VB Gaming Site is no longer active. For updated game programming information and tutorials, please visit The Game Programming Wiki!
Making Simple Card Games with Cards32.DLL
I'm sure that EVERYONE reading this has played MicroSoft's version of solitaire, hearts, or freecell. In fact, you've
likely played them millions of times! Such simple games, and yet they are insanely addictive. They are all based upon
one simple DLL that MicroSoft produced eons ago, Cards.DLL. That was the 16-bit version, Cards32.DLL is the latest 32-bit
version, and with it you can use VB to make simple, yet effective card games.
There are four primary functions that Cards32.DLL can perform. Declare them like this:
Declare Function cdtInit Lib "Cards32.Dll" (dx As Long, dy As Long) As Long
Declare Function cdtDrawExt Lib "Cards32.Dll" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal dx As Long, ByVal dy As Long, ByVal ordCard As Long, ByVal iDraw As Long, ByVal clr As Long) As Long
Declare Function cdtDraw Lib "Cards32.Dll" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal iCard As Long, ByVal iDraw As Long, ByVal clr As Long) As Long
Declare Function cdtTerm Lib "Cards32.Dll" () As Long
First, cdtInit can be used to initialize the Cards32.DLL for use. Be sure to store the two values returned by dx and
dy for future use as they will contain the width and height of the cards you are going to display.
Conversely, cdtTerm serves to terminate the Cards32.DLL session and release any memory held by the program. Call this in your
unload routines.
Now for the fun stuff. cdtDraw and cdtDrawExt can both be used to display cards on a form or picturebox. The only
difference between them is that cdtDrawExt can be used to draw cards of varying height and widths by utilizing the dx
and dy parameters. Here's how the other parameters breakdown:
hdc - hDC of a picturebox or form
X - X coordinate (in pixels) for the left-hand edge of the card
Y - Y coordinate (in pixels) for the top edge of the card
iCard - A number from 0 to 68 describing the type of card to display
iDraw - A value describing the method for displaying the card
clr - The colour with respect to which inversions will be determined
Ok, hdc, X, and Y are fairly straight forward. The only thing I have to add about those is that you may want to
set your form's ScaleMode property to "Pixels" to make calculating locations easier on you. And don't forget to set AutoRedraw to
"True" or else you'll get some wicked flicker.
iCard is an important one. Passing a number from 0-51 will result in the display of one of the standard 52 cards in the
deck. "0" corresponds to the Ace of Clubs, "1" corresponds to the Ace of Diamonds, "2" corresponds to the Ace of Hearts, and
"3" corresponds to the Ace of Spades. This trend continues all the way up to "51" which is the King of Spades. Any number above
51 will render one of the many different card backs or the "X" or "O" card (seen in solitaire, the red "X" shows up when the game
is over and you cannot draw again, the green "O" shows up when you've gone through the deck but can still continue). The
exact values for these images can be found in the sample project included with this tutorial.
iDraw is necessary to ensure that the item you've indicated with iCard displays correctly. If you've chosen an
iCard value from 0 to 51 (an normal card) then you must pass "0" (zero) as iDraw, as this will indicate that you're
attempting to draw one of the card faces. If you are trying to show the back of a card, you must pass "1" (one) as iDraw,
to indicate you're drawing a card back. If you pass "2" (two) as iDraw, you can invert the colours of any of the standard
cards in the deck (0-51).
The value you pass as clr will determine how cards will be coloured when they are "inverted". You want to invert a card's
colouring to indicate to the user that the card is "selected" so they know on which card an action can be performed. The value you
pass as clr should be vbWhite (the Visual Basic constant) in my opinion, as it gives the most attractive results. You
could alternatively pass Me.BackColor or some other colour value, but you'll notice that some cards appear purple or other
strange shades when inverted with colours other than white as the reference. Try messing around with this value, you'll see what I
mean. The reason that MicroSoft has included this (I believe) is to ensure that cards are still easily readable when inverted regardless of
the background behind them. Therefore if you do pass Me.BackColor as clr your inverted cards will certainly be readable
but not always the most attractive. If you're going with the standard "card playing green" background, vbWhite will serve
you well.
Perhaps a few examples are in order:
cdtDraw Me.hdc, 100, 100, 4, 0, vbWhite
This would draw a standard sized Two of Clubs at (100,100).
This would draw a 100pixel by 100pixel (square) King of Spades at (200,200).
cdtDraw Me.hdc, 0, 0, 8, 2, vbWhite
This would draw an inverted standard sized Three of Clubs at the top left corner of the form (0,0).
I think you get the picture. Now, the rest of the knowledge you require to create a basic card game using the Cards32.DLL is simple
VB programming. Things such as MouseDown events can be used to determine the location of player clicks, while Paint events can
be used to redraw the current game state. Have a look at my fully functional version of Crazy Eights to
witness the remarkable ease with which Windows cards games may be produced.