winforms 删除用户控件会导致“denenv.exe -系统错误”错误

643ylb08  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(143)

有一个属性为“BindingContainer”的Panel类型用户控件,它将UC放置在所需的容器中。

当尝试仅移除UC时-没有问题。但是,如果使用“属性”窗口设置“BindingContainer”属性并尝试删除(其中包含元素的容器),则会出现“denenv.exe -系统错误”-无法为堆栈创建新的保护页。Visual Studio将关闭。
-- UC透明面板--

public class TransparentPanel : Panel {
    protected override CreateParams CreateParams {
        get {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return cp;
        }
    }
    
    protected override void OnPaintBackground(PaintEventArgs e) {
        //base.OnPaintBackground(e);
    }

    protected override void OnCreateControl() {
        base.OnCreateControl();

        if (!DesignMode && BindingContainer != null) {
            this.Size = new Size(BindingContainer.Width - 20, BindingContainer.Height);
            this.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;

            this.BringToFront();
        }
    }

    public new bool DesignMode { get; } =
        (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");

    private Control bindingContainer;
    public Control BindingContainer {
        get { return bindingContainer; }
        set {
            bindingContainer = value;
            BindingContainerChanged();
        }
    }

    private void BindingContainerChanged() {
        if (BindingContainer == null) return;

        BindingContainer.Controls.Add(this);

        this.Location = new Point(0, 0);
        this.Size = new Size(15, BindingContainer.Height);
        this.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom;
    }
  }
gorkyyrv

gorkyyrv1#

正常工作的代码。

public class TransparentPanel : Panel {
    public TransparentPanel() {
        this.Size = new Size(110, 55);

        SetStyle(ControlStyles.Opaque |
            ControlStyles.ResizeRedraw, true);

        UpdateStyles();
    }

    protected override CreateParams CreateParams {
        get {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return cp;
        }
    }

    protected override void OnCreateControl() {
        if (!IsDesignMode(this)) {
            this.Location = new Point(0, 0);
            this.BorderStyle = BorderStyle.FixedSingle;
            this.Size = new Size(BindingContainer.Width - 20, BindingContainer.Height);
            this.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;

            this.BringToFront();
        }
    }

    protected override void OnParentChanged(EventArgs e) {
        BindingContainer = this.Parent;
    }

    public Control BindingContainer { get; private set; }

    // Can be moved to a separate class
    public bool IsDesignMode(IComponent component) {
        bool ret = false;

        if (null != component) {
            ISite site = component.Site;

            if (null != site) {
                ret = site.DesignMode;
            } else if (component is Control) {
                IComponent parent = (component as Control).Parent;
                ret = IsDesignMode(parent);
            }
        }

        return ret;
    }
}

相关问题