winforms刷新多个面板

时间:2018-08-13 18:04:39

标签: c# winforms

我正在构建一个扑克游戏,需要同时展示多副扑克牌。我可以显示1、3、5或10。

我有1只主手会一直显示,并且我会根据玩家想玩的手的数量来激活不同的视图。

我有2个主面板(main_hand_panel和extra_hands_panel)

我将所有面板添加到List中,当时间到时,我在其上调用ShowHand

我将第一手扑克牌添加到main_hand_panel中,如下所示:

Point startPosition = new Point(0, 0);
ComponentResourceManager resources = new ComponentResourceManager(typeof(MainScreen));
var mainPokerHand = new PokerPanel(startPosition, main_hand_panel.Size, offset, cardSize);
mainPokerHand.Initialize(resources);
this.main_hand_panel.Controls.Add(mainPokerHand);
allHands.Add(mainPokerHand);

然后根据显示的画面绘制并添加其他手形(显示五个手形示例)

public void DrawFivePlay(ComponentResourceManager resources)
{
    Point startPosition = new Point(0, 0);
    var containerSize = extra_hands_panel.Size;
    containerSize.Height = Convert.ToInt32(containerSize.Height / 2);
    containerSize.Width = Convert.ToInt32(containerSize.Width / 2);
    for (int i = 0; i < 2; i++)
    {
        startPosition.Y = containerSize.Height * i;
        var pokerHand = new PokerPanel(startPosition, containerSize, scale(offset, .30), scale(cardSize, .8));
        pokerHand.Initialize(resources);
        this.extra_hands_panel.Controls.Add(pokerHand);
        allHands.Add(pokerHand);
    }

    startPosition.X = containerSize.Width;
    startPosition.Y = 0;
    for (int i = 0; i < 2; i++)
    {
        startPosition.Y = containerSize.Height * i;
        var pokerHand = new PokerPanel(startPosition, containerSize, scale(offset, .30), scale(cardSize, .8));
        pokerHand.Initialize(resources);
        this.extra_hands_panel.Controls.Add(pokerHand);
        allHands.Add(pokerHand);
    }
}

当我准备展示双手时,我会调用manifest_click,它会遍历List中的所有双手并显示它们。

private void reveal_Click(object sender, EventArgs e)
{
    foreach (var hand in allHands)
    {
        hand.ShowHand();
    }
    Application.DoEvents();
}

有趣的是,它不显示主手,而是显示所有其他手,但是唯一显示第一手的时间是在程序刚刚启动时,我们只演奏一只手。如果我显示其他任何一个游戏选项,则第一手牌将不再显示。

这是PokerPanel代码:

namespace TEX_DrawPoker
{
    public class PokerPanel : Panel
    {
        private PictureBox pictureBox1_5;
        private PictureBox pictureBox1_1;
        private PictureBox pictureBox1_2;
        private PictureBox pictureBox1_3;
        private PictureBox pictureBox1_4;

        Timer drawTimer = new Timer();
        int timerTick = 0;
        string[] pokerHand;

        Size panelSize = new Size(5*229, 275);
        Size cardSize = new Size(146, 202);
        Point startPosition = new Point(0, 3);
        Point firstCardPosition = new Point(0, 0);
        Point offset  = new Point(160, 0);

        public PokerPanel(Point _startPosition,Size _panelSize, Point _offset, Size _cardSize)
        {
            drawTimer.Tick += new EventHandler(startDisplay);
            drawTimer.Interval = 100;
            drawTimer.Enabled = false;

            panelSize = _panelSize;
            cardSize = _cardSize;
            offset = _offset;
            startPosition = _startPosition;
        }

        public void startDisplay(object sender, EventArgs e)
        {
            switch (timerTick)
            {
                case 0:
                    this.pictureBox1_1.Image = (Image)(new Bitmap(Image.FromFile(".\\img\\cards\\" + pokerHand[timerTick] + ".png"), cardSize));
                    break;
                case 1:
                    this.pictureBox1_2.Image = (Image)(new Bitmap(Image.FromFile(".\\img\\cards\\" + pokerHand[timerTick] + ".png"), cardSize));
                    break;
                case 2:
                    this.pictureBox1_3.Image = (Image)(new Bitmap(Image.FromFile(".\\img\\cards\\" + pokerHand[timerTick] + ".png"), cardSize));
                    break;
                case 3:
                    this.pictureBox1_4.Image = (Image)(new Bitmap(Image.FromFile(".\\img\\cards\\" + pokerHand[timerTick] + ".png"), cardSize));
                    break;
                case 4:
                    this.pictureBox1_5.Image = (Image)(new Bitmap(Image.FromFile(".\\img\\cards\\" + pokerHand[timerTick] + ".png"), cardSize));
                    break;
            }

            if (timerTick >= 4)
            {
                drawTimer.Stop();
                timerTick = 0;
            }
            else
            {
                timerTick++;
            }
        }

