我用如下的程序段打印一个表格,用DrawWidth属性来设置打印的表格线的宽度,可是无论我把DrawWidth的值设成多少,表格线的宽度都只是一个像素的宽度,也就是说DrawWidth的设置值无效,为什么会这样呢?
h = 300
l = 700
hp = 100
printer.DrawWidth = 3
For I = 1 To 10
  For j = 1 To 5
     printer.Line ((j - 1) * l + hp, h * (I - 1) + hp)-((j - 1) * l + l + hp, h * I + hp), , B
  Next
Next然而,我把程序度改成如下,把表格打印到picture控件中,DrawWidth属性的值却表现得很正常,为什么DrawWidth用于picture控件有效,而用于打印机无效呢?在打印机中应如何设置表格线的宽度?
h = 300
l = 700
hp = 100
Form1.Picture1.DrawWidth = 3
For I = 1 To 10
  For j = 1 To 5
     Form1.Picture1.Line ((j - 1) * l + hp, h * (I - 1) + hp)-((j - 1) * l + l + hp, h * I + hp), , B
  Next
Next

解决方案 »

  1.   

    打印机上画表格. 
    看程序效果的步骤: 
    1. 新建一个工程; 
    2. 把以下 
    ''''''''''''''form1 down''''''''''''''''' ... 
    ''''''''''''''form1 up................... 
    之间的内容贴到form1的代码窗中, 同时在form1中画两个名为PicFrame及 
    PicPrint的PictureBox, 注意:PicPrint应在PicFrame中绘制, 即PicFrame 
    为PicPrint的容器. 一个水平滚动条和一个垂直滚动条:HScl, VScl, 
    画一个Frame, 在此frame1中画一个按钮名为cmdPrint用于打印; 
    3. 添加一个module, 把 
    ''''''''''''''module down'''''''''''''''' 
    ... 
    ''''''''''''''module up'''''''''''''''''' 
    之间的内容贴到module1的代码窗中, 就可以Run啦, 所用到的变量很简单, 
    见module里的变量说明及form1的Form_load()中的示例. 
    注: 该程序用于中文表格打印, 若用于打印英文, 可稍作修改. 
    其实如果不是为了预览, 一个PrintTable()函数就够了. ''''''''''''''form1 down''''''''''''''''' 
    Option Explicit 
    Private TxtHeight As Single, TxtWidth As Single 
    Private WidthAdd As Single, HeightAdd As Single 
    Private MapCurX As Single, MapcurY As Single 
    Private FormLoaded As Boolean Private Sub ViewTable() 
    Dim i As Integer, j As Integer 
    Dim TempX As Single, TempY As Single 
    Dim TabWidth As Single, TabHeight As Single 
    PicPrint.Cls 
    TxtWidth = PicPrint.TextWidth(" 字 ") 
    WidthAdd = PicPrint.TextWidth(" ") * 0.5 
    TxtHeight = PicPrint.TextHeight("字") * 1.2 
    HeightAdd = PicPrint.TextHeight("字") * 0.2 
    TabHeight = 0 
    For j = 0 To TabRow 
    If TabRowHeight(j) < TxtHeight Then TabRowHeight(j) = TxtHeight 
    TabHeight = TabHeight + TabRowHeight(j) 
    Next 
    TabWidth = 0 
    For j = 0 To TabCol 
    For i = 0 To TabRow 
    TxtWidth = PicPrint.TextWidth(TabContent(i, j)) + 4 * WidthAdd 
    If TabColWidth(j) < TxtWidth Then 
    TabColWidth(j) = TxtWidth 
    End If 
    Next 
    TabWidth = TabWidth + TabColWidth(j) 
    Next 
    PicPrint.Width = TabX0 + TabWidth + 8 
    PicPrint.Height = TabY0 + TabHeight + 8 + TxtHeight * 2 
    SetSclStatus 
    PicPrint.CurrentX = TabX0 
    PicPrint.CurrentY = TabY0 
    PicPrint.Print TabTitle 
    'Table Border frame: 
    PicPrint.DrawWidth = 2 
    PicPrint.Line (TabX0, TabY0 + TxtHeight)-(TabX0 + TabWidth, TabY0 + TxtHeighht), vbBlack 
    PicPrint.Line -(TabX0 + TabWidth, TabY0 + TxtHeight + TabHeight), vbBlack 
    PicPrint.Line -(TabX0, TabY0 + TxtHeight + TabHeight), vbBlack 
    PicPrint.Line -(TabX0, TabY0 + TxtHeight), vbBlack 
    'Table grid line: 
    PicPrint.DrawWidth = 1 
    TempX = TabX0 + TabColWidth(0) 
    For j = 1 To TabCol 
    PicPrint.Line (TempX, TabY0 + TxtHeight)- _ 
    (TempX, TabY0 + TxtHeight + TabHeight) 
    TempX = TempX + TabColWidth(j) 
    Next 
    TempY = TabY0 + TxtHeight + TabRowHeight(0) 
    For i = 1 To TabRow 
    PicPrint.Line (TabX0, TempY)-(TabX0 + TabWidth, TempY) 
    TempY = TempY + TabRowHeight(i) 
    Next 
    'Print text on table: 
    TempY = TabY0 + TxtHeight 
    For i = 0 To TabRow 
    TempX = TabX0 
    For j = 0 To TabCol 
    PicPrint.CurrentX = TempX + WidthAdd 
    PicPrint.CurrentY = TempY + TabRowHeight(i) - TxtHeight + HeightAdd 
    PicPrint.Print TabContent(i, j) 
    TempX = TempX + TabColWidth(j) 
    Next 
    TempY = TempY + TabRowHeight(i) 
    Next 
    PicPrint.CurrentX = TabX0 
    PicPrint.CurrentY = TabY0 + TabHeight + TxtHeight + 2 
    PicPrint.Print TabFoot 
    End Sub 
    Private Sub PrintTable() 
    On Error GoTo ErrorHandler 
    Dim i As Integer, j As Integer 
    Dim TempX As Single, TempY As Single 
    Dim TabWidth As Single, TabHeight As Single 
    TxtWidth = Printer.TextWidth(" 字 ") 
    WidthAdd = Printer.TextWidth(" ") * 0.5 
    TxtHeight = Printer.TextHeight("字") * 1.2 
    HeightAdd = Printer.TextHeight("字") * 0.2 
    TabHeight = 0 
    For i = 0 To TabRow 
    If TabRowHeight(i) < TxtHeight Then TabRowHeight(i) = TxtHeight 
    TabHeight = TabHeight + TabRowHeight(i) 
    Next 
    TabWidth = 0 
    For j = 0 To TabCol 
    For i = 0 To TabRow 
    TxtWidth = Printer.TextWidth(TabContent(i, j)) + 4 * WidthAdd 
    If TabColWidth(j) < TxtWidth Then 
    TabColWidth(j) = TxtWidth 
    End If 
    Next 
    TabWidth = TabWidth + TabColWidth(j) 
    Next 
    Printer.CurrentX = TabX0 
    Printer.CurrentY = TabY0 
    Printer.Print TabTitle 
    'Table Border frame: 
    Printer.DrawWidth = 4 
    Printer.Line (TabX0, TabY0 + TxtHeight)-(TabX0 + TabWidth, TabY0 + TxtHeightt), vbBlack 
    Printer.Line -(TabX0 + TabWidth, TabY0 + TxtHeight + TabHeight), vbBlack 
    Printer.Line -(TabX0, TabY0 + TxtHeight + TabHeight), vbBlack 
    Printer.Line -(TabX0, TabY0 + TxtHeight), vbBlack 
    'Table grid line: 
    Printer.FillColor = vbBlack 
    Printer.ForeColor = vbBlack 
    Printer.DrawWidth = 1 
    TempX = TabX0 + TabColWidth(0) 
    For j = 1 To TabCol 
    Printer.Line (TempX, TabY0 + TxtHeight)- _ 
    (TempX, TabY0 + TxtHeight + TabHeight), vbBlack 
    TempX = TempX + TabColWidth(j) 
    Next 
    TempY = TabY0 + TxtHeight + TabRowHeight(0) 
    For i = 1 To TabRow 
    Printer.Line (TabX0, TempY)-(TabX0 + TabWidth, TempY), vbBlack 
    TempY = TempY + TabRowHeight(i) 
    Next 
    'Print text on table: 
    TempY = TabY0 + TxtHeight 
    For i = 0 To TabRow 
    TempX = TabX0 
    For j = 0 To TabCol 
    Printer.CurrentX = TempX + WidthAdd 
    Printer.CurrentY = TempY + TabRowHeight(i) - TxtHeight + HeightAdd 
    Printer.Print TabContent(i, j) 
    TempX = TempX + TabColWidth(j) 
    Next 
    TempY = TempY + TabRowHeight(i) 
    Next 
    Printer.CurrentX = TabX0 
    Printer.CurrentY = TabY0 + TabHeight + TxtHeight + 2 
    Printer.Print TabFoot 
    Printer.EndDoc 
    Exit Sub 
    ErrorHandler: 
    MsgBox "打印机未准备好!" 
    End Sub 
      

  2.   

    续上Private Sub cmdPrint_Click() 
    PrintTable 
    End Sub 
    Private Sub Form_Load() 
    Printer.ForeColor = vbBlack 
    Printer.FillColor = vbBlack 
    Dim i As Integer, j As Integer 
    FormLoaded = False 
    ScaleMode = 2 
    PicFrame.ScaleMode = 2 
    PicPrint.ScaleMode = 2 
    PicFrame.BackColor = vbWhite 
    PicPrint.BackColor = vbWhite 
    Printer.ScaleMode = 2 
    Printer.PrintQuality = -4 '最高精度。 '以下中间内容在其他FORM中设置, 应删去: 
    '------------------------------------------------------------- 
    'Cut here.... 
    ReDim TabContent(3, 4) 
    ReDim TabColWidth(4) 
    ReDim TabRowHeight(3) 
    Printer.Font = "system" 
    Printer.FontSize = 14 
    TabX0 = 1.5 
    TabY0 = 1 
    TabTitle = "姓名:asdfASDF 卡号: 。。" 
    TabCol = 4 
    TabCol = 4 
    TabRow = 3 
    For i = 0 To TabRow 
    For j = 0 To TabCol 
    TabContent(i, j) = i + j 
    Next 
    TabRowHeight(i) = 1 
    Next 
    For j = 0 To TabCol 
    TabColWidth(j) = 2 
    Next 
    TabFoot = "出纳员: 。" 
    TabContent(2, 3) = "超长文本超长文本超长文本超长文本" 
    'Cut above 
    '-------------------------------------------------------------- 
    If TabCol > UBound(TabColWidth) Then TabCol = UBound(TabColWidth) 
    If TabRow > UBound(TabRowHeight) Then TabRow = UBound(TabRowHeight) 
    Init 
    ViewTable 
    FormLoaded = True 
    End Sub 
    Private Sub Form_Resize() 
    'If Form1.Width < 7400 Then Form1.Width = 7400 
    'If Form1.Height < 4500 Then Form1.Height = 4500 
    Frame1.Left = 0 
    Frame1.Top = 0 
    Frame1.Width = ScaleWidth 
    VScl.Top = Frame1.Height 
    VScl.Left = ScaleWidth - VScl.Width 
    VScl.Height = ScaleHeight - HScl.Height - Frame1.Height 
    HScl.Top = ScaleHeight - HScl.Height 
    HScl.Width = ScaleWidth - VScl.Width 
    PicFrame.Left = 0 
    PicFrame.Top = Frame1.Height 
    PicFrame.Width = ScaleWidth - VScl.Width 
    PicFrame.Height = ScaleHeight - HScl.Height - Frame1.Height 
    SetSclStatus 
    'cmdPrint(0).Left = ScaleWidth - 500 
    'cmdPrint(1).Left = ScaleWidth - 300 
    End Sub 
    Private Sub SetSclStatus() 
    If PicPrint.Width - PicFrame.ScaleWidth <= 0 Then 
    HScl.Enabled = False 
    MapCurX = 0 
    Else 
    HScl.Enabled = True 
    HScl.Max = PicPrint.Width - PicFrame.ScaleWidth 
    End If 
    If PicPrint.Height - PicFrame.ScaleHeight <= 0 Then 
    VScl.Enabled = False 
    Else 
    VScl.Enabled = True 
    MapcurY = 0 
    VScl.Max = PicPrint.Height - PicFrame.ScaleHeight 
    End If 
    End Sub 
    Private Sub HScl_Change() 
    MapCurX = HScl.Value 
    ScrollMap 
    End Sub 
    Private Sub HScl_Scroll() 
    MapCurX = HScl.Value 
    ScrollMap 
    End Sub 
    Private Sub picPrint_Paint() 
    ViewTable 
    End Sub 
    Private Sub VScl_Change() 
    MapcurY = VScl.Value 
    ScrollMap 
    End Sub 
    Private Sub VScl_Scroll() 
    MapcurY = VScl.Value 
    ScrollMap 
    End Sub 
    Private Sub ScrollMap() 
    PicPrint.Left = -MapCurX 
    PicPrint.Top = -MapcurY 
    End Sub 
    Private Sub Init() 
    Dim i As Integer 
    For i = 0 To TabCol 
    TabColWidth(i) = TabColWidth(i) * 28.35 'Centimeter to point 
    Next 
    For i = 0 To TabRow 
    TabRowHeight(i) = TabRowHeight(i) * 28.35 
    Next 
    TabX0 = TabX0 * 28.35 
    TabY0 = TabY0 * 28.35 
    End Sub 
    ''''''''''''''form1 up................... 
    ''''''''''''''module down'''''''''''''''' 
    Option Explicit 
    Public TabTitle As String, TabContent() As String 
    Public TabFoot As String, TabColWidth() As Single, TabRowHeight() As Single 
    Public TabX0 As Single, TabY0 As Single 
    Public TabCol As Single, TabRow As Single 
    Public printlist 
    'TabTitle 表头(字串) 
    'TabContent(i,j) 表中i行j列文本,可变维 
    'TabFoot 表脚(字串) 
    'TabColWidth(j) 各列宽度,若字串宽度大于设置宽度,则自动设置为字串宽度,单位:cmm, 可不设置. 
    'TabRowHeight(i)各列高度,若字串高度大于设置高度,则自动设置为字串高度,单位:cmm, 可不设置. 
    'TabCol 要打印的列数, TabRow: 要打印的行数, 
    '即可以只打印TabContent(m,n)之中 的TabContent(TabRow, TabCol)部分. 
    'TabXO,TabYO 表左上角在打印纸中的位置,单位:cm 
    ''''''''''''''module up''''''''''''''''''
      

  3.   

    很多谢楼上的。但是,如何打表格我心中有数,不想用作的程序段,我现在遇到的问题中是printer.DrawWidth的设置值无效,无论设为何值,表格线的宽度始终为一个像素的宽度,为什么,如何解决?
      

  4.   

    有人知道吗?为什么在picture控件中表格线预览正常而在printer打印机中却只是一个像素的宽度呢?难道我的打印机不支持printer.DrawWidth属性吗?
      

  5.   

    ScaleMode是缺省值,picture控件与printer对象都是缺省值,但是,DrawWidth属性在指定单位是像素,应该与scalemode的设置无关,就算把printer.DrawWidth设为2000,打印输入也还是一个像素,但在picture中却是一条无比粗的线了。为什么picturebox与printer的表现却不一样呢?