通过添加更多图片来闪烁带有图片的面板

本文关键字:闪烁 有图片 添加 | 更新日期: 2023-09-27 18:31:52

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

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

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);
}

通过添加更多图片来闪烁带有图片的面板

您可以

尝试使用 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();

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

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