Repainting problem with MDI child created by SetParent API

1.9k Views Asked by At

I have a legacy app written in C that consists of a main window and several mdi children opened from menu options. To allow new mdi children to be written in C# I have created a C++ COM interop layer that is called by the C code and in turn calls the C# code. I then use the SetParent API to set the C main window as the new parent of any C# window opened. This seems to work - the C# window behaves like an MDI child of the main window. BUT, the child window does not paint properly and only gets worse if you move other windows over it or move it to the edges of the main window - it gets painted with parts of the other windows or leaves bits of itself lying around as it moves. In addition the screen doesn't respond very well eg you cannot tab from one textbox to another.

Please don't question the architecture of my solution (believe me, this is the only way), but if you've ever seen a problem like this with a child created by SetParent I'd love to hear if you managed to fix it.

2

There are 2 best solutions below

1
On

Try these things:

  1. Add Application.DoEvents inside a processing loop that is normally running when things go bad.

  2. Try refreshing the MDI forms from within the main form's paint event.

1
On

I don't totally understand how SetParent() works; that being said, here's some more things to consider:


In the MSDN community content of the SetParent documentation, Chango V. from Microsoft added that you: "need to call SetWindowPos(SWP_FRAMECHANGED) when changing between null and non-null parent."


Also, are you sure you are actually running the .NET Form message loop? Did you call Application.Run(yourManagedForm), or are you running your own message loop in the C code? If you're running your own message loop, you may need to forward messages to the WndProc method on your managed form after filtering it through PreProcessMessage. You would need to expose an interface to these as they are protected. I don't know how valid this is, though.