在Windows窗体中使用XNA轻松渲染(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/easy-rendering-with-xna-inside-a-windows-form-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 7 分钟阅读 - 3154 个词 阅读量 0在Windows窗体中使用XNA轻松渲染(译文)
原文地址:https://www.codeproject.com/Articles/21330/Easy-Rendering-with-XNA-Inside-a-Windows-Form
原文作者:Inaki Ayucar
译文由本站 robot-v1.0 翻译
前言
This article shows an easy way to render 2D or 3D graphics in a Windows Form using XNA, keeping all the Windows features and controls
本文介绍了一种使用XNA在Windows窗体中呈现2D或3D图形的简便方法,同时保留了所有Windows功能和控件
介绍(Introduction)
我读了(I read in) 这个(this) 鲍勃`布拉德利(Bob Bradley)的文章指出,使用XNA可以轻松完成艰苦的工作,而轻松的事情则非常艰巨.到目前为止,这是绝对正确的,XNA与Windows Forms的集成就是一个例子.(article by Bob Bradley that hard things are easy with XNA, and easy things are quite hard. By now that’s absolutely true and XNA integration with Windows Forms is an example of that.) 注意:此示例基于(Note: This example is based on) 这个(this) ZiggyWare教程,并添加了一些功能,例如:(ZiggyWare tutorial, and adds to it several features like:)
- 可继承的WinForm准备呈现XNA(Inheritable WinForm ready to render XNA)
- 一种(A)
RefreshMode
允许选择"始终"刷新选项(请参见(allows to select an “always” refresh option (see)**程式(program.cs)**有关详细信息)或(for details) or an)OnPanelPaint
选项,每次绘制面板时都会刷新渲染.您可以随时致电(option, which will refresh the render each time the panel is painted. You can always call the)public Render()
手动方法(method manually) - 将视口背景色清除为面板控件的背景色(Cleans the viewport’s background color to the Panel Control’s back color)
- 机具(Implements)
OnFrameMove
,(,)OnFrameRender
,(,)OnDeviceResetting
和(and)OnDeviceReset
事件,以DirectX Framework的方式(events, in a DirectX Framework way) 注意2:我刚刚意识到关于同一主题还有另一篇文章(Note 2: I’ve just realized there’s another article on this same topic) 这里(here) .这也是一种很好的方法,可以处理我们稍后讨论的两条道路.看看这个!(. It’s also a very good approach and handles both roads that we discuss later. Check it out!)
背景(Background)
由于XNA是一个跨平台的游戏开发框架,可以与PC-Windows和XBox360一起使用,因此它的核心不能像DirectX多年来那样依赖Windows窗体.(As XNA is a cross-platform game development Framework, ready to work both with PC-Windows and XBox360, its core cannot rely on Windows Forms like DirectX has been doing for years.) 因此,当我们创建第一个XNA游戏项目时,根本找不到任何Window.甚至(So, when we create our first XNA Game Project, we’ll find no Window at all. Even)**System.Windows.Forms.dll(System.Windows.Forms.dll)**在项目中未引用.启动应用程序时,窗口会神奇地弹出.(is not referenced in the project. The window just magically pops up when we launch the application.) 实际上,所有这些工作都是由XNA Framework在内部完成的,它对我们来说是一个抽象层,为应用程序运行所在的平台构建了合适的环境.(In fact, all this stuff is done internally by the XNA Framework, which works as an abstraction layer for us, building the appropriate environment for the platform the application runs in.) 太好了,但是,如果我不需要XBox支持怎么办?如果我想添加一个简单的TextBox来让用户输入他的名字怎么办?当然,所有这些问题都将在XNA的未来版本中得到解答,包括可能仅通过新的Project Template对纯Windows项目提供本机支持,但就目前而言,我们还是一个人.那么我们该如何解决呢?(That’s great but, what if I don’t need XBox support? What if I want to add a simple TextBox to let the user enter his name? Of course all these questions will be answered in future versions of XNA, including native support for Windows-only projects, probably through a new Project Template, but for now, we are alone. So how do we fix that?) 基本上,有两条不同的道路:(Basically, there are two different roads:)
- 生成一个Windows应用程序项目,该项目使用XNA对象来处理控件中的呈现,保留所有WinForms设计器功能,但缺少Visual Studio的所有XNA扩展.(Build a Windows Application project which uses XNA objects to handle rendering in a control, keeps all the WinForms designer functionality, but lacks all the XNA extensions for Visual Studio.)
- 生成一个Game for Windows项目,该项目使用Windows控件制作UI.这保留了所有XNA和扩展功能,但将迫使您"手动"设计控件.(Build a Game For Windows project, which uses Windows Controls to make the UI. This keeps all the XNA and extensions functionality, but will force you to design your controls “by hand”.) 在本文中,我们将遵循第一个路径,将第二个路径留给下一篇文章.(In this article, we will follow the first path, and leave the second one for a next post.)
使用代码(Using the Code)
我在ZIP中提供的是一个完整的Visual Studio Express项目,您可以将其用作自己的应用程序的空模板.这包括(What I provide in the ZIP is a full Visual Studio Express project you can use as an empty template for your own applications. It includes) XNAWinForm
:您可以继承或更改的基本表单,以包含所需的尽可能多的元素.在示例代码中,您还将找到(: a base Form you can inherit from, or you can change it, to include as many elements you may need. In the sample code you will also find) Form1
:以及使用示例(: and an example of using) XNAWinForm
.(.)
因此,对于那些不熟悉DirectX或MDX的人,如果您不想在Form中包含所有呈现逻辑,则XNA会公开几个有用的事件:(So, for those of you who are familiar with DirectX or MDX, XNA exposes several useful events if you don’t want to include all your rendering logic inside the Form:)
-
OnFrameMove
:在渲染之前调用它,以允许您更新可能需要的任何内容.(: It is called before rendering, to allow you to update anything you may need.) -
OnFrameRender
:在其处理程序中包括您可能需要的任何渲染代码.(: Include in its handler any rendering code you may want.) -
DeviceResetting
:在将要重置设备时调用此事件,因此您可以处置对象或其他任何东西.(: This event is called when the device is going to be reset, so you can dispose objects or whatever.) -
DeviceReset
:在重置设备后调用,以允许您更新与设备有关的元素.(: Called after the device is reset, to allow you to update device-dependent elements.) 为了编写自己的XNA就绪表单,您要做的第一件事是创建一个New(In order to code your own XNA ready form, the first thing you have to do is to create a New)WindowsApplication
项目,添加对XNA DLL的引用,并将要呈现的控件添加到您的空表单中(在示例中:(project, add the references to XNA DLLs, and add to your empty form the control you want to render in (in the example:)panelViewport
).().) 如您稍后所见,我们将不得不处理事件(As you will see later, we will have to handle the events)Resize
和(and)Paint
面板视口中的对象,因此通过双击设计器中的事件为其添加处理程序.(of the panel viewport, so add a handler for them by double-clicking on the events in the designer.) 之后,将需要一点编码:(After that, a little bit of coding will be necessary:) -
加(Add)
using
声明,只是为了安慰:(statements, just for comfort:) -
创建类型的局部变量(Create a local variable of the type)
GraphicsDeviceObject
:(:) -
覆盖(Override the)
OnLoad
将所有XNA初始化放在此处的Form方法(基本上调用两个方法,用于设备创建和重置):(method of the Form to put all the XNA initialization there (basically call two methods, for device creation and reset):) -
在里面(In the)
Resize
创建事件处理程序,调用(event handler created, call)ResetGraphicsDevice
:(:) -
将所需的典型渲染代码包括在(Include the typical rendering code that you want in the)
Paint
事件处理程序:(event handler:) -
现在,我们将创建调用设备创建和重置所需的方法.以下方法创建一个(Now, we will create the methods called earlier, needed to handle device creation and reset. The following method creates a)
GraphicsDevice
具有某些创建参数的对象:(object, with certain creation parameters:) -
每次(The following one will be called each time the)
panelViewport
调整大小,以重新创建(is resized, to re-create the)GraphicsDevice
新的视口尺寸:(with the new viewport size:)
就是这样,您已经准备好Windows窗体可以使用XNA渲染图形.(That’s it, you have a Windows Form ready to render graphics with XNA.)
兴趣点(Points of Interest)
注意:此示例基于(Note: This example is based on) 这个(this) ZiggyWare教程.(ZiggyWare tutorial.) ZiggyWare(ZiggyWare) 对于任何XNA或一般开发人员来说,它都是必须的.他们有很棒的教程.(should be a must for any XNA or general developer. They have great tutorials.) 我刚刚意识到关于同一主题的另一篇文章(I’ve just realized there is another article on this same topic) 这里(here) .这也是一种非常好的方法,可以处理我们之前讨论的两种方法.看看这个!(. It’s also a very good approach and handles both ways we discussed previously. Check it out!)
历史(History)
- 第一版:2007年11月16日(First version: November 16, 2007)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# WinXP Windows .NET .NET1.1 Visual-Studio Dev 新闻 翻译