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

Robinson 156 Reputation points
2021-01-26T22:21:16.547+00:00

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.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,422 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
{count} votes

Accepted answer
  1. Drake Wu - MSFT 991 Reputation points
    2021-01-27T02:52:19.623+00:00

    Hi,@Robinson 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.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Robinson 156 Reputation points
    2021-01-27T14:39:14.13+00:00

    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.

    0 comments No comments

  2. Robinson 156 Reputation points
    2021-01-27T15:08:03.08+00:00

    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.

    0 comments No comments

  3. Robinson 156 Reputation points
    2021-01-27T15:45:01.483+00:00

    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.

    0 comments No comments