使用pictureBox1_Paint的问题?
想做一扑克游戏,用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;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private PictureBox[] PokerA = new PictureBox[14];
private int i, left_x, top_y, CardId;
[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);
[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);
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 button1_Click(object sender, EventArgs e)
{
for ( i = 1; i <= 3; i++)
{
left_x = 20 + (i - 1) * 20;
top_y = 20 ;
CardId = (i - 1) * 4;
PokerA[i] = new PictureBox();
PokerA[i].Width = 85;
PokerA[i].Height = 119;
PokerA[i].Left = left_x;
PokerA[i].Top = 20;
PokerA[i].Name = PokerA[i].ToString();
Controls.Add(PokerA[i]);
PokerA[i].BringToFront(); //将新增控件Z轴定义为最底层。
PokerA[i].Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint_1);
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
cdtDraw(g.GetHdc(), 5, 20,CardId, 0, 9);//此函数在pictureBox控件中画牌,CardId是不同的牌号。
g.ReleaseHdc();
}
用循环来画pictureBox,可不知为什么,每次画出的牌都是相同的牌,也就是循环到最后的CardId的牌。
这个pictureBox1_Paint这样循环调用为什么总是最后的值?