2015-02-12 3 views
3

Я хочу открыть приложение в своем окне. мой код отлично работает в форме Windows, но в wpf он не работает. Вот код с описаниемкак взять ручку окна в wpf

private Process pDocked; 
    private IntPtr hWndOriginalParent; 
    private IntPtr hWndDocked; 

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

    [DllImport("user32.dll", SetLastError = true)] 
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); 




    private void panel1_Resize(object sender, EventArgs e) 
    { 
     //Change the docked windows size to match its parent's size. 
     MoveWindow(hWndDocked, 0, 0, panel1.Width, panel1.Height, true); 
    } 


    private void dockIt(string utility) 
    { 
     if (hWndDocked != IntPtr.Zero) //don't do anything if there's already a window docked. 
      return; 
     //hWndParent = IntPtr.Zero; 

     pDocked = Process.Start(utility); 
     while (hWndDocked == IntPtr.Zero) 
     { 
      pDocked.WaitForInputIdle(1000); //wait for the window to be ready for input; 
      pDocked.Refresh();    //update process info 
      if (pDocked.HasExited) 
      { 
       return; //abort if the process finished before we got a handle. 
      } 
      hWndDocked = pDocked.MainWindowHandle; //cache the window handle 
     } 
     //Windows API call to change the parent of the target window. 
     //It returns the hWnd of the window's parent prior to this call. 
     hWndOriginalParent = SetParent(hWndDocked, panel1.Handle); 

     //Wire up the event to keep the window sized to match the control 
     panel1.SizeChanged += new EventHandler(panel1_Resize); 
     //Perform an initial call to set the size. 
     panel1_Resize(new Object(), new EventArgs()); 
    } 

И на кнопку мыши я передаю блокнот в DocKIT, но он не работает, как я заставить его работать в WPF, любой панели (DockPanel, StackPanel), но они не поддерживают изменение размера окна

+0

после использования интеграции форм окон он открывает утилиту, но это не видно –

+0

, и я хочу открыть его в tabitem –

+0

Можете ли вы расширить это немного? Объясните свои намерения, то, что вы ожидаете, и то, что вы на самом деле наблюдаете (как в выигрышных формах, так и в wpf). – Rikalous

ответ

2

Вы можете разместить блокнот в своем окне WPF следующим образом: добавьте WindowsFormsHost в свое окно, установите System.Windows.Forms.Panel как дочерний элемент и используйте дескриптор панели.

public partial class MainWindow : Window 
{ 
    [DllImport("user32.dll")] 
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 

    [DllImport("user32.dll", SetLastError = true)] 
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); 

    private Process pDocked; 
    private IntPtr hWndOriginalParent; 
    private IntPtr hWndDocked; 
    public System.Windows.Forms.Panel pannel; 

    public MainWindow() 
    { 
     InitializeComponent(); 
     pannel = new System.Windows.Forms.Panel(); 
     host.Child = pannel; 
     dockIt("notepad.exe"); 
    } 

    private void dockIt(string utility) 
    { 
     if (hWndDocked != IntPtr.Zero) //don't do anything if there's already a window docked. 
      return; 

     pDocked = Process.Start(utility); 
     while (hWndDocked == IntPtr.Zero) 
     { 
      pDocked.WaitForInputIdle(1000); //wait for the window to be ready for input; 
      pDocked.Refresh();    //update process info 
      if (pDocked.HasExited) 
      { 
       return; //abort if the process finished before we got a handle. 
      } 
      hWndDocked = pDocked.MainWindowHandle; //cache the window handle 
     } 
     //Windows API call to change the parent of the target window. 
     //It returns the hWnd of the window's parent prior to this call. 
     hWndOriginalParent = SetParent(hWndDocked, pannel.Handle); 

     //Wire up the event to keep the window sized to match the control 
     SizeChanged += window_SizeChanged; 
     //Perform an initial call to set the size. 
     AlignToPannel(); 
    } 

    private void AlignToPannel() 
    { 
     MoveWindow(hWndDocked, 0, 0, pannel.Width, pannel.Height, true); 
    } 

    void window_SizeChanged(object sender, SizeChangedEventArgs e) 
    { 
     AlignToPannel(); 
    } 
} 

окно XAML:

<TabControl> 
    <TabItem Header="Notepad"> 
     <WindowsFormsHost x:Name="host" /> 
    </TabItem> 
</TabControl> 

Но как только вы кладете WindowsFormsHost на вкладке будет много WinForms/WPF вопросы интеграции. Лучший источник знаний по этой теме Я нашел в этой статье "Mitigating Airspace Issues In WPF Applications". Удачи.

+0

это прекрасно работает, но уже объясняет, что это возможно для управления вкладками, или вы можете открыть его как tabitem –

+0

@Anoop Mishra, проверьте обновление – igorushi

+0

спасибо, что ответ на него сейчас работает ........ –

Смежные вопросы