在 WPF 中获取窗体或控件句柄

引入命名空间:

using System.Windows.Interop;

窗体:

IntPtr hwnd = new WindowInteropHelper(this).Handle;

控件:

IntPtr hwnd = ((HwndSource)PresentationSource.FromVisual(uielement)).Handle;