简体   繁体   English

如何从c / cpp创建C#自定义控件

[英]How to create C# custom controls from c/cpp

Can somebody shed some light on how to create c# custom controls from vc? 有人可以说明如何从vc创建c#自定义控件吗? 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. 我需要创建一些图形控件,这些控件将在C#项目中使用,就像默认控件一样,因为性能很重要而且它位于低性能的Windows CE设备上,我想我必须在c / cpp中完成。

The simple, straightforward answer is that you can't. 简单明了的答案就是你做不到。 Period. 期。 The Compact Framework doesn't support managed C++ (C++/CLI). Compact Framework不支持托管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. 现在我假设您可以将UI组件创建为COM控件,然后为管理端手动滚动所有COM接口,但这将是构建,调试和特别部署的噩梦。

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. 你最好只是放弃不安全的代码并使用P / Invokes到你在C#代码后面的Win32 API。 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. 如果你有一些绝对必须在C中完成的速度,那么创建一个你通过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#. 您可以尝试在C中编写一个完整的控件,并提供一个非常薄的包装器,从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. 如果我们讨论的是Forms控件,它并不一定需要遵循UserControl或Control的模型,这对于正确的操作来说有点复杂,并且所有成本都用于这些控件。 Its very harsh. 它非常苛刻。

You can also investigate WPF, which tries to get more done on the managed side, thus faster. 您还可以调查WPF,它试图在托管端完成更多工作,因此更快。 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. 它通常会相当集中地复合/“bitblit”,但经常过分,但它的方式比双缓冲区Forms控件更快(或可能),最终它将在双缓冲模式下执行相同的复合,但随着更大的虚假胜利消息处理速度变慢。 (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. 同样,可以控制臂长,精心编写的C侧渲染并获得接近原生的性能。

Edit: 编辑:

I missed the Windows CE part of the question. 我错过了Windows CE部分的问题。 My bad. 我的错。 I dont know if what I said will apply. 我不知道我说的是否适用。

I assume you're talking about WinForms and not Silverlight. 我假设你在谈论WinForms而不是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). Windows CE上的.NET执行CIL的JIT(与解释它的Micro Framework不同,就像老式Java一样)。 There is no real performance penalty of writing controls in C#. 在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. 最昂贵的操作是绘画,如果你使用C#或C ++,那么你将使用GDI进行此操作,并且它在GDI的函数调用中,昂贵的操作位于其中,你唯一能节省的是Managed和Eaged之间的编组。原住民的领土,但这确实很少。

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. 您可能希望使用C ++创建Windows CE GUI的唯一情况是,如果您正在使用视频或动画框架(如Flash),如果您这样做,那么您将完全使用C ++而不使用.NET对于任何GUI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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