简体   繁体   中英

Visual Studio designer crashes when overrideing WndProc

I have a control inherited from System.Windows.Forms.Control which has the WndProc function overridden to pass the messages to an external DLL. The function looks like

public class GraphicsPanel : Control
{
        bool designMode;
        EngineWrapper Engine;

        public GraphicsPanel()
        {
            designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
            this.SetStyle(ControlStyles.Selectable, true);
            this.TabStop = true;
        }

        public void SetEngine(EngineWrapper Engine)
        {
            this.Engine = Engine;
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if(designMode) // Tried this option
                return;

            if (!designMode && Engine != null) // And this option
                Engine.ProcessWindowMessage(m.Msg, m.WParam, m.LParam);
        }
}

If I have any mention of Engine in this function, it crashes because the external DLL apparently isn't loaded when the designer is displayed. I get the error message "Could not load file or assembly "..." or one of its dependencies.

I certainly should add that this code all works at run-time just not design-time. It's annoying having to comment out the two Engine lines of code whenever I want to use the designer.

Any advice for how to get the designer to work correctly while including this line of code.

在此处输入图片说明

Do you need the WndProc to be executed in design-time? If not, you can try by adding a condition to the if:

bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
if (!designMode && Engine != null)
    ...

You will need System.ComponentModel namespace imported.

Detecting design mode from a Control's constructor

I solved this by moving the Engine references to a different function. This is odd but it works.

    void PassMessage(Message m)
    {
        if (Engine != null)
            Engine.ProcessWindowMessage(m.Msg, m.WParam, m.LParam);
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        if (!designMode)
            PassMessage(m);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM