CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2011
    Posts
    4

    direct2d in C++ but form in c#

    Hi. I have both Visual studio C# and C++ Express editions. I have been experimenting with direct2d. I can create forms in C# but can only write direct2d rendering code in c++. C# naturally lends itself to creating forms easily. But direct2d bindings (unless I am mistaken) are only available in c++. If I make a form in c# with an event handler for the paint scenario, and I write the direct2d renderer in c++, how do I get a handle to the window frame I wrote in c# to c++ so that the renderer in c++ can render to teh window I created in c#?

    I guess I should be asking a more fundamental question. What is the orthodox (i.e. pro way) to do this? I have no idea how to do this. Writing the skeleton code (message loop, dispatcher, etc) in c++ is a major pain. Right now I am thinking of sharing a dll, where the dll contains the binaries for the renderer in c++ and the C# app gives a hWnd reference to the dll. Another way is to create a socket between the c++ and c# processes and share the hWnd (but something tells me this is not the orthodox way).

    Essentially I know how to make forms in C# and direct2d renderers in c++, but dont know how to take advantage of both put together.

  2. #2
    Join Date
    Apr 2012
    Posts
    29

    Re: direct2d in C++ but form in c#

    I have been experimenting with direct2d. I can create forms in C# but can only write direct2d rendering code in c++. C# naturally lends itself to creating forms easily. But direct2d bindings (unless I am mistaken) are only available in c++.
    Another way is to create a socket between the c++ and c# processes and share the hWnd (but something tells me this is not the orthodox way).
    I am messed up just to read your ideas. . I would prefer the C++ dll that I can call its exported methods from C#. When it comes to networking, what language is used to implement the client or server is no longer an issue provided that I can establish a communication channel for the two applications to talk to each other; that is, I can have a server written either in C# or C++ while my client is written in pure C.

  3. #3
    Join Date
    Jan 2010
    Posts
    1,133

    Re: direct2d in C++ but form in c#

    Write code in C++, write a .NET wrapper in C++/CLI, compile to a .NET DLL, then use from C# as you would normally use any other .NET library. Window handles are passed around as .NET IntPtr structures; you can obtain one from, say, a Windows Form window via the Handle property, and then pass that to the unmanaged code.
    See this MSDN entry, and, for example, this article series.

    BTW, you can use the older Managed DirectX (but I don't think it has Direct2D) or the newer XNA framework (probably has a way of working with Direct2D, but not sure) in .NET, and I just stumbled upon these two APIs: SlimDX, and SharpDX; both open source and both support Direct2D. Can't vouch for quality though - didn't try either.
    Last edited by TheGreatCthulhu; July 26th, 2012 at 08:19 PM.

  4. #4
    Join Date
    Jul 2012
    Posts
    3

    Re: direct2d in C++ but form in c#

    If your looking at just trying to render to a windows form, then the OpenTK library for C# (which is a OpenGL wrapper) could be extremely useful to you, as it can render in both 2D and 3D and it also integrates extremely well with windows forms. It even has a control you can drag directly onto your form. OpenTK also comes with a build-in 3D math library that takes away the need to make your own or find one. The only disadvantage of using it is that you'll have to learn OpenGL (which is actually really easy ). I don't know if this answered your question but it's a great alternative and OpenGL is also multi-platform in case you ever want to develop for mulitple platforms.

  5. #5
    Join Date
    Feb 2002
    Posts
    4,640

    Re: direct2d in C++ but form in c#

    Quote Originally Posted by xDarkShadowx View Post
    If your looking at just trying to render to a windows form, then the OpenTK library for C# (which is a OpenGL wrapper) could be extremely useful to you, as it can render in both 2D and 3D and it also integrates extremely well with windows forms. It even has a control you can drag directly onto your form. OpenTK also comes with a build-in 3D math library that takes away the need to make your own or find one. The only disadvantage of using it is that you'll have to learn OpenGL (which is actually really easy ). I don't know if this answered your question but it's a great alternative and OpenGL is also multi-platform in case you ever want to develop for mulitple platforms.
    Nice, but doesn't C# only run on one platform?

    Viggy

  6. #6
    Join Date
    Jul 2012
    Posts
    3

    Re: direct2d in C++ but form in c#

    @MrViggy

    The .NET framework indeed only runs on one platform but you could compile your C# code for multiple platforms via the mono framework but idk if OpenTK can be used with it. It's a good start when trying to learn OpenGL though, but if your looking into actual cross-platform graphics development then I would suggest using freeglut which allows you to easily open windows on multiple platforms and also has several event handlers that you can assign methods to. You can also use straight c++ and OpenGL in it .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured