1、在WPF中,獲取當前窗體的句柄與WINFORM中不一樣:
WINFORM直接獲取:this.Handle----------this是窗體的類名,handle就是句柄。
2、WPF中先引用命名空間:System.Windows.Interop;
IntPtr hwnd = new WindowInteropHelper(this).Handle;----this就是要獲取句柄的窗體的類名;
3、控件句柄,同樣引用 :System.Windows.Interop:
IntPtr hwnd = ((HwndSource)PresentationSource.FromVisual(uielement)).Handle;----uielement就是要獲取句柄的控件,該控件必須繼承自UIElement。
4、進程模塊句柄
引用 System.Runtime.InteropServices、System.Diagnostics 命名空間
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern IntPtr GetModuleHandle(string lpModuleName);
調用獲得進程模塊的句柄:
IntPtr hwnd = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);