        public void ShowHand()
        {
            pokerHand = Deck.shuffle();
            drawTimer.Start();
        }

        public void Reset()
        {
            Image back = Image.FromFile(".\\img\\cards\\back.png");
            this.pictureBox1_1.Image = (Image)(new Bitmap(back, cardSize));
            this.pictureBox1_2.Image = (Image)(new Bitmap(back, cardSize));
            this.pictureBox1_3.Image = (Image)(new Bitmap(back, cardSize));
            this.pictureBox1_4.Image = (Image)(new Bitmap(back, cardSize));
            this.pictureBox1_5.Image = (Image)(new Bitmap(back, cardSize));
        }

        public void Initialize(ComponentResourceManager resources)
        {
            Image back = Image.FromFile(".\\img\\cards\\back.png");

            this.pictureBox1_1 = new System.Windows.Forms.PictureBox();
            this.pictureBox1_2 = new System.Windows.Forms.PictureBox();
            this.pictureBox1_3 = new System.Windows.Forms.PictureBox();
            this.pictureBox1_4 = new System.Windows.Forms.PictureBox();
            this.pictureBox1_5 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1_1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1_2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1_3)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1_4)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1_5)).BeginInit();
            this.SuspendLayout();
            // 
            // hand_1
            // 
            this.Controls.Add(this.pictureBox1_5);
            this.Controls.Add(this.pictureBox1_4);
            this.Controls.Add(this.pictureBox1_3);
            this.Controls.Add(this.pictureBox1_2);
            this.Controls.Add(this.pictureBox1_1);

            this.Location = startPosition;
            this.Name = "hand_1";
            this.Size = panelSize;
            this.TabIndex = 0;
            // 
            // pictureBox1_1
            // 
            this.pictureBox1_1.Image = (Image)(new Bitmap(back, cardSize));
            this.pictureBox1_1.Location = firstCardPosition;
            this.pictureBox1_1.Name = "pictureBox1_1";
            this.pictureBox1_1.Size = cardSize;
            this.pictureBox1_1.TabIndex = 4;
            this.pictureBox1_1.TabStop = false;
            // 
            // pictureBox1_2
            // 
            var positionCard_2 = firstCardPosition;
            positionCard_2.Offset(offset);
            this.pictureBox1_2.Image = (Image)(new Bitmap(back, cardSize));
            this.pictureBox1_2.Location = positionCard_2; 
            this.pictureBox1_2.Name = "pictureBox1_2";
            this.pictureBox1_2.Size = cardSize;
            this.pictureBox1_2.TabIndex = 3;
            this.pictureBox1_2.TabStop = false;
            // 
            // pictureBox1_3
            // 
            var positionCard_3 = positionCard_2;
            positionCard_3.Offset(offset);
            this.pictureBox1_3.Image = (Image)(new Bitmap(back, cardSize));
            this.pictureBox1_3.Location = positionCard_3;
            this.pictureBox1_3.Name = "pictureBox1_3";
            this.pictureBox1_3.Size = cardSize;
            this.pictureBox1_3.TabIndex = 2;
            this.pictureBox1_3.TabStop = false;
            // 
            // pictureBox1_4
            // 
            var positionCard_4 = positionCard_3;
            positionCard_4.Offset(offset);

            this.pictureBox1_4.Image = (Image)(new Bitmap(back, cardSize));
            this.pictureBox1_4.Location = positionCard_4;
            this.pictureBox1_4.Name = "pictureBox1_4";
            this.pictureBox1_4.Size = cardSize;
            this.pictureBox1_4.TabIndex = 1;
            this.pictureBox1_4.TabStop = false;
            // 
            // pictureBox1_5
            // 
            var positionCard_5 = positionCard_4;
            positionCard_5.Offset(offset);
            this.pictureBox1_5.Image = (Image)(new Bitmap(back, cardSize));
            this.pictureBox1_5.Location = positionCard_5;
            this.pictureBox1_5.Name = "pictureBox1_5";
            this.pictureBox1_5.Size = cardSize;
            this.pictureBox1_5.TabIndex = 0;
            this.pictureBox1_5.TabStop = false;

            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1_5)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1_4)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1_3)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1_2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1_1)).EndInit();
        }
    }
}

我还使用计时器来显示卡片,以便它们交错排列。

任何帮助将不胜感激。

谢谢

2 个答案:

答案 0 :(得分:0)

发现我的问题, 我没有清除换手之间的控件,所以我试图更新后台的面板。 Controls.Clear()在移到另一手之前为我修复了该问题。抱歉,漫长的帖子一无所获。 :(

答案 1 :(得分:-1)

您是否考虑过刷新整个表格?

Form1.Refresh();

或仅刷新面板

PokerPanel.Refresh();