Create C# Winforms with VC++ OpenGL Scene - c#

Is it possible to render VC++ OpenGL output to a panel in Winforms window created with C# ? I'm aware of Tao framework and OpenTK but I don't want to rely on third party support. There is an article here but this is merely only pixel reproduction and I think this method does not guarantee user interaction with OpenGL scene.

Yes, this is absolutely possible. You have to create the OpenGL context on the HWND that is given by panel.Handle (it's an IntPtr, but can easily be cast to HWND). Be aware, that this is only working in Windows Forms, since in WPF controls do not necessarily have their own HWND. Here is another stackoverflow question, that covers a similar topic.
There are several ways how to access native C++ code from a C# application, so it might be that, depending on your needs, you will have to write a wrapper in C++/CLI around your native C++ library simmilar to (here or here).

Related

Writing .Net window application in C++

I have a task to do: I have to write a graphical (window) application in .Net which is going to read temperature from the three sensors (namely MCP9808, SI7051 and STS31-DIS) connected via MCP2221 I2C/USB converter and display it on the screen.
There are some requirements: The app has to be graphically estetic, it has to have the some buttons (eg for the periodic read of the temperature) and it has to display a chart showing the temperature variations.
I know that .Net usually comes with C# but I've never used it before and also the libraries for the MCP2221 are for C++ as I believe.
Could anyone help me how to get down to this problem?
If you really need to use .NET for your GUI (WPF or WinForms), you should consider .NET interoperability, which allows you to call C++ compiled code from your .NET code.
Here is another StackOverflow post that should help you:
How to call C++ DLL in C#
If you want to use C++ syntax only, there is also C++/CLI.

How can I call C++ OpenGL code inside a C# winform window?

I need to program a windows window with a toolstrip on top, a OpenGL window in the middle and a status line in the bottom:
I am not to thrilled about programming the GUI elements in win32. I have some experience programming winforms in C# and would like to do this.
The problem then is how do I implement the OpenGL window?
As I understand OpenTK comes with a GLControl .net component that I can add to a form.
However I already have the OpenGL code that I am going to use in C++.
I would prefer not having to port this to OpenTK but instead use it directly.
How can I achieve this?
I should also explain that my window is not a main window. It is running inside another program that I am implementing a plug-in for.
The OpenGL context is per-thread state. If you use OpenTK in C# to make a context current, then any C++ code you call from the same thread will automatically use it.
Here is what you need to do:
Isolate your C++ code into a dll.
Create a C# application and create an OpenGL context. Make it the current context.
Call native functions of your C++ code through the C# code using pinvoke. You can expose one or more entry points that can be called by the C# application.
I have never tried this personally but in theory this should work.

Metro App with 2D Graphics in C#

I have done some looking and I can't figure out a good way to write a 2D tile/sprite based game in C# for Win8. According to the MSDN documentation:
You can use managed code languages like C# and Visual Basic to develop 2D (and lightweight 3D) games.
The problem is that I can't figure out how to do this. The reason I would like to use C# is because I already have a lot of code written for Windows Phone that I would like to reuse.
Can anyone point me to how I would write a fairly efficient 2D tile/sprite MetroUI game for Win8? The only thing I can find uses C++ and DirectX.
[EDIT]
To clarify I do not care too much what I use (WinRT, DirectX...) so long as I can submit to the app store, and write my code in C#.
If you are familiar with the DirectX API, you can use SharpDX available via http://sharpdx.org. It is basically exposing DirectX APIs into C#. From my understanding, if you are going to upload apps to the store, the SharpDX should be fine and pass compliance, but I would look into it further before going too deep.
Also, as of this time, not all of the features are available (like Direct2d not fully certified for Win8 Metro).
If your game uses simple graphics, you can use Drawing Library for Modern UI, it exposes a GDI+ like interface to be used in XAML/C#/VB.NET Windows 8 Modern UI applications, it works by drawing to a XAML Canvas.
If you're looking for something quick and easy check out the "Physics Helper XAML" project;
http://physicshelperxaml.codeplex.com/
It's based on the farseer engine, and comes with sample apps. There are a few games already released using it.
http://www.andybeaulieu.com/Home/tabid/67/EntryID/223/Default.aspx
Have a look at:
[XAML images sample] (http://code.msdn.microsoft.com/windowsapps/0f5d56ae-5e57-48e1-9cd9-993115b027b9/sourcecode?fileId=102748&pathId=677969581)
The page and samples give some code for NineGrid and WriteableBitmap Image manipulation.
You can now use Win2D (open source, released by Microsoft for metro/WinRT apps) which provides hardware-accelerated 2D graphics rendering.

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

Embed non-managed directX into C# form

I got a quick question about running a directX application (C++) in a managed environment. I'm attempting to write a MDI tool (in C#) where the background of the parent window would be an embedded render window of directX (C++).
I've read ways that involved writing the C++ into a native dll, however it would be prefered to be able to have the C++ projects included within the solution (I dont even know if that's possible though). Eitherway, if you know of some helpful steps, hints, or if this is a bad idea in general, please let me know. Thanks!
The easiest way to do this would be to add a C++/CLI project to your solution. This would then enable you to use the DirectX COM interfaces directly and create a managed wrapper that's easy to call from your C# code. I've taken this approach a few times and it's by far the easiest way of mixing DirectX and .Net that I've ever tried. Managed DirectX used to be an option, but it's no longer supported and it was a fairly small subset of the full COM API anyway.
First of all writing the C++ part in a different dll file doesn't mean that it couldn't be at the same solution as the C# project.
In order to use native DX to render on a managed window you need to pass the HWND window id (use form.WindowId.ToInt32) to the C++ D3Ddevice c'tor. after that each time you'll render using that device it would render on the .NET window.
To do this you probably need two saparate projects - a C++ dll & .NET project. use COM wrapper or p-invoke to pass the HWND to the C++ dll.
If you don't want to spend too much time on writing C++ code for DirectX, you can consider using
SlimDX, since Managed DirectX 1.0 is out of the question, where as 2.0 never leaves the beta and later replace with XNA which has quite different from DirectX itself, and require you to install XNA Game Studio
SlimDX is the opensource version of managed directx with slightly different API and internal structure, but it's easy to use. The recently released version is very stable. I'm currently using it to write a production application.
SlimDX
Managed DirectX

Categories

Resources