2011-01-12 50 views
1

我有一个程序,为用户提供了行车路线,我想用户能够打印只是方向只是方向和地图通过为每个选项选择相应的单选按钮。我如何只打印这些部分而不是整个表格?我怎样才能使用单选按钮来告诉它打印什么?由于如何打印只是一个或两个控制器,而不是在整个窗体的Visual Basic

更新代码:

Private Sub btnprint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprint.Click 
     If rbprintall.Checked = True Then 
      'Ad code to print all here 
     ElseIf rbprintdirections.Checked = True Then 
      lbldirections.DrawToBitmap() 
      lbldirections2.DrawToBitmap() 
      lbldirections3.DrawToBitmap() 
      lbldirections4.DrawToBitmap() 

     Else 
      MessageBox.Show("You must select one of the above options!") 
     End If 

回答

1

如果你使用Windows窗体,几乎所有的内置控件,可以很容易地使用他们的DrawToBitmap method呈现到的图像。如果您只想打印控件当前显示的任何内容,则这是最简单的方法。但请注意,对于可以用这种方式绘制的内容有几点警告。详细信息请参阅链接的文档。

通过利用System.Drawing.Printing namespace中提供的功能,.NET Framework中的打印也变得相对简单。如果你还没有这方面的经验,谷歌的一些教程。例如,MSDN杂志上有一篇相当不错的文章here

因此,要实现自己的终极目标,你需要做到以下几点:

  1. 当用户点击“打印”,确定哪个单选按钮当前选择。
  2. 创建一个临时位图,并使用控件的DrawToBitmap方法在该位图中获取相应控件的副本。 (如果您需要打印多个控制,为每个人单独的临时位图,然后打印他们每个人的下一个步骤。)
  3. 画出图像中的PrintPage event handler方法使用Graphics.DrawImage method打印机您创建的PrintDocument对象。

编辑:我真的不知道您的问题是关于你已经发布的代码,但我看到了两个主要问题。

  1. 在你if语句的第一个块中,意见建议你不知道如何打印所有的控制。你有几个不同的选择。如果您只想打印出现在窗体上的每个控件,则可以简单地使用Form本身的DrawToBitmap方法。这将创建一个表单的整个客户区域的位图,包括它包含的所有控件。

    如果表单上仍有一些控件不需要打印,即使用户选择“全部打印”,您也需要在每个控件上调用DrawToBitmap方法。您可能会设置一个循环,以便您不必为每个控件编写一行代码,但没有真正的另一个好选择。

  2. 我怀疑你会遇到第二个elseif块的问题,就像你期望的那样。回想一下,我上面说过,您需要创建临时位图图像,然后将划分为这些位图?该DrawToBitmap方法有两个参数:

    • 位:要控制的图像的System.Drawing.Bitmap被拖入

    • targetBounds:一个System.Drawing.Rectangle描述控件的边界将会呈现

    您显示的代码缺少这两个参数ETERS。在这种情况下,第二个(targetBounds)很简单 - 因为要绘制整个控件,所需的只是为该参数指定ClientRectangle property

    对于第一个(bitmap)参数,您需要按照上面所述进行操作,并通过声明一个临时变量来创建一个新的Bitmap图像。然后,您需要使用指定的临时位图调用DrawToBitmap方法。

    也许我可以更清楚一个例子。修改上面的代码看起来像这样:

    'Declare some class-level variables to hold images of the controls to print 
    Private bmpDirections1 As System.Drawing.Bitmap 
    Private bmpDirections2 As System.Drawing.Bitmap 
    Private bmpDirections3 As System.Drawing.Bitmap 
    Private bmpDirections4 As System.Drawing.Bitmap 
    
    Private Sub btnprint_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnprint.Click 
        If rbprintall.Checked = True Then 
         'Add code to print all here 
        ElseIf rbprintdirections.Checked = True Then 
         'Draw lbldirections control to bitmap variable named bmpDirections1 
         bmpDirections1 = New Bitmap(lbldirections.Width, lbldirections.Height) 
         lbldirections.DrawToBitmap(bmpDirections1, lbldirections.ClientRectangle) 
    
         'Draw lbldirections2 control to bitmap variable named bmpDirections2 
         bmpDirections2 = New Bitmap(lbldirections2.Width, lbldirections2.Height) 
         lbldirections2.DrawToBitmap(bmpDirections2, lbldirections2.ClientRectangle) 
    
         'Draw lbldirections3 control to bitmap variable named bmpDirections3 
         bmpDirections3 = New Bitmap(lbldirections3.Width, lbldirections3.Height) 
         lbldirections3.DrawToBitmap(bmpDirections3, lbldirections3.ClientRectangle)    
    
         'Draw lbldirections4 control to bitmap variable named bmpDirections4 
         bmpDirections4 = New Bitmap(lbldirections4.Width, lbldirections4.Height) 
         lbldirections4.DrawToBitmap(bmpDirections4, lbldirections4.ClientRectangle) 
        Else 
         MessageBox.Show("You must select one of the above options!") 
        End If 
    End Sub 
    

    要记住的唯一的事情是,你需要,当你使用完毕后调用Dispose method上的每个位图的变量。 Dispose方法“释放图像拥有的资源”,这意味着它释放它正在使用的内存。如果你不打算再次使用它们,没有理由保留这些大型图像。所以,你应该在打印工作完成后做这个。一旦处理位图对象,将不再能够使用它包含的图像,但是您始终可以创建一个新的位图对象,并在下次用户将其分配给变量(如上面的代码所示)时点击打印。

  3. 无关:没有理由测试If x = True。更简单的If x是同样的事情,大多数程序员认为它是更好的风格。实际上,您正在对True进行双重比较,这不是必要的。

+0

谢谢一吨科迪! – Blake 2011-01-12 02:40:39

相关问题