使用pictureBox1_Paint的问题?

junsheng 2009-01-29 08:45:36
想做一扑克游戏,用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这样循环调用为什么总是最后的值?
...全文
458 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
宝_爸 2009-01-30
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 junsheng 的回复:]
谢谢秋心,你的方法不错,不过我还是不明白,我的那样做为什么只会显示最后一个?我试着调试时确实button1_Click要全部执行完毕后才执行pictureBox1_Paint.这是为什么?
[/Quote]

这个,说来话长,win form默认是单线程程序。因此肯定在一个函数执行的时候是无法打断的,因此你的全局变量CardId在click执行完之后肯定是最后一个。

按你的思路应该是为一个picturebox赋值后,就要刷新它,事实上这可以通过picturebox.invalidate来实现(不确认,你需要试一试,不清楚invalidate底层是用委托还是消息机制怎样实现的)。但是如果你的窗口需要刷新(被别的窗口覆盖,或者是最小化再最大化)后,pictureBox1_Paint会被自动调用,这时候又刷新最后一张牌了。

宝_爸 2009-01-30
  • 打赏
  • 举报
回复
看起来,你是想在不同的picturebox中画不同的牌,而不是在一个picturebox中连续话不同的牌,这样就和线程没有问题了。
你可以从PictureBox继承一个类,加入一个成员记录CardId
class CardBox:PictureBox
{
int CardId;
}

public partial class Form1 : Form
{

private CardBox[] PokerA = new CardBox[14];

private int i, left_x, top_y;

.....
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 ;

PokerA[i] = new CardBox();
PokerA[i].CardId = (i - 1) * 4;

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;
CardBox cardBox = (CardBox)sender;
cdtDraw(g.GetHdc(), 5, 20,cardBox .CardId, 0, 9);//此函数在pictureBox控件中画牌,CardId是不同的牌号。
g.ReleaseHdc();


}
junsheng 2009-01-30
  • 打赏
  • 举报
回复
谢谢秋心,你的方法不错,不过我还是不明白,我的那样做为什么只会显示最后一个?我试着调试时确实button1_Click要全部执行完毕后才执行pictureBox1_Paint.这是为什么?
Scofield_zao 2009-01-30
  • 打赏
  • 举报
回复
来学习一下~~
feifeiyiwen 2009-01-30
  • 打赏
  • 举报
回复
ding
junsheng 2009-01-29
  • 打赏
  • 举报
回复
我不太明白,应如何刷新?
junsheng 2009-01-29
  • 打赏
  • 举报
回复
你这个思路是不对的。

由于你的程序单线程程序,因此button1_Click中必须要全部执行完毕,而且你又没有刷新的调用,因此当执行
pictureBox1_Paint时,CardId都是最后一个。你应该让每个picturebox对应一个CardId吧。



是这个意思,可这和单线程有关吗?
宝_爸 2009-01-29
  • 打赏
  • 举报
回复
你这个思路是不对的。

由于你的程序单线程程序,因此button1_Click中必须要全部执行完毕,而且你又没有刷新的调用,因此当执行
pictureBox1_Paint时,CardId都是最后一个。你应该让每个picturebox对应一个CardId吧。
king19840811 2009-01-29
  • 打赏
  • 举报
回复
mark
lovehongyun 2009-01-29
  • 打赏
  • 举报
回复
家里没环境,先标记一下,等上班如果lz还没解决,帮你搞一下
wuyq11 2009-01-29
  • 打赏
  • 举报
回复
位置是否为固定

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