这次讲的是全屏下的操作、自定义字体颜色和绘制各种图形。
===============华丽的分割线===============
学习内容DirectX与VB.NET编程(十一)全屏绘图与字体颜色的改变
·掌握全屏的实现方法
·能够自定义字体和颜色
·能够利用DirectDraw绘制各种图形
===============华丽的分割线===============
这次的范例运行后自动切换到全屏,点击绘制按钮绘制图形,点击退出按钮退出程序。
打开VB.NET,新建一个项目,添加DirectDraw的引用,在窗口中放置两个按钮(Button),控件设计请参考下图:
在类头输入如下代码开导入名称空间:
Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectDraw
声明设备和表面:
Dim vDev As New Device
Dim vSurface As Surface
===============华丽的分割线===============
为了全屏下隐藏窗口的标题栏,将窗口的FormBorderStyle属性设置为None
Me.FormBorderStyle = FormBorderStyle.None
下面设置设备的协作级别,承载控件依旧是窗口,协作类型选择为FullscreenExclusive,代表全屏独占模式
vDev.SetCooperativeLevel(Me, CooperativeLevelFlags.FullscreenExclusive)。
此时系统会自动将程序切换到全屏模式。
下面设置全屏模式下的显示模式,我们在此设置为640*320的分辨率,16位色深(65536色),85Hz刷新率,语句如下:
vDev.SetDisplayMode(640, 480, 16, 85, True)
最后一个参数是StandardVGAMode(标准VGA模式),根据SDK的注释是大概是设置硬件显示驱动模式,不管怎样,设置为True即可。
在此可能会有两个问题
一、如果窗口的大小不等于分辨率,DirectDraw会自动将窗口的大小调整到分辨率大小,因此,你无须担心窗口的尺寸问题。
二、设置了显示器不支持的显示模式,如果出现了该问题,该语句会抛出一个NotSupportedException的异常,我们只要进行捕捉即可,因此该语句应该放入Try块中,如下:
Try
vDev.SetDisplayMode(640, 480, 16, 85, True)
Catch ex As NotSupportedException
MsgBox("您的显示器不支持指定显示模式,程序将以当前分辨率运行。", MsgBoxStyle.Critical Or MsgBoxStyle.OKOnly, "错误")
End Try
===============华丽的分割线===============
下面就是表面的初始化:
Dim vSurfaceCaps As New SurfaceCaps
vSurfaceCaps.PrimarySurface = True
vSurfaceCaps.VideoMemory = True
Dim vSurfaceDes As New SurfaceDescription(vSurfaceCaps)
vSurface = New Surface(vSurfaceDes, vDev)
vSurface.FontTransparency = True
因为我们需要自定义绘图的字体和颜色,因此我们需要修改表面的某些属性:
首先修改字体,将字体的句柄传入表面中即可:
Dim vFont As IntPtr = New Font("黑体", 25, FontStyle.Bold Or FontStyle.Italic).ToHfont
vSurface.FontHandle = vFont
然后修改表面的前景色(ForeColor)和字体背景色(FontBackColor),前景色将影响所有绘制线条和文字的显示颜色,字体背景色则会影响所有文字的背景颜色,除非已经设置字体背景透明:
vSurface.ForeColor = Color.Red
vSurface.FontBackColor = Color.Black
如此,初始化工作全部完成。
===============华丽的分割线===============
下面就是填充“绘制”按钮的点击事件:
'画直线
vSurface.DrawLine(0, 0, 640, 480)
'画矩形
vSurface.DrawBox(5, 5, 635, 475)
'画圆
vSurface.DrawCircle(320, 240, 100)
'画椭圆
vSurface.DrawEllipse(0, 100, 640, 380)
'画圆角矩形
vSurface.DrawRoundedBox(10, 10, 630, 470, 5, 5)
'填充
vSurface.ColorFill(New Rectangle(315, 235, 10, 10), Color.White)
'绘制透明背景文本
vSurface.FontTransparency = True
vSurface.DrawText(5, 50, "HelloWorld", False)
'绘制非透明背景文本
vSurface.FontTransparency = False
vSurface.FontBackColor = Color.Black
vSurface.DrawText(5, 100, "HelloWorld", False)
在以上方法函数中,
DrawLine:画直线,参数分别是起始点X、起始点Y、终止点X和终止点Y;
DrawCircle:画圆,参数分别是圆心X、圆心Y和半径;
DrawEllipse:画椭圆,参数分别是左上角X、左上角Y、右下角X和右下角Y;
DrawRoundedBox:画圆角矩形,参数分别是对应的正矩形、圆角水平半径和圆角垂直半径;
ColorFill:填充,填充的矩形范围和填充的颜色;
FontTransparency:设置表面的字体是否背景透明。
以上方法可能还有其它重载函数,在此不熬述。
===============华丽的分割线===============
下面实现“退出”按钮的事件,退出按钮主要工作是释放对象内存并退出程序,代码如下:
If Not vSurface Is Nothing Then
vSurface.Dispose()
vSurface = Nothing
End If
If Not vDev Is Nothing Then
vDev.Dispose()
vDev = Nothing
End If
Me.Dispose()
End
===============华丽的分割线===============
程序的全部代码如下:
Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectDraw
Public Class Form1
Inherits System.Windows.Forms.Form
Dim vDev As New Device
Dim vSurface As Surface
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。DirectX与VB.NET编程(十一)全屏绘图与字体颜色的改变
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button2 = New System.Windows.Forms.Button
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(120, 8)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(104, 24)
Me.Button2.TabIndex = 2
Me.Button2.Text = "退出"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(104, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "绘制"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(344, 208)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.Button2)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.FormBorderStyle = FormBorderStyle.None
vDev.SetCooperativeLevel(Me, CooperativeLevelFlags.FullscreenExclusive)
Try
vDev.SetDisplayMode(640, 480, 16, 85, True)
Catch ex As NotSupportedException
MsgBox("您的显示器不支持指定显示模式,程序将以当前分辨率运行。", MsgBoxStyle.Critical Or MsgBoxStyle.OKOnly, "错误")
End Try
Dim vSurfaceCaps As New SurfaceCaps
vSurfaceCaps.PrimarySurface = True
vSurfaceCaps.VideoMemory = True
Dim vSurfaceDes As New SurfaceDescription(vSurfaceCaps)
vSurface = New Surface(vSurfaceDes, vDev)
vSurface.FontTransparency = True
Dim vFont As IntPtr = New Font("黑体", 25, FontStyle.Bold Or FontStyle.Italic).ToHfont
vSurface.FontHandle = vFont
vSurface.ForeColor = Color.Red
vSurface.FontBackColor = Color.Black
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'画直线
vSurface.DrawLine(0, 0, 640, 480)
'画矩形
vSurface.DrawBox(5, 5, 635, 475)
'画圆
vSurface.DrawCircle(320, 240, 100)
'画椭圆
vSurface.DrawEllipse(0, 100, 640, 380)
'画圆角矩形
vSurface.DrawRoundedBox(10, 10, 630, 470, 5, 5)
'填充
vSurface.ColorFill(New Rectangle(315, 235, 10, 10), Color.White)
'绘制透明背景文本
vSurface.FontTransparency = True
vSurface.DrawText(5, 50, "HelloWorld", False)
'绘制非透明背景文本
vSurface.FontTransparency = False
vSurface.DrawText(5, 100, "HelloWorld", False)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Not vSurface Is Nothing Then
vSurface.Dispose()
vSurface = Nothing
End If
If Not vDev Is Nothing Then
vDev.Dispose()
vDev = Nothing
End If
Me.Dispose()
End
End Sub
End Class
程序最终运行效果如下:
===============华丽的分割线===============
DirectX与VB.NET编程(十一)全屏绘图与字体颜色的改变下次是图片的显示和剪切的使用。