【问题标题】:pop up user control not rendering correctly when created创建时弹出用户控件未正确呈现
【发布时间】:2012-06-15 09:07:42
【问题描述】:

我有一个类可以处理一种用户控件弹出窗口,它通过从 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);

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

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

【问题讨论】:

  • 查看控件的填充和边距。他们是对的吗?
  • 据我所知看起来一样。

标签: c# winforms controls drawing


【解决方案1】:

有几件事可以尝试:

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

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 类正在做什么。

【讨论】:

    猜你喜欢
    • 2013-10-31
    • 2015-12-30
    • 2012-11-09
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 2018-07-10
    • 2019-08-26
    • 1970-01-01
    相关资源
    最近更新 更多