简体   繁体   中英

How to create C# custom controls from c/cpp

Can somebody shed some light on how to create c# custom controls from vc? I need to create some graphical controls which will be used in a C# project like the default controls, because performance is important and it's on a low performance windows CE device, I guess I have to do it in c/cpp.

The simple, straightforward answer is that you can't. Period. The Compact Framework doesn't support managed C++ (C++/CLI). Now I suppose you could create the UI component as a COM control, then hand-roll all of the COM interface stuff for the managed side, but that would be a nightmare to build, debug and especially deploy.

You're better off just dropping to unsafe code and using P/Invokes to the Win32 APIs you're after right in the C# code. For most things, you can achieve what you want and get good perf. If you have something that absolutely must be done in C for speed, then create a library that you pass in buffers to via P/Invoke. The Imaging library, for example, uses this type of mechanism for creating thumbnails, etc.

You can try to write a complete control in C and provide a very thin wrapper exposing what you need to control from say C#. It doesnt necessarily need to follow UserControl or Control's model if we are talking about Forms controls, which is somewhat complicated to get right, and where all the cost goes for those controls. Its very harsh.

You can also investigate WPF, which tries to get more done on the managed side, thus faster. It has its own complications. It will composite / "bitblit" usually fairly inteligently on its own, but goes overboard often, but its way faster (or can be) than a double buffer Forms control, which in the end will be doing the same composite in double buffer mode, but slower with a larger flurry of faux win message handling. (To grossly oversimplify)

So rather than just saying NO, i think those are your options. Again, it is possible to control at arms-length, a well-written C side render and get near native performance.

Edit:

I missed the Windows CE part of the question. My bad. I dont know if what I said will apply.

I assume you're talking about WinForms and not Silverlight.

You don't need to.

.NET on Windows CE performs JIT of CIL (unlike the Micro Framework which interprets it, like old-school Java). There is no real performance penalty of writing controls in C#. The most expensive operation is painting, and if you use C# or C++ then you'd be doing this with GDI, and it's inside GDI's function calls where the expensive operations lie, the only thing you'd be saving is the marshalling between Managed and Native territories, but that really does count for very little.

The only situation where you might want to use C++ to create a Windows CE GUI was if you were working with video or an animation framework (like Flash), and if you were doing that then you'd use C++ entirely and not use .NET for any of your GUI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM