Using Cards.dll in a Windows Control Library

This project aims at designing a windows control library that encapsulates a playing card.

This control can be added to the tool box with other .NET controls. It Can be dragged at design time into the form area.

The control introduces concepts of interoperability between managed code and unmanaged code by calling the Windows cards.dll functions to paint its client area. So the card here is more than a painting.

Also it introduces a tip in VS.NET form designer, is that it executes any code written in the (OnPaint) function of any component or form. When you override the OnPaint and add the drawing code, then the designer will execute it for you to show how the control will appear at run-time.

You can create cards statically at design time or dynamically at run time and add them to the form surface. This can be achieved by a static function:


to randomize 52 playing cards.

To create a custom control whose project is named(windows control library project).

A card class inherits from System.Windows.Forms.UserControl which encapsulates a user defines control:

public class Card : System.Windows.Forms.UserControl

- There are some enumerations that serve the card control which are:

  1. Face : represents the number painted on the face of the card(1,2,3,., Queen and King).
  2. Suit: represents the suit of the card(Clubs, Diamonds, Hearts and Spades).
  3. Back: one of the card back bitmaps available in cards.dll.

Every card has (Card Properties) Face, Suit, Back which is categorized using the Description Attribute:

private void MyCancelButton_Click(object sender, System.EventArgs e)
// user cancelled the dialog, so just close it

- Basic Drawing functions imported from cards.dll with DllImport Attribute and :

using System.Runtime.InteropServices;
public static extern bool cdtInit (ref int width, ref int height);

A description of these functions is in their summary, and on

Note that any function imported from unmanaged code must be declared as static and extern function. The we overriding OnPiant to implement the code of drawing.

Note that every setting to a property, is followed by a cal to invalidate() to repaint the client area of the control to reflect the change, this is most important when you use the properties window of the designer.

this.suit = value;

After building the project you can create a windows application and

1. Right click on the tool box then select customize tool box


2. Choose .NET Framework tap, the browse for card.dll  in its location , click ok


3. Finally click ok to close the dialog and the card icon will be added to the current tool box. Click it and drag to the form area.

4. Set its properties and change its appearance.

Note: sometimes the designer fails to paint the card, although at run time the card is painted correctly.

Up Next
    Ebook Download
    View all
    View all