2009-07-08 12 views

risposta

7

non SetParent è considerato "più corretta" rispetto SetWindowLong con GWL_HWDPARENT (-8)?

[DllImport("user32.dll", SetLastError = true)] 
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 
+0

Questo ha funzionato perfettamente per me impostando la finestra WPF come genitore della finestra di dialogo Win32. – SliverNinja

12

Utilizzare questo metodo:

[DllImport("user32.dll")] 

private static extern int SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong); 

/// <summary> 
/// sets the owner of a System.Windows.Forms.Form to a System.Windows.Window 
/// </summary> 
/// <param name="form"></param> 
/// <param name="owner"></param> 
public static void SetOwner(System.Windows.Forms.Form form, System.Windows.Window owner) 
{ 
    WindowInteropHelper helper = new WindowInteropHelper(owner); 
    SetWindowLong(new HandleRef(form, form.Handle), -8, helper.Handle.ToInt32()); 
} 
+4

Nota Per scrivere codice compatibile con le versioni di Windows a 32 e 64 bit, utilizzare SetWindowLongPtr. Durante la compilazione per Windows a 32 bit, SetWindowLongPtr viene definito come una chiamata alla funzione SetWindowLong. – Dave

Problemi correlati