查看: 3726|回复: 0

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

发表于 2018-2-9 08:00:00

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Runtime.InteropServices;
  9. using System.Windows.Forms.Design;
  10. namespace GetCards
  11. {
  12. public partial class Form1 : Form
  13. {
  14. [DllImport("cards.dll")]
  15. public static extern bool cdtInit(ref int width, ref int height);
  16. [DllImport("cards.dll")]
  17. public static extern void cdtTerm();
  18. [DllImport("cards.dll")]
  19. public static extern bool cdtDraw(IntPtr hdc,int x,int y,int card,int mode,long color);
  20. //mode=0表正面,1表反面,Color我从0-0xFF000试了很多,好象没颜色改变
  21. //[DllImport("cards.dll")]
  22. //public static extern bool cdtDrawExt(IntPtr hdc,int x,int y,int dx,int dy,int card,int type,long color);
  23. //[DllImport("cards.dll")]
  24. //public static extern bool cdtAnimate(IntPtr hdc,int cardback,int x,int y,int frame);
  25. int[] bb = new int[100];
  26. public Form1()
  27. {
  28. InitializeComponent();
  29. }
  30. private void Form1_Load(object sender, EventArgs e)
  31. {
  32. int width, height;
  33. width = 0; height = 0;
  34. cdtInit(ref width, ref height);
  35. }
  36. private void btn_PaintCard_Click(object sender, EventArgs e)
  37. {
  38. int i, k, left_x, top_y, CardId;
  39. for (k = 0; k <= 3; k++)
  40. {
  41. for (i = 1; i <= 13; i++)
  42. {
  43. left_x = 20 + (i - 1) * 15; //牌的重叠后的宽度是15
  44. top_y = 20 + k * 100; //每行13张牌.高度是20
  45. CardId = (i - 1) * 4 + k; //原来52张牌是编了号的
  46. cdtDraw(this.CreateGraphics().GetHdc(), left_x, top_y, CardId, 0,9);
  47. }
  48. }
  49. }
  50. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  51. {
  52. cdtTerm();
  53. }
  54. private void btn_PaintBack_Click(object sender, EventArgs e)
  55. {
  56. int i, left_x, top_y, BackId;
  57. for (i = 0; i <= 11; i++) //12张牌背面图
  58. {
  59. BackId = i;
  60. top_y = 20 + (i & 3) * 100; //小于等于3的不变,>3的截尾,相当于竖排
  61. left_x = 20 + (i >> 2) * 80 + 180 + 80; //左边牌占15*12+80=260,也就是和最右张牌20(隐含了牌大小=80)
  62. cdtDraw(this.CreateGraphics().GetHdc(), left_x, top_y, 54 + BackId, 1, 9);
  63. }
  64. }
  65. private void btn_Random1_Click(object sender, EventArgs e) //第一种方法实现随机交换牌
  66. {
  67. int ii, k, left_x, top_y, CardId;
  68. int[] theArray = new int[52];
  69. Random r = new Random();
  70. listBox1.Items.Clear();
  71. for (int i = 0; i < 52; i++)
  72. {
  73. theArray[i] = i + 1;
  74. }
  75. for (int i = 0; i < 52; i++) //就是做52次随机交换两张牌
  76. {
  77. int a = r.Next(52); //生成0--->51的随机数
  78. int b = r.Next(52);
  79. int tmp = theArray[a];
  80. theArray[a] = theArray[b];
  81. theArray[b] = tmp;
  82. }
  83. for (int i = 0; i < 52; i++)
  84. {
  85. listBox1.Items.Add(theArray[i]);
  86. k = (int)(i / 13);
  87. ii = i % 13 + 1;
  88. left_x = 20 + (ii - 1) * 15;
  89. top_y = 20 + k * 100;
  90. CardId = theArray[i] - 1;
  91. cdtDraw(this.CreateGraphics().GetHdc(), left_x, top_y, CardId, 0, 9);
  92. }
  93. }
  94. private void btn_Random2_Click(object sender, EventArgs e) //第一种方法实现随机交换牌
  95. {
  96. int ii, k, left_x, top_y, CardId;
  97. int[] theArray = new int[52];
  98. int i = 0;
  99. while (i < theArray.Length)
  100. {
  101. theArray[i] = ++i;
  102. }
  103. Random r = new Random();
  104. listBox1.Items.Clear();
  105. while (i > 1) //从51-->1依次随机向前交换获得最终值
  106. {
  107. int j = r.Next(i);
  108. int t = theArray[--i];
  109. theArray[i] = theArray[j];
  110. theArray[j] = t;
  111. }
  112. for (i = 0; i < theArray.Length; ++i)
  113. {
  114. listBox1.Items.Add(theArray[i].ToString());
  115. k = (int)(i / 13);
  116. ii = i % 13 + 1;
  117. left_x = 20 + (ii - 1) * 15;
  118. top_y = 20 + k * 100;
  119. CardId = theArray[i] - 1;
  120. cdtDraw(this.CreateGraphics().GetHdc(), left_x, top_y, CardId, 0, 9);
  121. }
  122. }
  123. }
  124. }
复制代码

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

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

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

您可能感兴趣的文章:

  • 深入C# winform清除由GDI绘制出来的所有线条或图形的解决方法
  • c#图片处理之图片裁剪成不规则图形
  • C#实现判断图形文件格式的方法
  • C#画笔Pen保存和恢复图形对象的设置方法
  • C#画笔Pen使用路径绘制图形的方法
  • C#实现在图像中绘制文字图形的方法
  • C#使用Region对图形区域构造和填充的方法
  • C#图形区域剪切的实现方法
  • C#实现图形区域组合操作的方法
  • 使用C#编写简单的图形化的可发送附件的邮件客户端程序
  • C#利用GDI绘制常见图形和文字


回复

使用道具 举报