【问题标题】:blinking panels with pictures by adding a few more通过添加更多的图片闪烁面板
【发布时间】:2013-09-18 13:24:40
【问题描述】:

我有一个小应用程序,其中一个面板包含另外 6 个面板(在此面板图片内),当我单击按钮时,此面板的底部会动态创建几个面板,当面板创建整个面板时,此面板会闪烁.. .它看起来不太好...请告诉我,该怎么办?

我创建面板的循环,单击按钮创建面板会添加相同数量的面板

for (int i = 0; i < list_afy_add.Count; i++)
{
    Panel main_panel = new Panel();
    main_panel.Name = i.ToString();
    main_panel.Width = 308;
    main_panel.BackColor = Color.Transparent;
    main_panel.Location = new Point(x, y);

    Panel panel = new Panel();
    panel.Name = i.ToString();
    panel.MouseEnter += new EventHandler(panel_MouseEnter);
    panel.Width = 300;
    panel.Location = new Point(3, 5);

    Label textBox_date = new Label();
    panel.Controls.Add(textBox_date);
    textBox_date.Name = "textBox_date" + i.ToString();
    textBox_date.Location = new Point(220, 8);
    textBox_date.Size = new System.Drawing.Size(70, 15);
    textBox_date.BorderStyle = BorderStyle.None;
    textBox_date.MinimumSize = new System.Drawing.Size(72, 15);
    textBox_date.TextAlign = ContentAlignment.MiddleRight;
    textBox_date.BackColor = Color.FromArgb(((int)(((byte)(203)))), ((int)(((byte)(208)))), ((int)(((byte)(217)))));
    textBox_date.Anchor = AnchorStyles.Right | AnchorStyles.Top;
    textBox_date.ForeColor = SystemColors.InactiveCaption;

    Label textBox_name = new Label();
    panel.Controls.Add(textBox_name); 
    textBox_name.Size = new System.Drawing.Size(100, 15);
    textBox_name.MinimumSize = new System.Drawing.Size(100, 15);
    textBox_name.Location = new Point(5, 8);
    textBox_name.BorderStyle = BorderStyle.None;
    textBox_name.BackColor = Color.FromArgb(((int)(((byte)(203)))), ((int)(((byte)(208)))), ((int)(((byte)(217)))));
    textBox_name.ForeColor = SystemColors.InactiveCaption;

    Label textBox_msg = new Label();
    panel.Controls.Add(textBox_msg);
    textBox_msg.Location = new Point(5, 30);
    textBox_msg.Name = i.ToString();
    textBox_msg.Tag = list_afy_add[i]._AddEventNotification.eventNotificationId;
    textBox_msg.BorderStyle = BorderStyle.None;
    textBox_msg.BackColor = Color.FromArgb(((int)(((byte)(203)))), ((int)(((byte)(208)))), ((int)(((byte)(217)))));
    textBox_msg.ForeColor = SystemColors.Highlight;
    textBox_msg.Cursor = Cursors.Arrow;
    textBox_msg.MaximumSize = new System.Drawing.Size(280, 100);
    textBox_msg.MinimumSize = new System.Drawing.Size(280, 14);
    textBox_msg.AutoSize = true;
    textBox_msg.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;

    Label labelOpenChat = new Label(); 
    labelOpenChat.Size = new System.Drawing.Size(90, 15);
    labelOpenChat.Text = "Открыть чат";
    labelOpenChat.Font = new Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(200)));
    labelOpenChat.Location = new Point(10, 8);
    labelOpenChat.BorderStyle = BorderStyle.None;
    labelOpenChat.BackColor = Color.FromArgb(((int)(((byte)(203)))), ((int)(((byte)(208)))), ((int)(((byte)(217)))));
    labelOpenChat.ForeColor = SystemColors.InactiveCaption;
    panel.Controls.Add(labelOpenChat);


    main_panel.Controls.Add(panel);
    panel1.Controls.Add(main_panel);
    panel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
    main_panel.Dock = DockStyle.Top;

    date_and_msg_check(list_afy_add[i]._AddEventNotification.eventNotificationName, list_afy_add[i]._AddEventNotification.eventNotificationDate, list_afy_add[i]._AddEventNotification.eventNotificationMsg); 
    textBox_name.Text = valForName; 
    textBox_date.Text = valForDate;
    textBox_msg.Text = valForMsg;

    int height = textBox_msg.Size.Height + textBox_name.Height + 19;
    panel.Height = height; 
    main_panel.Height = height + 10; 
    int panHei = panel.Height;


    /*--------------------------------*/
    Panel panel_top_left = new Panel();
    Panel panel_top_right = new Panel();
    Panel panel_bottom_left = new Panel();
    Panel panel_bottom_right = new Panel();

    Bitmap btm_msg_panel_top_left = new Bitmap(Properties.Resources.blue_t_l1);
    panel_top_left.BackgroundImage = btm_msg_panel_top_left;
    Bitmap btm_msg_panel_top_right = new Bitmap(Properties.Resources.blue_t_r);
    panel_top_right.BackgroundImage = btm_msg_panel_top_right;
    Bitmap btm_msg_panel_bottom_left = new Bitmap(Properties.Resources.blue_b_l);
    panel_bottom_left.BackgroundImage = btm_msg_panel_bottom_left;
    Bitmap btm_msg_panel_bottom_right = new Bitmap(Properties.Resources.blue_b_r);
    panel_bottom_right.BackgroundImage = btm_msg_panel_bottom_right;

    panel_top_left.Width = panel.Width;
    panel_top_left.Height = height + 2;
    panel_top_right.Width = btm_msg_panel_top_right.Width;
    panel_bottom_left.Height = btm_msg_panel_bottom_left.Height;
    panel_bottom_left.Width = btm_msg_panel_bottom_left.Width;
    panel_bottom_right.Height = btm_msg_panel_bottom_right.Height;
    panel_bottom_right.Width = btm_msg_panel_bottom_right.Width;

    panel_top_right.Location = new Point(panel_top_left.Width - 4, 0);
    panel_bottom_left.Location = new Point(0, panel_top_left.Height - panel_bottom_left.Height);
    panel_bottom_right.Location = new Point(panel_top_left.Width - 4, panel_top_left.Height - panel_bottom_right.Height);


    panel_top_right.Anchor = AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
    panel_bottom_right.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
    panel_bottom_left.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
    panel_top_left.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
    /*--------------------------------*/


    Button delButt = new Button();
    delButt.Name = list_afy_add[i]._AddEventNotification.eventNotificationId;
    delButt.Click += new EventHandler(delButt_Click);
    delButt.Size = new System.Drawing.Size(12, 12);
    delButt.Location = new Point(292, 6);
    delButt.Anchor = AnchorStyles.Right | AnchorStyles.Top;
    delButt.BackColor = Color.Transparent;
    delButt.FlatStyle = FlatStyle.Flat;
    delButt.FlatAppearance.BorderSize = 0;
    delButt.FlatAppearance.MouseDownBackColor = Color.Transparent;
    delButt.FlatAppearance.MouseOverBackColor = Color.Transparent;
    delButt.BackgroundImage = bmp_close_normal;
    panel_top_left.Controls.Add(delButt);
    delButt.Hide();

    panel.Controls.Add(panel_bottom_right);
    panel.Controls.Add(panel_top_right);
    panel.Controls.Add(panel_bottom_left);
    panel.Controls.Add(panel_top_left);
}

