我需要使用 activeX 为遗留应用程序创建插件。我正在使用 C#,以便我可以共享几个相关独立产品的代码库。
当我在 SDK 的测试容器 (TstCon32.exe) 中托管控件时,一切正常。问题是,当我第一次尝试在目标应用程序中加载它时,如果我更改背景颜色控制其他东西背景保持我指定的相同颜色,但所有控件仍被控制灰色覆盖(来自容器应用程序?)。单击其他控件将导致它们绘制,调整控件大小将导致所有内容重新绘制。在初始消隐后,控制功能正常,直到下一次创建副本。
我终于找到了解决方案。第一部分是将我的目标框架从 4.0 更改为以前的版本。这揭示了当控件被消隐时发生的异常,该控件以前被消耗并隐藏在我面前:
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Windows.Forms.UnsafeNativeMethods.IOleInPlaceSite.OnUIActivate()
at System.Windows.Forms.Control.ActiveXImpl.InPlaceActivate(Int32 verb)
at System.Windows.Forms.Control.ActiveXImpl.OnFocus(Boolean focus)
at System.Windows.Forms.Control.ChildGotFocus(Control child)
at System.Windows.Forms.Control.OnGotFocus(EventArgs e)
at System.Windows.Forms.Control.WmSetFocus(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
从那里我可以找到 tomasvdb 的帖子,他遇到了与 Google 边栏相同的问题。
对链接到此消息的帖子的回复之一为侧边栏应用程序向 .net 控件发送无效数据的错误提供了解决方法。
在控件的事件处理程序中调用该解决方法VisibleChanged
也解决了我的问题。