欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#编程调用Cards.dll实现图形化发牌功能示例

程序员文章站 2023-12-12 20:11:34
本文实例讲述了c#编程调用cards.dll实现图形化发牌功能。分享给大家供大家参考,具体如下: using system; using system.coll...

本文实例讲述了c#编程调用cards.dll实现图形化发牌功能。分享给大家供大家参考,具体如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using system.runtime.interopservices;
using system.windows.forms.design;
namespace getcards
{
  public partial class form1 : form
   {
     [dllimport("cards.dll")]
    public static extern bool cdtinit(ref int width, ref int height);
     [dllimport("cards.dll")]
    public static extern void cdtterm();
     [dllimport("cards.dll")]
    public static extern bool cdtdraw(intptr hdc,int x,int y,int card,int mode,long color);
    //mode=0表正面,1表反面,color我从0-0xff000试了很多,好象没颜色改变
    //[dllimport("cards.dll")]
    //public static extern bool cdtdrawext(intptr hdc,int x,int y,int dx,int dy,int card,int type,long color);
    //[dllimport("cards.dll")]
    //public static extern bool cdtanimate(intptr hdc,int cardback,int x,int y,int frame);
    int[] bb = new int[100];
    public form1()
     {
       initializecomponent();
     }
    private void form1_load(object sender, eventargs e)
     {
      int width, height;
       width = 0; height = 0;
       cdtinit(ref width, ref height);
     }
    private void btn_paintcard_click(object sender, eventargs e)
     {
      int i, k, left_x, top_y, cardid;
      for (k = 0; k <= 3; k++)
       {
        for (i = 1; i <= 13; i++)
         {
           left_x = 20 + (i - 1) * 15;        //牌的重叠后的宽度是15
           top_y = 20 + k * 100;           //每行13张牌.高度是20
           cardid = (i - 1) * 4 + k;         //原来52张牌是编了号的
           cdtdraw(this.creategraphics().gethdc(), left_x, top_y, cardid, 0,9);
         }
       }
     }
    private void form1_formclosed(object sender, formclosedeventargs e)
     {
       cdtterm();
     }
    private void btn_paintback_click(object sender, eventargs e)
     {
      int i, left_x, top_y, backid;
      for (i = 0; i <= 11; i++)              //12张牌背面图
       {
         backid = i;
         top_y = 20 + (i & 3) * 100;           //小于等于3的不变,>3的截尾,相当于竖排
         left_x = 20 + (i >> 2) * 80 + 180 + 80;     //左边牌占15*12+80=260,也就是和最右张牌20(隐含了牌大小=80)
         cdtdraw(this.creategraphics().gethdc(), left_x, top_y, 54 + backid, 1, 9);
       }
     }
    private void btn_random1_click(object sender, eventargs e) //第一种方法实现随机交换牌
     {
      int ii, k, left_x, top_y, cardid;
      int[] thearray = new int[52];
       random r = new random();
       listbox1.items.clear();
      for (int i = 0; i < 52; i++)
       {
         thearray[i] = i + 1;
       }
      for (int i = 0; i < 52; i++) //就是做52次随机交换两张牌
       {
        int a = r.next(52); //生成0--->51的随机数
        int b = r.next(52);
        int tmp = thearray[a];
         thearray[a] = thearray[b];
         thearray[b] = tmp;
       }
      for (int i = 0; i < 52; i++)
       {
         listbox1.items.add(thearray[i]);
         k = (int)(i / 13);
         ii = i % 13 + 1;
         left_x = 20 + (ii - 1) * 15;
         top_y = 20 + k * 100;
         cardid = thearray[i] - 1;
         cdtdraw(this.creategraphics().gethdc(), left_x, top_y, cardid, 0, 9);
       }
     }
    private void btn_random2_click(object sender, eventargs e) //第一种方法实现随机交换牌
     {
      int ii, k, left_x, top_y, cardid;
      int[] thearray = new int[52];
      int i = 0;
      while (i < thearray.length)
       {
         thearray[i] = ++i;
       }
       random r = new random();
       listbox1.items.clear();
      while (i > 1) //从51-->1依次随机向前交换获得最终值
       {
        int j = r.next(i);
        int t = thearray[--i];
         thearray[i] = thearray[j];
         thearray[j] = t;
       }
      for (i = 0; i < thearray.length; ++i)
       {
         listbox1.items.add(thearray[i].tostring());
         k = (int)(i / 13);
         ii = i % 13 + 1;
         left_x = 20 + (ii - 1) * 15;
         top_y = 20 + k * 100;
         cardid = thearray[i] - 1;
         cdtdraw(this.creategraphics().gethdc(), left_x, top_y, cardid, 0, 9);
       }
     }
   }
}

界面设计的话截图比贴designer.cs省事多了:

C#编程调用Cards.dll实现图形化发牌功能示例

C#编程调用Cards.dll实现图形化发牌功能示例

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#图片操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。

上一篇:

下一篇: