Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6048837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:30:30+00:00 2026-05-23T07:30:30+00:00

In my WPF application, I host Win32 content using HwndHost. However, creating a HwndHost

  • 0

In my WPF application, I host Win32 content using HwndHost. However, creating a HwndHost does not create the native window. Rather, this is done in the overridden BuildWindowCore() method which is called sometime later by WPF.

My hosted content needs the window handle of the native window for its own initialization. Unfortunately, there is no way I can force the creation of the window (i.e. having WPF call the BuildWindowCore), so I have a second thread which polls the HwndHost until it has been initialized.

In .NET 4.0 / WPF 4.0, a new method WindowInteropHelper.EnsureHandle() was added. I had hoped this would resolve the situation, but it only works for a Window, not a HwndHost (which doesn’t derive from Window). Do you have a suggestion what I could do instead?

EDIT:

I forgot to add some more constraints for a possible solution:

  1. The HwndHost is placed in a control which, depending on user settings, can be a child of the application’s main window or can be placed in a new Window (via a third-party docking manager). This means that during creation of the window I do not know for sure what the parent Window (and thus its hWnd) will be.
  2. While the native code needs the hWnd during its initialization, the window is only displayed when the user requests it to be shown (i.e. it is invisible at first). Needing to show the window, only to hide it immediately again, should be avoided, if possible.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:30:31+00:00Added an answer on May 23, 2026 at 7:30 am

    There seems to be no perfect solution. I changed my approach slightly compared to the time of the question being asked:

    In the constructor of my HwndHost-derived class I have the (possible) parent hWnd as one of the parameters. I then create the native window using the native CreateWindow() method, using the given parent hWnd. I store the created hWnd in a separate property, which I use everywhere instead of the HwndHost’s Handle property. That way, I don’t need to show the window (this solves constraint #2).

    In the overridden BuildWindowCore() method, I compare the given parent hWnd with the one I was given in the constructor. If they are different, I reparent my hosted window using the native SetParent() method (this solves constraint #1). Note that this relies on nobody storing the parent hWnd!

    In code, the relevant parts (checks omitted):

    public class Win32Window : HwndHost
    {
        public Win32Window(IntPtr parentHwnd)
        {
            this.ParentHwnd = parentHwnd;
            this.Win32Handle = NativeMethods.CreateWindowEx( /* parameters omitted*/ );
        }
    
        public IntPtr Win32Handle { get; private set; }
        private IntPtr ParentHwnd { get; set; }
    
        protected override HandleRef BuildWindowCore(HandleRef hwndParent)
        {
            if (hwndParent.Handle != this.ParentHwnd)
            {
                NativeMethods.SetParent(this.Win32Handle, hwndParent.Handle);
            }
    
            return new HandleRef(this, this.Win32Handle);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a WPF Application using VS 2005. I download the add on
Possible Duplicate: How can one host Flash content in a WPF application and use
I am trying to create a parent WPF/surface application which will host multiple WPF/surface
Can I host an XBAP in my WPF application dynamically? Can I download an
I'm working with a Win32 application that needs to create a variety of custom
I'm trying to host the Workflow Designer in a WPF application. The WorkflowView control
I have a WPF application using MVVM. I have some user controls that show
I have a WPF application embedded in a Microsoft Outlook 2003 window. Everything is
I have a WPF application where I'd like to create a custom pop-up that
I have a simple WPF application I'm using for experimenting. I have two themes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.