弹出用户控件在创建时无法正确呈现

时间:2012-06-15 09:07:43

标签: c# winforms controls drawing

我有一个类来处理一种用户控件弹出窗口,它通过继承System.Windows.Forms.ToolStripDropDown来工作。这对我目前的弹出类型有用,我将在下面详述;

首先,是用于以弹出类型样式保存用户控件的类;

 public class PopupWindow : System.Windows.Forms.ToolStripDropDown
{
    private System.Windows.Forms.Control _content;
    private System.Windows.Forms.ToolStripControlHost _host;

    public PopupWindow(System.Windows.Forms.Control content)
    {
        //Basic setup...
        this.AutoSize = false;
        this.DoubleBuffered = true;
        this.ResizeRedraw = true;
        this.BackColor = content.BackColor;

        this._content = content;
        this._host = new System.Windows.Forms.ToolStripControlHost(content);

        //Positioning and Sizing
        this.MinimumSize = content.MinimumSize;
        this.MaximumSize = content.Size;
        this.Size = content.Size;
        content.Location = Point.Empty;

        //Add the host to the list
        this.Items.Add(this._host);
    }
}

正如我们在这里看到的,我只是将控件传递给它,并让它完成工作。在“onclick”弹出窗口中使用它时,就像这样,它工作正常;

 public void Popup(object sender, MouseEventArgs e, other params)
    {

        DevicePopup popupDevice = new DevicePopup();

          //do stuff to the control here before displaying

        PopupWindow popup = new PopupWindow(popupDevice);
        popup.Show(Cursor.Position);

    }

并像这样调用它;

  this.Controls[btnAdd.Name].MouseClick += (sender, e) =>
            {
                int index = temp;
                generatePopup.Popup(sender, e, mDevices[index], this);
            };

按照预期成功执行此操作会在鼠标单击时创建弹出式用户控件。

然而,我现在正在尝试使用第二种类型的弹出窗口,当出现问题时会产生弹出窗口。下面是我的新弹出类,并调用它;

public void AlarmNotificationPopup(IDeviceInterface device)
     {


         try
         {
             AlarmNotification ANotification = new AlarmNotification();

       //do stuff to the control again before displaying


             PopupWindow popup = new PopupWindow(ANotification);
             popup.Show(100, 100);


         }
         catch (Exception e)
         {
             MessageBox.Show(e.Message);
         }


     }


    AlarmNotificationPopup(device);

但是,这个弹出窗口没有正确渲染/创建,看起来像这样;

Rendering issue

我不完全确定如何解决这个问题。有人有什么想法吗?

1 个答案:

答案 0 :(得分:0)

要尝试的几件事情:

我认为您不需要“基本设置”,因此请注释掉。另外,尝试设置边距和填充:

public PopupWindow(Control content) {
  // Basic setup...
  // this.AutoSize = false;
  // this.DoubleBuffered = true;
  // this.ResizeRedraw = true;
  // this.BackColor = content.BackColor;

  this._content = content;
  this._host = new ToolStripControlHost(content);
  this._host.Margin = new Padding(0);
  this._host.Padding = new Padding(0);
  this.Padding = new Padding(0);
  this.Margin = new Padding(0);

  //Positioning and Sizing
  this.MinimumSize = content.MinimumSize;
  this.MaximumSize = content.Size;
  this.Size = content.Size;
  content.Location = Point.Empty;

  //Add the host to the list
  this.Items.Add(this._host);
}

确保您的AlarmNotification设置了MinimumSize属性。

如果这无法解决问题,那么您可能需要记录AlarmNotification类正在做什么。