VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之WPF获取窗口句柄

通过WPF的互操作帮助类WindowInteropHelper,相关连接:https://msdn.microsoft.com/zh-cn/library/system.windows.interop.windowinterophelper.aspx

        public MainWindow()
        {
            InitializeComponent();
           
            this.SourceInitialized += MainWindow_SourceInitialized;
        }

        private void MainWindow_SourceInitialized(object sender, System.EventArgs e)
        {
            var handle = (new WindowInteropHelper(this)).Handle;
        }

可以使用HwndSource.AddHook(HwndSourceHook)加一个事件处理程序接收所有窗口消息,相关连接:https://msdn.microsoft.com/zh-cn/library/system.windows.interop.hwndsource.aspx

        private void MainWindow_SourceInitialized(object sender, System.EventArgs e)
        {
            var handle = (new WindowInteropHelper(this)).Handle;
            HwndSource.FromHwnd(handle).AddHook(new HwndSourceHook(WndProc));
        }

        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            throw new NotImplementedException();
        }
 
 
 

相关教程