图形与打印编程入门
1. 图形与打印基础
1.1 关键概念与术语
在图形与打印编程中,有许多重要的概念和术语需要了解:
|术语|解释|
| ---- | ---- |
|AddHandler 语句|为特定事件添加事件处理程序|
|AddressOf 运算符|指定可与事件关联的方法的位置|
|API(应用程序编程接口)|程序用于访问操作系统和计算机上各种服务的接口|
|Brush 对象|用于在绘制实心形状时指定绘图参数|
|Brushes 类|提供对表示标准颜色的 Brush 对象的便捷访问|
|Color 结构|表示一种颜色,并提供创建自定义颜色的方法|
|Control 类|可用于声明变量以引用窗体上的控件,定义 Windows 窗体控件的公共属性和方法|
|坐标系统|用于标识计算机屏幕上每个可能点的方案|
|设计单位|字体的任何指定测量单位|
|抖动|使用现有颜色的小点形成图案来模拟所需颜色|
|PrintPreviewDialog 类的 Document 属性|允许指定在对话框中显示的文档|
|Graphics 类的 DrawLine 方法|在两个指定点之间绘制指定颜色的线|
|Graphics 类的 DrawRectangle 方法|在指定位置绘制指定大小和颜色的矩形轮廓|
|Graphics 类的 DrawString 方法|在指定位置绘制指定的字符串|
|Font 类|包含定义唯一字体的属性|
|FontFamily 类|表示字体的字体系列(用于组织具有相似属性的字体的分组结构)|
|FontStyle 枚举|提供用于指定字体样式的常量,如粗体、斜体等|
|Color 结构的 FromArgb 方法|从 RGB 值和 alpha 值创建新的 Color 对象|
|GDI+(图形设备接口)|提供用于创建二维矢量图形的类的应用程序编程接口|
|FontFamily 类的 GetName 方法|返回 FontFamily 对象的名称|
1.2 关键类与方法
1.2.1 Font 类
用于定义应用程序中文本的字体、大小和样式,其属性如下:
-
Bold
:设置文本的粗细。
-
FontFamily
:包含一个 FontFamily 对象,用于存储字体外观信息。
-
Italic
:设置文本的倾斜角度。
-
Size
:设置文本的大小。
-
SizeInPoints
:以磅为单位返回文本的大小。
1.2.2 Graphics 类
包含用于绘制文本、线条和形状的方法:
-
DrawLine
:绘制指定大小和颜色的线。
-
DrawRectangle
:在指定位置绘制指定大小和颜色的矩形轮廓。
-
DrawString
:在指定位置以指定字体和颜色绘制字符串。
1.2.3 PrintDocument 类
允许指定如何打印文档:
-
事件
:PrintPage 事件在需要打印当前页面的数据时触发。
-
方法
:Print 方法使用 Graphics 对象打印页面。
1.2.4 PrinterSettings 类
存储有关系统打印机设置的信息,其属性
InstalledPrinters.Count
返回系统上安装的打印机数量。
1.2.5 PrintPageEventArgs 类
包含传递给 PrintPage 事件的数据,其属性如下:
-
MarginBounds
:指定打印页面的边距。
-
Left
:指定页面的左边界。
-
Top
:指定页面的上边界。
1.2.6 PrintPreviewDialog 控件
用于显示文档打印时的外观,其属性和方法如下:
-
属性
:
-
Document
:指定控件预览的文档,文档必须是 PrintDocument 类型。
-
Name
:指定用于以编程方式访问 PrintPreviewDialog 控件的名称。
-
UseAntiAlias
:指定对话框是否显示平滑图像。
-
方法
:ShowDialog 方法用于向用户显示 PrintPreviewDialog。
1.3 基本操作
1.3.1 打印操作
-
打印线条
:
- 使用 PrintPageEventArgs 对象的 Graphics 属性。
- 使用 Graphics 属性调用 DrawLine 方法。
- 指定五个参数:Pen 对象、第一个 x 坐标、第一个 y 坐标、第二个 x 坐标和第二个 y 坐标。
-
打印矩形
:
- 使用 PrintPageEventArgs 对象的 Graphics 属性。
- 使用 Graphics 属性调用 DrawRectangle 方法。
- 指定五个参数:Pen 对象、x 坐标、y 坐标、宽度和高度。
-
打印字符串
:
- 使用 PrintPageEventArgs 对象的 Graphics 属性。
- 使用 Graphics 属性调用 DrawString 方法。
- 指定五个参数:要打印的字符串、字体样式、Brush 对象、字符串开始打印位置的 x 坐标和 y 坐标。
1.3.2 事件关联
将事件与定义的事件处理程序关联时,遵循以下格式:
AddHandler objectName.eventName, AddressOf eventHandlerName
其中,
objectName
表示与事件关联的对象的名称,
eventName
表示有效事件的名称,
eventHandlerName
表示要与指定事件关联的定义的事件处理程序的名称。
1.3.3 打印文档
打印文档的步骤如下:
1. 创建一个新的 PrintDocument 对象。
2. 定义 PrintDocument 的 PrintPage 事件处理程序,以指定要打印的内容。
3. 使用 PrintDocument 调用 Print 方法。
1.3.4 显示打印预览对话框
显示打印预览对话框的步骤如下:
1. 创建一个 PrintPreviewDialog 对象。
2. 在 PrintPreviewDialog 的 Document 属性中指定要预览的 PrintDocument。
3. 调用 PrintPreviewDialog 的 ShowDialog 方法。
1.4 示例代码分析
以下是一个打印按钮点击事件处理程序的代码示例:
Private Sub printButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles printButton.Click
Dim output As New PrintDocument()
AddHandler output.PrintPage, _
AddressOf output_PrintPage
output.Print()
End Sub ' printButton_Click
这段代码的功能是:当用户点击打印按钮时,创建一个新的 PrintDocument 对象,将其 PrintPage 事件与
output_PrintPage
方法关联,然后调用 Print 方法开始打印。
1.5 错误处理代码
以下是一个显示错误消息的代码示例:
' display an error message to the user
Sub ErrorMessage()
MessageBox.Show("No printers installed. You must " & _
"have a printer installed to preview or print " & _
"the document.", "Print Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Sub ' ErrorMessage
当系统中没有安装打印机时,调用该方法会弹出一个消息框,提示用户必须安装打印机才能预览或打印文档。
1.6 操作流程总结
graph TD;
A[创建 PrintDocument 对象] --> B[关联 PrintPage 事件处理程序];
B --> C[调用 Print 方法打印文档];
D[创建 PrintPreviewDialog 对象] --> E[指定要预览的 PrintDocument];
E --> F[调用 ShowDialog 方法显示预览对话框];
以上就是图形与打印编程的基础内容,包括关键概念、类和方法,以及基本的操作步骤和示例代码。通过这些知识,你可以开始进行简单的图形绘制和打印应用程序的开发。
2. 图形与打印应用实例
2.1 CheckWriter 应用程序
2.1.1 功能概述
CheckWriter 应用程序允许用户在支票中输入数据,并使用计算机上安装的打印机进行打印。在开发该应用程序的过程中,涉及到图形对象(如 Pens 和 Brushes)的使用,以及如何使用代码创建字体来应用于要显示或打印的文本。
2.1.2 关键操作
- 创建 PrintDocument 对象 :用于描述如何打印文档。
Dim output As New PrintDocument()
- 关联 PrintPage 事件处理程序 :指定当用户点击打印按钮时,执行绘制和打印支票的代码。
AddHandler output.PrintPage, AddressOf output_PrintPage
- 调用 Print 方法 :开始打印文档。
output.Print()
- 添加打印预览对话框 :允许用户在打印前预览支票。
Dim previewDialog As New PrintPreviewDialog()
previewDialog.Document = output
previewDialog.ShowDialog()
2.1.3 代码示例
Private Sub printButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles printButton.Click
Dim output As New PrintDocument()
AddHandler output.PrintPage, _
AddressOf output_PrintPage
Dim previewDialog As New PrintPreviewDialog()
previewDialog.Document = output
If PrinterSettings.InstalledPrinters.Count > 0 Then
previewDialog.ShowDialog()
output.Print()
Else
ErrorMessage()
End If
End Sub ' printButton_Click
2.2 应用程序扩展练习
2.2.1 CheckWriter 应用修改
修改 CheckWriter 应用程序,使其能够显示和打印支票的背景图像。具体步骤如下:
1.
复制模板到工作目录
:将
C:\Examples\Tutorial26\Exercises\ModifiedCheckWriter
目录复制到
C:\SimplyVB2008
目录。
2.
打开应用程序模板文件
:双击
ModifiedCheckWriter
目录中的
CheckWriter.sln
文件打开应用程序。
3.
创建 CheckedChanged 事件处理程序
:双击 Wood RadioButton 创建其 CheckedChanged 事件处理程序。
4.
定义 CheckedChanged 事件处理程序
:当用户选择背景时通知应用程序。如果选择 Wood RadioButton,则在 previewPicture 中显示木质背景的预览;如果选择 Brick RadioButton,则显示砖块背景的预览。
Private Sub woodRadioButton_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles woodRadioButton.CheckedChanged
If woodRadioButton.Checked Then
previewPicture.Image = Image.FromFile("C:\Examples\Tutorial26\Exercises\Images\wood.jpg")
End If
End Sub
Private Sub brickRadioButton_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles brickRadioButton.CheckedChanged
If brickRadioButton.Checked Then
previewPicture.Image = Image.FromFile("C:\Examples\Tutorial26\Exercises\Images\brick.jpg")
End If
End Sub
- 修改 document_PrintPage 事件处理程序 :打印背景图像。
Private Sub document_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles output.PrintPage
If woodRadioButton.Checked Then
e.Graphics.DrawImage(Image.FromFile("C:\Examples\Tutorial26\Exercises\Images\wood.jpg"), 0, 0, 800, 600)
ElseIf brickRadioButton.Checked Then
e.Graphics.DrawImage(Image.FromFile("C:\Examples\Tutorial26\Exercises\Images\brick.jpg"), 0, 0, 800, 600)
End If
' 其他打印内容
End Sub
-
运行应用程序
:选择
Debug > Start Debugging运行应用程序,输入数据并选择背景,验证预览和打印效果。 - 关闭应用程序和 IDE :点击关闭框关闭应用程序和 Visual Basic IDE。
2.2.2 公司标志设计应用程序
开发一个公司标志设计应用程序,允许用户设计公司标志。具体步骤如下:
1.
复制模板到工作目录
:将
C:\Examples\Tutorial26\Exercises\CompanyLogo
目录复制到
C:\SimplyVB2008
目录。
2.
打开应用程序模板文件
:双击
CompanyLogo
目录中的
CompanyLogo.sln
文件打开应用程序。
3.
定义添加按钮的 Click 事件处理程序
:根据用户指定的形状在 PictureBox 上绘制。
Private Sub addButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles addButton.Click
Dim g As Graphics = pictureBox1.CreateGraphics()
Dim color As Color = Color.FromName(comboBox1.Text)
Dim pen As New Pen(color)
If lineRadioButton.Checked Then
Dim x1 As Integer = Integer.Parse(x1TextBox.Text)
Dim y1 As Integer = Integer.Parse(y1TextBox.Text)
Dim x2 As Integer = Integer.Parse(x2TextBox.Text)
Dim y2 As Integer = Integer.Parse(y2TextBox.Text)
g.DrawLine(pen, x1, y1, x2, y2)
ElseIf rectangleRadioButton.Checked Then
Dim x As Integer = Integer.Parse(xTextBox.Text)
Dim y As Integer = Integer.Parse(yTextBox.Text)
Dim width As Integer = Integer.Parse(widthTextBox.Text)
Dim height As Integer = Integer.Parse(heightTextBox.Text)
g.DrawRectangle(pen, x, y, width, height)
End If
End Sub
- 定义清除按钮的 Click 事件处理程序 :清除 PictureBox 中的图形,并清空所有文本框。
Private Sub clearButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles clearButton.Click
pictureBox1.Invalidate()
x1TextBox.Text = ""
y1TextBox.Text = ""
x2TextBox.Text = ""
y2TextBox.Text = ""
xTextBox.Text = ""
yTextBox.Text = ""
widthTextBox.Text = ""
heightTextBox.Text = ""
End Sub
-
运行应用程序
:选择
Debug > Start Debugging运行应用程序,使用 RadioButtons 和 TextBoxes 显示不同类型的形状,点击清除按钮清除图形。 - 关闭应用程序和 IDE :点击关闭框关闭应用程序和 Visual Basic IDE。
2.2.3 信头设计应用程序
创建一个信头设计应用程序,允许用户设计公司文档的信纸。具体步骤如下:
1.
复制模板到工作目录
:将
C:\Examples\Tutorial26\Exercises\LetterHead
目录复制到
C:\SimplyVB2008
目录。
2.
打开应用程序模板文件
:双击
LetterHead
目录中的
LetterHead.sln
文件打开应用程序。
3.
添加 OpenFileDialog
:在工具箱的对话框选项卡中双击 OpenFileDialog 控件,命名为
openImageFileDialog
,并将
FileName
属性设置为
letterhead.png
。
4.
定义浏览按钮的 Click 事件处理程序
:显示打开对话框,允许用户浏览所需的图像文件。
Private Sub browseButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles browseButton.Click
openImageFileDialog.ShowDialog()
If openImageFileDialog.FileName <> "" Then
imageLocationTextBox.Text = openImageFileDialog.FileName
pictureBox1.Image = Image.FromFile(openImageFileDialog.FileName)
End If
End Sub
- 创建 PrintPreviewDialog 控件 :允许用户在打印前预览信头。
Dim previewDialog As New PrintPreviewDialog()
- 定义 PrintPage 事件处理程序 :在屏幕顶部绘制所选图像,并在图像下方打印指定文本。
Private Sub output_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles output.PrintPage
If imageLocationTextBox.Text <> "" Then
e.Graphics.DrawImage(Image.FromFile(imageLocationTextBox.Text), 0, 0, 800, 200)
End If
e.Graphics.DrawString("Contact Information: " & contactInfoTextBox.Text, New Font("Arial", 12), Brushes.Black, 0, 220)
End Sub
- 定义打印按钮的 Click 事件处理程序 :指定 PrintDocument 的 PrintPage 事件处理程序,并打印文档。
Private Sub printButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles printButton.Click
Dim output As New PrintDocument()
AddHandler output.PrintPage, AddressOf output_PrintPage
If PrinterSettings.InstalledPrinters.Count > 0 Then
output.Print()
Else
ErrorMessage()
End If
End Sub
- 定义预览按钮的 Click 事件处理程序 :指定 PrintDocument 的 PrintPage 事件处理程序,并显示预览对话框。
Private Sub previewButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles previewButton.Click
Dim output As New PrintDocument()
AddHandler output.PrintPage, AddressOf output_PrintPage
Dim previewDialog As New PrintPreviewDialog()
previewDialog.Document = output
If PrinterSettings.InstalledPrinters.Count > 0 Then
previewDialog.ShowDialog()
Else
ErrorMessage()
End If
End Sub
-
测试应用程序
:使用提供的
Letterhead.png图像文件测试应用程序的信头图像功能。 -
运行应用程序
:选择
Debug > Start Debugging运行应用程序,输入联系信息并指定图像位置,验证预览和打印效果。 - 关闭应用程序和 IDE :点击关闭框关闭应用程序和 Visual Basic IDE。
2.3 编程挑战
2.3.1 屏幕保护程序应用程序
开发一个模拟屏幕保护程序的应用程序,在屏幕上随机位置添加随机颜色、随机大小的实心和空心形状。具体步骤如下:
1.
复制模板到工作目录
:将
C:\Exercises\Tutorial26\ScreenSaver
目录复制到
C:\SimplyVB2008
目录。
2.
编写 DisplayShape 方法
:创建 Graphics 对象,指定随机颜色、大小和位置。
Sub DisplayShape()
Dim g As Graphics = Me.CreateGraphics()
Dim random As New Random()
Dim color As Color = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256))
Dim pen As New Pen(color)
Dim brush As New SolidBrush(color)
Dim shapeType As Integer = random.Next(2) ' 0 表示矩形,1 表示椭圆
Dim x As Integer = random.Next(Me.Width - 100)
Dim y As Integer = random.Next(Me.Height - 100)
Dim width As Integer = random.Next(100)
Dim height As Integer = random.Next(100)
If shapeType = 0 Then
g.DrawRectangle(pen, x, y, width, height)
g.FillRectangle(brush, x, y, width, height)
Else
g.DrawEllipse(pen, x, y, width, height)
g.FillEllipse(brush, x, y, width, height)
End If
End Sub
- 关联 Timer 的 Tick 事件处理程序 :定期调用 DisplayShape 方法。
Private Sub timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles timer1.Tick
DisplayShape()
End Sub
2.3.2 增强型屏幕保护程序应用程序
在屏幕保护程序应用程序的基础上进行增强,在指定时间后清除屏幕上显示的形状,并为颜色指定随机透明度。具体步骤如下:
1.
修改 Timer 的 Tick 事件处理程序
:添加代码以清除屏幕。
Private Sub timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles timer1.Tick
Static counter As Integer = 0
counter += 1
If counter > 10 Then ' 每 10 次刷新清除一次屏幕
Me.Invalidate()
counter = 0
End If
DisplayShape()
End Sub
- 修改 DisplayShape 方法 :为颜色指定随机透明度。
Sub DisplayShape()
Dim g As Graphics = Me.CreateGraphics()
Dim random As New Random()
Dim alpha As Integer = random.Next(256)
Dim color As Color = Color.FromArgb(alpha, random.Next(256), random.Next(256), random.Next(256))
Dim pen As New Pen(color)
Dim brush As New SolidBrush(color)
Dim shapeType As Integer = random.Next(2) ' 0 表示矩形,1 表示椭圆
Dim x As Integer = random.Next(Me.Width - 100)
Dim y As Integer = random.Next(Me.Height - 100)
Dim width As Integer = random.Next(100)
Dim height As Integer = random.Next(100)
If shapeType = 0 Then
g.DrawRectangle(pen, x, y, width, height)
g.FillRectangle(brush, x, y, width, height)
Else
g.DrawEllipse(pen, x, y, width, height)
g.FillEllipse(brush, x, y, width, height)
End If
End Sub
2.4 总结
通过以上应用实例和编程挑战,我们可以看到图形与打印编程在实际应用中的广泛应用。从简单的支票打印到复杂的公司标志设计和屏幕保护程序开发,都离不开图形对象、事件处理和打印功能的使用。掌握这些知识和技能,能够帮助我们开发出更加丰富和实用的应用程序。
graph LR;
A[CheckWriter应用] --> B[公司标志设计应用];
B --> C[信头设计应用];
C --> D[屏幕保护程序应用];
D --> E[增强型屏幕保护程序应用];
综上所述,图形与打印编程是一个充满挑战和机遇的领域,通过不断学习和实践,我们可以开发出更加优秀的应用程序。
超级会员免费看
8537

被折叠的 条评论
为什么被折叠?



