WPF Application Post Connector and PostThreadMessage

In the WPF application, inside there is a classic message loop (in the sense of Windows GetMessage/DispatchMessage ), inside Application.Run ? Is it possible to catch a message sent from another Win32 application with PostThreadMessage into a WPF user interface thread (message without an HWND descriptor). Thanks.

+6
source share
1 answer

I used .NET Reflector to track Applicaton.Run implementation before Dispatcher.PushFrameImpl . You can also get the same information from . NET Framework data sources . There is a truly classic message loop:

 private void PushFrameImpl(DispatcherFrame frame) { SynchronizationContext syncContext = null; SynchronizationContext current = null; MSG msg = new MSG(); this._frameDepth++; try { current = SynchronizationContext.Current; syncContext = new DispatcherSynchronizationContext(this); SynchronizationContext.SetSynchronizationContext(syncContext); try { while (frame.Continue) { if (!this.GetMessage(ref msg, IntPtr.Zero, 0, 0)) { break; } this.TranslateAndDispatchMessage(ref msg); } if ((this._frameDepth == 1) && this._hasShutdownStarted) { this.ShutdownImpl(); } } finally { SynchronizationContext.SetSynchronizationContext(current); } } finally { this._frameDepth--; if (this._frameDepth == 0) { this._exitAllFrames = false; } } } 

Next, here is the implementation of TranslateAndDispatchMessage that actually fires the ComponentDispatcher.ThreadFilterMessage event as it executes inside RaiseThreadMessage :

 private void TranslateAndDispatchMessage(ref MSG msg) { if (!ComponentDispatcher.RaiseThreadMessage(ref msg)) { UnsafeNativeMethods.TranslateMessage(ref msg); UnsafeNativeMethods.DispatchMessage(ref msg); } } 

It seems to work for any published message, not just the keyboard. You should be able to subscribe to ComponentDispatcher.ThreadFilterMessage and keep track of your post of interest.

+3
source

Source: https://habr.com/ru/post/951541/


All Articles