PresentationSource is such a useful class after digging deeper into it. I’ve use it from time to time and looking at its members, I can see more use of it in the future.

The purpose of PresentationSource is to handle interop scenarios. To begin using this, anytime you want to call into User32.dll, you need a HWND.

Then you need this to get hwnd:
IntPtr hwnd = ((HwndSource)PresentationSource.FromVisual(uielement)).Handle;

You can do a ton of work from a hwnd itself like calling SetWindowLong, SetParent, SetWindowRegion and hundreds more etc..

Next cool function of PresentationSource is to get HwndSource so that you can call AddHook which allows you to subclass by providing your own WndProc.

HwndSource hwndSource = PresentationSource.FromVisual(window) as HwndSource;


Then you can use PresentationSource to determined, if an element is rendered. If so it must be connected to PresentationSource.

PresentationSource.FromVisual(uielement); will return a PresentationSource object if connected, null otherwise.

PresentationSource.FromVisual(uielement).RootVisual is an alternate way to get to your Root UI like a Window without walking up the visual tree.

You can use PresentationSource.AddSourceChangedHandler to determined when the element are disconnected from it’s Parent.

Finally, you can also get CompositionTarget which is another cool class by presentationSource.CompositionTarget. By using CompositionTarget, you can do stuffs like this but don’t over-do it cause it’s expensive in performance. CompositionTarget.Rendering is an alternate way to get high fidelity (animation class) rendering. Here a summary of CompositionTarget.Rendering.

The CompositionTarget.Rendering event is fired when the animation system finishes applying animation related changes. The updated display information is then handed off to the rendering engine, which evaluates how many times per-second it can redisplay based on scene complexity and other factors. Depending on that, it may decide to skip a particular frame produced by the animation system. Putting it another way, the draw rate can easily be different from the rate of the CompositionTarget.Rendering events.