UWP and DirectX - c#

I have a task:
'Second component must be written in C++ and must be a UWP assembly/component that will be used by the First part of the task. It must be a DirectX component that draws to SwapChain with transparent background. The content must be a 2D animation that must be bound to current mouse position. The content must be controllable via set of public methods.
DirectX component must be placed on top of your C# GUI so it will act as overlay.
Make it event-transparent overlay, it must not capture any GUI events and otherwise interfere with C# app.
Use C# host to send mouse events and content change to DX layer.'
But I have no idea how to combine UWP and DirectX.
I already have functioning UWP C# app part and what's next?
I'll be grateful for any responces and ideas.

You need to include a SwapChainPanel or SwapChainBackgroundPanel control into your XAML file. This surface will be used to draw your DirectX content. You have a default uwp sample in Visual C++ Template named DirectX 11 and XAML App. It can help you to understand the plumbing but there is only C++ code on this solution.

MonoGame!
It's a game framework, based on XNA4 that works on UWP, among a myriad of other platforms. You can pick apart the samples to learn what you need.
http://www.monogame.net/

Related

C# .NET - Difference between SendKeys, SendInput, SendMessage, InputInjector and Cursor.Position

I am learning and building my first UWP test app, and need a way to simulate:
relative mouse movement
absolute mouse positioning
keyboard typing (not necessarily key presses/releases)
fine tuned x&y scrolling (so I can scroll by any amount)
I have come across the following methods for doing this, but can't figure out which ones are modern / best for UWP apps or best in general for my purposes:
SendKeys (A C# wrapper for SendInput of some sort?)
SendInput (A win32 API for simulating events, but is it best for UWP?)
SendMessage (Used for directly typing into focused applications?)
InputInjector (A more modern but limited way of simulating inputs, can't absolutely position cursor?)
Cursor.Position (A function for cursor movement and positing)
There are so many methods and approaches to this problem, and I'm not entirely sure which of these is most supported or recommended for UWP apps, or yields the best results.
The purpose of this project is to be able to control my PC (move the mouse, type), by interacting with through my phone. For example my phone becomes a trackpad, or I can type in my phone's soft keyboard and it types into my PC. The PC hosts a server on the local network, and the phone send input data packets to this server. The server receives these input data packets, and executes them (which is where I need the ability to simulate keyboard/mouse events). Very similar to Remote Mouse.
So my questions are:
What are the differences between these methods? (Like Windows Forms or Win32??)
Which is best for UWP apps / my need here?
Are there any better (not listed) solutions?
This is my first look into this stuff (C#, .NET, Windows dev) so any and all information is very helpful.
Thanks for your help!
Dan :D
Edit
Further research has shown that InputInjector is under the UWP reference, SendKeys and Cursor.Position are both under the .NET reference. Does this mean that InputInjector is the most ideal?
After researching some more, I found that InjectedInput is the only one included in the UWP API.
To clarify, when developing a Windows application, in Visual Studio you must select one "type" to use, be it WPF, Windows Forms, Win32 or UWP. UWP is the only one (mostly) that can be uploaded to the Microsoft Store.
This meant that I could only use methods inside the UWP API, in this case WinRT was a part of UWP and InjectedInput is a part of WinRT.
It supports absolute mouse positioning with the "Absolute" option, relative mouse movement with the "Move" option, and scrolling with the "Wheel" and "HWheel" options used in InjectedInputMouseOptions alongside InjectedInputMouseInfo. Keyboard input can be done with InjectedInputKeyOptions alongside InjectedInputKeyboardInfo.
Use the "Option" variant class to modify the effect of the input (such as selecting which options to change), and then use InputInjector with its TryCreate() method to instantiate it, along with the relevant InjectMouseInput or InjectKeyboardInput to execute the input injection.
This sample code alongside its related blog post is fantastic for understanding the basic useage, it jumps straight to the chase.

How to get a screenshot from the background window DirectX

I was looking for examples of how to make a screenshot of an inactive window, but I did not find it. I want to take a screenshot of the game that works in the window, and not in full screen mode.
Which can be blocked by other windows.
I found some examples on the Internet, but could not implement in my project.
Could you set an example of how to do this with the help of EasyHook and SharpDX (or SlimDX) on language C#?
I'll be very grateful!
I created an example project for this on GitHub, https://github.com/spazzarama/Direct3DHook
Allows screen capture and drawing an overlay into the Direct3D target application using C#, EasyHook and SharpDX.
Keep in mind that some games will employ anti-injection/hooking techniqiues that may block EasyHook.
In addition this usually won’t work for minimised applications as it is best practice to pause your render loop when minimised.

3D container and viewer control for WPF

Recently I have been working on a small WPF project involving the visualization and manipulation of 3d objects. For 2d ojects, this can be easily achieved with a Canvas warpped in a ViewBox or some nice 3rd party controls. But I can't find any free, ready-to-use solution for 3d objects. There is a likely one in WpfToolKit from XceedSoftware(https://github.com/xceedsoftware/wpftoolkit), but no information is given.
I know WPF has some 3D support, like ViewPort3D and other classes in Media3D namespace, and I did find a book, '3D Programming for Windows'(http://www.charlespetzold.com/3D/index.html), by Charles Petzold introducing the concept and basic knowledge. However, many tricky tasks like adjusting camera according to mouse and keyboard input is not presented. So before I start building my own control, I want to at least confirm that I'm not wasting my time doing something already perfectly done. And how to properly config the mouse and keyboard events to make the control behaves as if using some professional CAD softwares?
In short:
1.Any free 3d control for WPF
2.How to build my own ones
Can anyone help me?

Changing display colors in UWP

Is there a way to apply canvas effects listed in following MSDN page to all display?
http://microsoft.github.io/Win2D/html/N_Microsoft_Graphics_Canvas_Effects.htm
So, can the input and output of ,e.g., TemperatureAndTintEffect be the display buffer?
robertos is correct. Win2D is a UWP API, and there is no general purpose way for UWP apps to read or write to the display outside of their specific app window.
On mobile devices you can use ScreenCapture APIs to read the displayed image, but there is no way to modify that or replace it with something else. Or if you want to apply filters only within your own XAML app, you can do that by rendering the XAML visual tree to a bitmap. More about both options in this thread: Universal Windows Library (UWP) Way to take screenshot?
No. UWP apps do not have access to the raw graphics card and buffers outside of their app.

Hook/Overlay a DirectX game?

Can anyone tell me how to hook/overlay a DirectX game in C#?
I've tried getting a fullscreen C# window to overlap a game, however it wont.
After researching a little, I found out that I need to hook the game and then display the C# window.
Can anyone explain how I would do this?
Would I be able to display a C# form over a DirectX game?
You can try my example on hooking the Direct3D 9 API using C#. This utilizes EasyHook an open source .NET assembly that allows you to install hooks from managed code into unmanaged functions.
SlimDX is also used - this is an open source managed wrapper around the Direct3D libraries.
The tricky part of the hooking is determining the addresses of a COM objects' virtual functions. This is done in the above example using a small C++ helper DLL that finds the addresses from the VTable. [Update: there is a comment posted that show's how to get the function pointers from the VTable in C# also - given a IntPtr to the com object]
The example hooks the EndScene method of an IDirect3DDevice9, which is also where you would want to draw any overlays. As to displaying a form in the overlay I'm not so sure that will be so easy - I mean you can render the image easily enough, but you will have to capture inputs and manually respond/pass-thru the events to the form in question appropriately. Good luck!
(disclosure: I work for this company)
You can try our Deviare API, it has functionality to hook COM objects from C#.
It's a set of COM objects that can be used from any programming language (supporting COM). An article with source code showing how to capture video and add an overlay is available: instrumenting Direct3D applications to capture video and calculate FPS
COM Spy Console and Direct Sound Capture are two examples that implements a console to hook COM objects like DirectX.
Hope it helps.
Since you don't have direct DirectX Access from C# this may be tricky.
From my limited understanding of the concept:
DirectX Hooks consist of attaching to a DirectX Context and manipulating it, this may/may not require messing with another program's memory, something that's best suited to C/C++
I may be wrong though, however this is an advanced topic and my gut tells me you may have issues under .NET

Categories

Resources