本文实例讲述了c#编程调用cards.dll实现图形化发牌功能。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
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#程序设计有所帮助。