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

 更新时间:2017年06月26日 11:29:33   作者:songkexin  
这篇文章主要介绍了C#编程调用Cards.dll实现图形化发牌功能,结合实例形式分析了C#动态链接库调用及图形操作技巧,需要的朋友可以参考下

本文实例讲述了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#相关内容感兴趣的读者可查看本站专题:《C#图片操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结

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

相关文章

  • C#实现两个richtextbox控件滚动条同步滚动的简单方法

    C#实现两个richtextbox控件滚动条同步滚动的简单方法

    这篇文章主要给大家介绍了C#实现两个richtextbox控件滚动条同步滚动的简单方法,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-05-05
  • C#如何给枚举类型增加一个描述特性详解

    C#如何给枚举类型增加一个描述特性详解

    这篇文章主要给大家介绍了关于C#如何给枚举类型增加一个描述特性的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-02-02
  • C#多态详解

    C#多态详解

    这篇文章主要介绍了C#中的多态,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-10-10
  • C#信号量用法简单示例

    C#信号量用法简单示例

    这篇文章主要介绍了C#信号量用法,结合简单C#控制台应用程序形式分析了信号量的功能、定义、调用、释放等操作技巧,需要的朋友可以参考下
    2016-07-07
  • 在WinForm应用程序中快速实现多语言的处理的方法

    在WinForm应用程序中快速实现多语言的处理的方法

    在国际化环境下,越来越多的程序需要做多语言版本,这篇文章主要介绍了在WinForm应用程序中快速实现多语言的处理的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2018-07-07
  • C#实现微信分账功能的完整步骤

    C#实现微信分账功能的完整步骤

    这篇文章主要给大家介绍了关于C#实现微信分账功能的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-05-05
  • C#调用新浪微博API实例代码

    C#调用新浪微博API实例代码

    在本篇文章里小编给大家整理的是一篇关于C#调用微博API的相关知识点内容,有需要的朋友们可以学习下。
    2019-11-11
  • C#实现的几种委托方式介绍

    C#实现的几种委托方式介绍

    这篇文章主要是介绍C#实现的几种委托方式,需要的朋友可以参考下
    2013-03-03
  • C#文件操作类分享

    C#文件操作类分享

    这篇文章主要为大家分享了C#文件操作类的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • C#图片处理3种高级应用

    C#图片处理3种高级应用

    本文介绍C#图片处理高级应用,这些功能并无多大技术含量。全部基于.Net Framework类库完成,代码中包含了C#图片处理的一些基础知识,与大家分享,个人能力有限,不足之处还请及时指正。
    2015-10-10

最新评论