【问题讨论】:

  • 欢迎来到重叠绘制调用的世界,您可以尝试将双缓冲区属性设置为 true,将扩展样式设置为 ClipChildren,或者基本上只是尝试看看是否有更好的布局可以使用,或this
  • 首先,我将“main_panel”替换为TableLayoutPanel...这将使布局和定位更易于控制。除此之外,DoubleBuffered 是个好主意。你甚至可以考虑自己接管Paint。
  • 谢谢,但我试过 DoubleBuffered... 没有帮助..
  • @Zuhan 你到底试过什么?将DoubleBuffered 设置为true 或使用TableLayoutPanel?
  • @Zuhan 那只是为了UserControl(容器),那你的panels呢?

标签: c# .net winforms


【解决方案1】:

您可以尝试使用Reflection 将所有面板的DoubleBuffered 设置为true,如下所示:

public static class PanelExtensions {
   public static void EnableDoubleBuffered(this Panel panel){
     typeof(Panel).GetProperty("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance)
                  .SetValue(panel, true, null);
   }
}
//Use it
yourPanel.EnableDoubleBuffered();

注意:我不确定这是否有效,只是在此处发布以帮助您尝试一些可以解决您的问题的方法,否则我不知道如何改进它并将删除此答案.

【讨论】:

  • hm.. 什么是 BindingFlags?抱歉,这个 BindingFlags 有错误 :)
  • @Zuhan 您必须在代码页的开头添加using System.Reflection;。
【解决方案2】:

在添加子控件之前尽量使用控件的SuspendLayout()方法,完成后不要忘记调用ResumeLayout()。这应该有助于减少或消除眨眼。

【讨论】:

猜你喜欢
  • 2013-05-27
  • 2014-03-19
  • 2017-09-08
  • 2010-10-23
  • 2015-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多