Since the weather was bad yesterday and (it appears) it’s going to be same-o-same-o today, I decided to check into (this last month’s) Card objects project. All earlier created objects were ‘non-visual’ objects and since we’re playing cards, it probably makes sense to create a couple of visual components that show the player’s hands.
I decided to base my custom component on the PictureBox control, mainly because I’m not concerned about using specific drag and drop effects at this stage. If you make a descendant of the PictureBox and plan to do some custom drawing, you should override the PictureBox’s OnPaint event:
protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); if (fhand != null) { pe.Graphics.Clear(this.BackColor); // main drawing starts here switch (this.Orientation) { case THandOrientiation.HandHorizontalUp : // Drawstuff break; case THandOrientiation.HandVerticalLeft : // Drawstuff break; case THandOrientiation.HandHorizontalDown : // Drawstuff break; case THandOrientiation.HandVericalRight : // Drawstuff break; } } }
Next part was to get my sets of card styles (currently 2) and add both sets as two separate resource files, with each bitmap named appropriately (as in ‘consistent’) so that only one call is needed to actually draw the current selected player’s cards (or their backs).
private Bitmap GetBitmapCard(TCard acard) { string s = "_" + CardFunctions.GetCardChar(acard); Bitmap nbitmap = null; if (acard.IsCardOpen) { switch (fcardset) { case TCardSet.Default: nbitmap = (Bitmap) BetterSpades.card_defaultset.ResourceManager.GetObject(s); break; case TCardSet.Roman: nbitmap = (Bitmap) BetterSpades.card_romanset.ResourceManager.GetObject(s); break; } } else { nbitmap = (Bitmap)BetterSpades.card_backs.ResourceManager.GetObject( CardFunctions.GetCardBack(this.CardBack) ); } return nbitmap; }
And then finally, to wrap things up: selecting one (or multiple) card(s) from that very same custom control: Code was added to an overriden ‘MouseUp event’. The event converts the (current) mouse’s coordinates to an actual card index and either toggles a card ‘selected or unselected’:
protected override void OnMouseUp(MouseEventArgs e) { if (fhand != null) { if (e.Button == MouseButtons.Left) { int i = FindIndex(e.X, e.Y); if (i > -1) { this.fhand[i].IsCardSelected = !this.fhand[i].IsCardSelected; // call the overridden OnPaint Event! this.Refresh(); } } } base.OnMouseUp(e); }
Next up: the Table and Deck visual components.