【问题标题】:winforms refresh multiple panelswinforms刷新多个面板
【发布时间】:2018-08-15 23:47:24
【问题描述】:

我正在构建一个扑克游戏,我需要同时展示多个扑克牌。我可以显示 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);
    }
}

当我准备好展示手牌时,我调用reveal_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();
        }
    }
}

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

任何帮助将不胜感激。

谢谢,

【问题讨论】:

  • mainPokerHand 被添加到main_hand_panel,其他手都被添加到extra_hands_panel。 main_hand_panel 和 extra_hands_panel 是否有可能重叠?或者您在main_hand_panel 上调用了 Hide() (Visible = false),这就是为什么当您有多只手时它不显示的原因?尝试将 main_hand_panel 的背景颜色设置为亮绿色,然后运行应用程序,看看亮绿色面板会发生什么。

标签: c# winforms


【解决方案1】:

发现我的问题, 我没有清除手动更改之间的控件,因此我试图更新后台的面板。 Controls.Clear() 在移动到下一手之前为我解决了这个问题。对不起,长篇大论一无所获。 :(

【讨论】:

    【解决方案2】:

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

    Form1.Refresh();
    

    或者只刷新面板

    PokerPanel.Refresh();
    

    【讨论】:

      猜你喜欢
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 2014-03-09
      相关资源
      最近更新 更多