简体   繁体   English

创建独立控件Direct2D的好策略

[英]Good strategy for creating standalone controls Direct2D

After spending a fair amount of time with Direct2D (1_1), I am looking for information on creating standalone controls with Direct2D, primarily with hit-testing and abstracting layout. 在用Direct2D(1_1)花费了相当长的时间之后,我正在寻找有关使用Direct2D创建独立控件的信息,主要是命中测试和抽象布局。

Creating an entire scene is simple enough (special thanks to Kenny Kerr), however I have not found a good strategy for separating code into reusable UI components. 创建整个场景非常简单(特别要感谢Kenny Kerr),但是我还没有找到将代码分成可重用的UI组件的好策略。

Ultimately I would also like to provide wrappers to these controls that would also be able to be used in WPF. 最终,我还想为这些控件提供包装,这些包装也可以在WPF中使用。 Thanks for your help. 谢谢你的帮助。

[EDIT Feb 19 - 7AM] [EDIT Feb 19-7AM]

Consider this very simple example: The Render (or Draw) function renders a few rounded rect geometries, some of which overlap. 考虑这个非常简单的示例:Render(或Draw)功能可渲染一些圆角的rect几何形状,其中一些重叠。 Now let's treat each of these rounded rects as an independent ui control. 现在,让我们将这些四舍五入的矩形视为独立的ui控件。 So we abstract that rounded rect into its own class, provide a render function which has a parameter of a render target and a point at which to draw. 因此,我们将四舍五入的rect提取到其自己的类中,提供了一个render函数,该函数具有一个render目标的参数和一个绘制点。 But there is a good deal more to consider. 但是还有很多需要考虑的地方。 The control shouldn't necessarily know where it is located. 该控件不必一定知道它的位置。 And a generic hit testing strategy that our system can continue to build on would be important. 我们的系统可以继续建立的通用命中测试策略将很重要。

As @CodeAngry pointed out, properties like visible, enabled, zOrder, width, height, etc should be implemented. 正如@CodeAngry指出的,应该实现诸如visible,enabled,zOrder,width,height等属性。 But the layout system should be our starting point. 但是布局系统应该是我们的出发点。

None of this is incredibly difficult, however I know this stuff is done everyday, and I was hoping to find some pattern or strategy to consider. 这些都不是很难做到的,但是我知道这是每天要做的事情,我希望找到一些可以考虑的模式或策略。 Thanks again. 再次感谢。

The generic hit testing can be done by doing something like this: 通用命中测试可以通过执行以下操作来完成:

  • each control should have a geometry of it's layout. 每个控件应具有其布局的几何形状。 Either a basic rectangle, ellipse, irregular shape. 基本矩形,椭圆形,不规则形状。 Then, the control manager does hit testing against them. 然后,控制管理员确实对他们进行了测试。 Then orders possible matches by zorder 然后按zorder排序可能的匹配项
  • just hit-tests against them in zorder to prevent the sorting step. 只是在zorder中对它们进行命中测试以防止进行排序。

You should store control hierarchy zordered anyways, and this, by itself, skips the sorting. 您应该以任何方式存储按顺序排序的控件层次结构,而这本身会跳过排序。

Controls don't need to know about their location but NEED to know their sizes (if rectangular) and/or shapes. 控件不需要知道它们的位置,而需要知道它们的大小(如果是矩形)和/或形状。 And the manager knows the location and a bounding rectangle. 经理知道位置和边界矩形。 Combine these elements and you can the hit-test. 结合这些要素,即可进行命中测试。 First make sure the point falls in the bounding rectangle and only then hit-test against the control's geometry. 首先确保该点落在边界矩形中,然后才对控件的几何进行命中测试。 Before hit-testing, update the HWND client point to the control client point by subtracting the control client offsets known by the control manager. 在进行命中测试之前,通过减去控制管理器已知的控制客户端偏移,将HWND客户端点更新为控制客户端点。

That's how I'm (planning to) doing it. 这就是我(计划)这样做的方式。 But I'm making it windowsless. 但是我让它没有窗户。 If each control is in a child HWND of its own, life is so much easier! 如果每个控件都位于其自己的子HWND中,那么生活将变得更加轻松!

PS : You can mail me if you want to exchange ideas. PS :如果您想交流想法,可以给我发邮件。 You can find my email if you want to. 您可以根据需要找到我的电子邮件。 ;) ;)

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

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