简体   繁体   English

使用 Direct2D 和 DirectWrite(C++、DirectX)制作按钮

[英]Producing buttons with Direct2D and DirectWrite (C++, DirectX)

I've been looking around for awhile about how to produce buttons using Direct2D and DirectWrite with no luck.我一直在寻找有关如何使用 Direct2D 和 DirectWrite 生成按钮的方法,但没有成功。 I'm comfortable with shapes, text and that jazz.我对形状、文字和爵士乐感到满意。 However, it suddenly occurred to me I might be looking about it in the wrong way.然而,我突然想到我可能以错误的方式看待它。

Take the sentence:取一句话:

you draw your controls and content for your app using the Direct2D and DirectWrite APIs, handling all the input events directly.您使用 Direct2D 和 DirectWrite API 为您的应用绘制控件和内容,直接处理所有输入事件。

I'm now thinking this means that instead of being able to quickly produce a fully functional button as I would using XAML.我现在认为这意味着我不能像使用 XAML 那样快速生成功能齐全的按钮。 I would draw the button, manually check the location of the mouse on click, whether it's within the button boundaries and then handle the event?我会绘制按钮,手动检查鼠标点击的位置,是否在按钮边界内,然后处理事件? Similar method for hovering without the click.无需单击即可悬停的类似方法。

Is this the kind of method required when using Direct2D and DirectWrite?使用 Direct2D 和 DirectWrite 时需要这种方法吗?

Yes, to draw a UI Button with Direct2D, you need to handle everything yourself, why? 是的,要使用Direct2D绘制UI按钮,您需要自己处理所有事情,为什么? Direct2D is a 2D graphics API, not controls library. Direct2D是2D图形API,不是控件库。 you need to draw the layout of your button, and handle the message of your button(such as click, mouse hover...), you lost lots of convenient and that's time-consuming, but the most important thing is: you can control it by yourself! 您需要绘制按钮的布局并处理按钮的消息(例如单击,鼠标悬停...),您失去了很多便利,这很耗时,但是最重要的是:您可以控制自己动手!

I haven't any experience with DirectX, but in OpenGL I build my buttons from scratch. 我没有使用DirectX的经验,但是在OpenGL中,我是从头开始构建按钮的。 Assuming you have animated sprites implemented, your buttons are essentially sprites that play certain animations in response to being clicked, hovered over, etc., and which you can register callbacks with. 假设您已实现了动画精灵,则按钮实际上是可以响应于被单击,悬停等而播放某些动画的精灵,并可以向其注册回调。 In my 2D engine, I have a class called UiButton, which inherits Sprite, and listens for various UI events. 在我的2D引擎中,我有一个名为UiButton的类,该类继承了Sprite,并侦听各种UI事件。 It gets more complicated when you want to handle keyboard navigation (arrow keys + enter to select) as you have to think about how the buttons are connected and which of them has focus at any given moment. 当您要处理键盘导航(箭头键+ Enter进行选择)时,这变得更加复杂,因为您必须考虑按钮的连接方式以及在任何给定时刻哪个按钮都具有焦点。

Here is my implementation for reference: 这是我的实现以供参考:
Headers: https://github.com/RobJinman/dodge/tree/master/Dodge/include/dodge/ui 标头: https//github.com/RobJinman/dodge/tree/master/Dodge/include/dodge/ui
Source: https://github.com/RobJinman/dodge/tree/master/Dodge/src/ui 来源: https : //github.com/RobJinman/dodge/tree/master/Dodge/src/ui

If you're not prepared to roll your own, Googling "direct2d gui framework" seems to bring up some promising results. 如果您不准备推出自己的产品,那么Googling的“ direct2d gui框架”似乎会带来一些有希望的结果。

Sorry I can't be of more help. 抱歉,我没有更多帮助。

I would look at Graphucks. 我会看看Graphucks。 It was created by Jeremiah Morrill (Microsoft MVP) and I have very good experiences with it. 它是由耶利米·莫里尔(Jeremiah Morrill)(Microsoft MVP)创建的,我对此有很好的经验。 http://graphucks.codeplex.com/ http://graphucks.codeplex.com/

Direct2D is a graphics library. Direct2D 是一个图形库。 UI controls like, Text-selection, Textbox, and Buttons is not a part of it. UI 控件(如文本选择、文本框和按钮)不是其中的一部分。 However the benefits of using Direct2D and DirectWrite is we can implement our own UI controls, and having a full control of it.然而,使用 Direct2D 和 DirectWrite 的好处是我们可以实现自己的 UI 控件,并且可以完全控制它。

Please also see: ID2D1Geometry::FillsContainsPoint() for hit-testing task.另请参阅: ID2D1Geometry::FillsContainsPoint()用于命中测试任务。

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

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