Subclassing MS Word's window from a VSTO add-in

元气小坏坏 提交于 2020-01-06 19:36:49

问题


I'm trying to detect some events that VSTO doesn't provide such as WM_MOVE, WM_SIZE, etc in order to adjust the position of a window. This window is created by the add-in I'm working on and should react when the Word window is changed. I've basically manage to do my task but a very annoying problem remains. Whenever I close Word it pops up its crash handler. Obviously, this has something to do with the improper disposal of the NativeWindow-based object that I use. I've placed a button in the Ribbon to be able to dispose the object by hand and it worked perfectly fine. My suspicion is that the garbage collector is not doing its job properly for some reason. Furthermore, even calling ReleaseHandle() on WM_CLOSE or WM_DESTROY does not prevent the crash. Here's the code of my interceptor object:

public class OfficeWindow : NativeWindow, IDisposable
{        
    public OfficeWindow(IntPtr handle)
    {
        this.AssignHandle(handle);            
    }

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        { 
            case (int)WindowMessages.WM_MOVE:
                MessageBox.Show("Move");
                break;
            //other cases
        }

        base.WndProc(ref m);
    }

    #region IDisposable Members

    ~OfficeWindow()
    {
        Dispose(false);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            // Free other state (managed objects).
        }
        ReleaseHandle();
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    #endregion
}

I'm using VS 2008, VSTO 3.0 and the add-in targets Word 2007.

来源:https://stackoverflow.com/questions/7766534/subclassing-ms-words-window-from-a-vsto-add-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!