• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    VB中Grid控件的打印方法

    發布: 2007-7-14 20:28 | 作者: 佚名    | 來源: 網絡轉載     | 查看: 16次 | 進入軟件測試論壇討論

    領測軟件測試網 Grid控件是Visual Basic(以下簡稱VB)最常見控件之一,用它可 以以表格的形式顯示、瀏覽數據。特別是在數據庫應用中,直接綁定 即可顯示和瀏覽數據庫信息。然而,美中不足的是Grid沒有編輯和打 印功能,列與列的位置不能相互交換。筆者曾嘗試著給Grid增添了這 些功能,使之功能更強大。下面給出改進方法及源程序,讀者可參考使 用。該程序在HP5/100、Window 95環境下用VB5.0調試通過。
      給Grid 控件增加打印方法有三種:一是直接打印控件,二是通過p rinter 來實現打印功能,三是通過調用MS-Word及MS-Excel 來實現打 印。
      首先,打開一個應用,在FORM1中增加DATA控件DATA1,把DATA1的CO NNECT屬性設為dBA SEⅢ,再把DATABASENAME屬性設為D:\PJXM.DBF。 然后再在FORM1中增加MSFLEXGRID控件GRID1,并把GRID1的DATASOURCE 屬性設為DATA1。這樣數據庫PJXM.DBF的信息就會在GRID1 中顯示出 來。下面分析介紹各種打印方法。
      方法一:直接打印窗體
      在FORM1中增加命令按鈕(command),CAPTION屬性設為直接打印, 再寫入下列代碼:
      Sub command_click
      Form1.printform
      End sub
      這樣即可通過打印窗體FORM1的方法把GRID1的數據打印出來。這 種方法的缺點是只能打印GRID1中顯示的數據部分,顯示不出來的則無 法打印,而且這種打印方法很像屏幕硬拷貝,而且不能靈活地控制字體 等。
      方法二:通過PRINTER實現打印
      1. 加入打印命令按鈕(command1)和函數(prnt1),寫入下面代碼, 讀者稍加改動可寫成標準的函數或過程。Function prnt1 (x As Int eger, y As Integer, font As Sing le, txt As String)
      printer.CurrentX = x
      printer.CurrentY = y
      printer.FontBold = False
      printer.FontSize = font
      printer.Print txt End Function Sub command1_click
      Dim fnt As Single
      Dim pp as integer
      Pp=0 '設置開始頁碼0
      Dim stry,strx,strx1,stry1,linw,page1,p As Integer
      Static a(8) As Integer '定義打印的列數
      ss$ = "內部結算存入款對帳單" '定義表頭
      kan = 0
      For i = 0 To 8
      a(i) = 1500' 定義每列寬
      kan = kan + a(i) '計算表格總寬度
      Next
      page1 = 50 ’定義每頁行數
      strx = 200
      strx1 = 200 ’定義X方向起始位置
      stry = 1400
      stry1 = 1400 ’定義Y方向起始位置
      linw = 240 ’定義行寬
      fnt = 8 ’定義字體大小
      printer.fontname = "宋體" ’定義字體
      dd = prnt1(4000, 700, 18, ss$) ’打印標題
      printer.Line (strx - 50, stry - 30)-(strx +
       kan - 10, stry - 30)
      For j = 0 To gridrow - 1
       ’gridrow為所要打印的行數
       grid1.row = j
       strx = strx1
       printer.Line (strx - 50, stry - 30)-(strx +
      kan - 10, stry - 30)
       p = p + 1
       For i = 0 To 8
       grid1.col = i
       dd = prnt1(strx, stry, fnt, grid1.text)
       strx = strx + a(i)
       Next
      If p > page1 Then ’next page
       p = 0
       strx = strx1
       printer.Line (strx - 50, stry + linw)-(strx + kan - 10, stry + linw)
       stry = stry1
       For n = 0 To 8
       printer.Line (strx - 30, stry - 30)-(strx
       - 30, stry + (page1 + 2) * linw)
       strx = strx + a(n)
       Next
       printer.Line (strx - 30, stry - 30)-(strx -
      30, stry + (page1 + 2) * linw)
       pp=pp+1
       foot$="第 "+cstr(pp)+" 頁"
       dd = prnt1(strx - 30-1000, stry + (page1 +
      2) * linw+100, 10, foot$) ’打印頁腳碼
       printer.NewPage ’next page
       dd = prnt1(4000, 700, 18, ss$) ’打印標題
       strx = strx1
       stry = stry1
       printer.Line (strx - 50, stry - 30)-(strx +
      kan - 10, stry - 30) ’打印第一行
      Else
       stry = stry + linw
      End If Next
       st = stry
      If p < page1 Then ’在最后頁剩余劃空行
      For o = p To page1 + 1
       strx = strx1
       printer.Line (strx - 50, stry - 30)-(strx +
      kan - 10, stry - 30)
       stry = stry + linw
      Next
      End If
      stry = stry1
      strx = strx1
      stry = stry1’line col
      For n = 0 To 8
       printer.Line (strx - 30, stry - 30)-(strx -
      30, stry + (page1 + 2) * linw)
       strx = strx + a(n)
      Next
      printer.Line (strx - 30, stry - 30)-(strx -
      30, stry + (page1 + 2) * linw)
       pp=pp+1
       foot$="第 "+cstr(pp)+" 頁"
       dd = prnt1(strx - 30-1000, stry + (page1 +
      2) * linw+100, 10, foot$) ’打印頁腳碼
       printer.EndDoc ’打印結束
      End sub
      這種方法通過靈活的編程可以方便地調整字體、字型、線形、頁 面、紙張大小等,可打印出比較滿意的效果。
      如果計算機上裝有Microsoft Word和Microsoft Excel,則可把GR ID的表格通過VB發送到這些應用程序中去,生成Microsoft Word和Mic rosoft Excel表格。這樣可以充分利用它們的打印、編輯功能,打印 出更理想的效果。下面逐一介紹。
      方法三:通過生成Word表格打印
      1. 在declaration 中寫入:
      Dim msword As Object
      2. 加入打印命令按鈕(command2),CAPTION設為"生成Word表格", 寫入下面代碼:Pri vate Sub command2_Click()
      screen.MousePointer = 11
      Set msword = CreateObject("word.basic")
      Dim AppID, ReturnValue
      appID = Shell("d:\office97\office\WIN-
      WORD.EXE", 1)  ’運行Word
      msword.AppActivate "Microsoft Word"
      full
      Screen.MousePointer = 0 End Sub
      3. 寫入以下過程full() Sub full()
      Dim i As Integer, j As Integer, col As Integer,
      row As Integer
      Dim cellcontent As String
      Me.Hide
      cols = 4 ’表格的列數
      row = gridrow ’打印表的行數
      msword.filenewdefault
      msword.MsgBox "正在建立MS_WORD報表,請稍候
      ……", "", -1
      msword.leftpara
      msword.screenupdating 0
      msword.tableinserttable , col, row, , , 16, 167
      msword.startofdocument
      for j=0 to gridrow ’表格的行數
       grid1.row=j
       For i = 1 To cols
       Gri1d.col=i
        If IsNull(grid1.text) Then
       cellcontent$ = ""
        Else
       cellcontent$ = grid1.text
        End If
        msword.Insert cellcontent$
        msword.nextcell
       Next i
      Next j
      msword.tabledeleterow
      msword.startofdocument
      msword.tableselectrow
      msword.tableheadings 1
      msword.centerpara
      ’msword.startdocument
      msword.screenrefresh
      msword.screenupdating 1
      msword.MsgBox "結束", "", -1
      Me.Show End Sub
      方法四:通過發送到Excel實現表格打印
      1. 加入打印命令按鈕(command3),CAPTION設為"生成EXCEL表格" ,寫入下面代碼: Private Sub command3_Click()
      Dim i As Integer
      Dim j As Integer
      Dim xlApp As Excel.Application
      Dim xlBook As Excel.Workbook
      Dim xlSheet As Excel.Worksheet
      Set xlApp = CreateObject("Excel.Application")
      xlApp.Visible = True
      Set xlBook = xlApp.Workbooks.Add
      Set xlSheet = xlBook.Worksheets(1)
      xlSheet.Cells(6, 1) = "i"
      For i = 0 To gridrow
       grid1.Row = i
       For j = 0 To 6
       Grid1.Col = j
       If IsNull(Grid1.Text) = False Then
      xlSheet.Cells(i + 5, j + 1) = Grid1.Text
       End If
       Next j
      Next i End Sub

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>