поставь wpf на win hdl - PullRequest
       23

поставь wpf на win hdl

1 голос
/ 29 октября 2009

я получаю winHdl (который является winForm) из нативного приложения на c ++.

Реализация плагина в c #:

public int Create(int hParentWnd ...){ ... // here i want to put wpf on this hParentWnd }

Теперь я хочу поместить свой пользовательский элемент управления WPF в это окно. Как я могу это сделать? Доступны ли фрагменты кода?

спасибо

Ответы [ 3 ]

1 голос
/ 30 октября 2009

Вам не нужны WinForms для этого. Вы можете использовать HwndSource напрямую:

  public void CreateControl(IntPtr hParentWnd)
  {
    _userControl = new MyWPFUserControl();

    var parameters = 
      new HwndSourceParameters("", _initialWidth, _initialHeight)
      {
        ParentWindow = (IntPtr)hwndParent,
        WindowStyle = ...,          // Win32 style bits
        ExtendedWindowStyle = ...,  // Win32 ex style bits
      })

    _hwndSource = 
      new HwndSource(parameters) { RootVisual = _userControl };
  }

  public void DestroyControl()
  {
    _hwndSource.Destroy();
  }

Параметр CreateControl действительно должен быть IntPtr, а не 'int', но вы можете установить его в 'int', если вам нужно, и позже просто привести его к IntPtr.

Если вы хотите вернуть дескриптор вновь созданного окна, верните _hwndSource.Handle.

0 голосов
/ 10 ноября 2009

Хорошо, я понял это. Вот результат:

                int WS_CHILD = 0x40000000;
                int WS_VISIBLE = 0x10000000;

                // paint wpf on int hParentWnd
                myWindow = new MyWpfWindow();

                // First it creates an HwndSource, whose parameters are similar to CreateWindow:
                HwndSource otxHwndSource = new HwndSource(
                                        0,                          // class style
                                        WS_VISIBLE | WS_CHILD,      // style
                                        0,                          // exstyle
                                        10, 10, 200, 400,
                                        "MyWpfWindow",             // NAME
                                        (IntPtr)hParentWnd          // parent window
                                        );

                otxHwndSource.RootVisual = ...;

                // following must be set to prcess key events
                myHwndSource.AddHook(new HwndSourceHook(ChildHwndSourceHook));

с крючком:

private IntPtr ChildHwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == (int)(0x0087)) //WM_GETDLGCODE
            {
                handled = true;
                int returnCode = (int)(0x0080);
                return new IntPtr(returnCode);
            }
            return System.IntPtr.Zero;
        }

благодаря Рэй Бернс!

0 голосов
/ 29 октября 2009

ElementHost - это то, что вы ищете, это элемент управления WinForms, который содержит содержимое WPF.

http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.elementhost.aspx

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...