question

Robinson-7738 avatar image
0 Votes"
Robinson-7738 asked Robinson-7738 answered

Parent a user control inside a Windows Form (with one eye on Win32 window!)

I have a service page (actually a Win32 tab control panel) that I'd like to embed a C# .NET user control inside. The service manager app is Win32. As a test I thought I'd try parenting an instance of the user control to a Windows Form, not by adding it to the controls collection of the parent form but by using Win32's SetParent. When I do this however, my user control doesn't show on the form.

With:

 [DllImport("user32.dll")]
 public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

I write:

   if (My_User_Control_Instance == null)
   {
         My_User_Control_Instance = new My_New_User_Control();                
   }
    
   My_User_Control_Instance .Bounds = position;
    
   var result = SetParent(My_User_Control_Instance.Handle, hWnd);

where hWnd is an IntPtr acquired from the main forms .Handle.

With one eye on the eventual integration of this little applet with the Win32 application, what is the best way for me to do this? Or is the whole scheme misconcieved.






dotnet-csharpwindows-apiwindows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If your ultimate objective is to have parent/child windows between two different applications (i.e., cross-process) I suggest you consider the discussion here - Is it legal to have a cross-process parent/child or owner/owned window relationship?


0 Votes 0 ·
DrakeWu-MSFT avatar image
0 Votes"
DrakeWu-MSFT answered

Hi,@Robinson-7738 I can’t reproduce your problem with the UserControl example here.
Result:
60805-1.png
Are the two windows you want to set in the same DPI awareness modes? According to SetParent:

Unexpected behavior or errors may occur if hWndNewParent and hWndChild are running in different DPI awareness modes.

Does SetParent return successfully? If it fails, what error code does GetLastError return?

If the above situations still do not help you solve the problem, please provide a minimal sample that can reproduce the problem so that we can help you investigate this problem in depth.


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1.png (35.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Robinson-7738 avatar image
0 Votes"
Robinson-7738 answered

Thanks for your reply. SetParent returns a handle (not null) so it isn't failing. The main difference I suppose is my user control is in a c# .dll project and the form project is referencing that dll, so I presume therefore they both have the same DPI awareness (the awareness of the parent project).

Given this I think I'll do a repro. If it works the question will remain, why does my test project not work.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Robinson-7738 avatar image
0 Votes"
Robinson-7738 answered

Just to confirm, as your experience suggested, the simple repro (all in one project, one form, one user control), worked fine. I suppose that narrows the problem down somewhat.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Robinson-7738 avatar image
0 Votes"
Robinson-7738 answered

Aha! I finally understand my error. The user controls were missing an InitializeComponent() (they have somewhat different constructors). As so often happens, asking the question causes a closer look at the obvious. Thanks.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.