top
Loading...
解決VB中的Grid控件的打印問題
rid控件是VisualBasic最常見控件之一,從VB3.0到VB5.0都有該控件。也是VB愛好者最 喜愛的工具之一。用它可以以表格的形式顯示、瀏覽數據,特別是數據庫應用,直接綁定即可顯 示瀏覽數據庫信息。然而,美中不足的是Grid沒有編輯和打印功能,列與列的位置不能相互交 換。筆者曾嘗試著給Grid增添了這些功能,使之錦上添花,功能更強大。下面給出改進方法及源 程序,讀者只需按步驟寫下源程序即可使你的Grid具有打印功能。該程序筆者在HP5/100Window 95環境下用VB5.0調試通過。

給Grid控件增加打印方法有三種:1是直接打印控件的方法,2 是過printer來實現打印功能,3是通過調用MS-WORD及MS-EXCEl來實現打印。
首先,打開一個應用,在FORM1中增加DATA控件DATA1,把DATA1的CONNECT屬性設為dBASEIII, 再把DATABASENAME屬性設為D:PJXM.DBF。然后再在FORM1中增加MSFLEXGRID空間GRID1,并把GRID1的DATASOURCE 屬性設為DATA1。這樣數據庫PJXM.DBF的信息就會在GRID1中顯示出來。

方法一:直 接打印窗體法,在FORM1中增加命令按鈕(command),CAPTION屬性設為直接打印,再寫入下列 編碼:

Subcommand_click
Form1.printform
Endsub

這樣即可通過打印窗體FORM1的方法把GRID1的數據打印出來,遺憾的是只 能打印GRID1中顯示的數據部分,顯示不出來的則無法打印,而且這種打印方法很象屏幕硬拷 貝把其他控件也打印出來。也不能靈活的控制字體等。

方法二:通過PRINTER實現打印。 這種方法

1、加入打印命令按鈕(command1)、函數(prnt1)即可實現打印功能,寫入 下面代碼,讀者稍加改動可寫成標準的函數或過程。

Functionprnt1(xAsInteger, yAsInteger,
fontAsSingle,txtAsString)
printer.CurrentX=x
printer.CurrentY=y
printer.FontBold=False
printer.FontSize=font
printer.Printtxt
EndFunction

Subcommand1_click
DimfntAsSingle
Dimppasinteger
Pp=0設置開始頁碼0
Dimstry,strx,strx1,stry1,linw,page1,pAsInteger
Statica(8)AsInteger定義打印的列數
ss$="內部結算存入款對帳單"定義表頭
kan=0
Fori=0To8
a(i)=1500定義每列寬
kan=kan+a(i)計算表格總寬度
Next

page1=50定義每頁行數
strx=200
strx1=200定義X方向起始位置
stry=1400
stry1=1400定義Y方向起始位置
linw 0定義行寬
fnt=8定義字體大小
printer.fontname="宋體"定義字體

dd=prnt1(4000,700,18,ss$)打印標題
printer.Line(strx-50,stry-30)
-(strx+kan-10,stry-30)
Forj=0Togridrow-1gridrow為所要打印的行數
grid1.row=j
strx=strx1
printer.Line(strx-50,stry-30)
-(strx+kan-10,stry-30)
p=p+1
Fori=0To8
grid1.col=i
dd=prnt1(strx,stry,fnt,grid1.text)
strx=strx+a(i)
Next

Ifp$#@62;page1Thennextpage
p=0
strx=strx1
linelastline
printer.Line(strx-50,stry+linw)
-(strx+kan-10,stry+linw)
stry=stry1
linecol
Forn=0To8
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.NewPagenextpage
dd=prnt1(4000,700,18,ss$)打印標題
strx=strx1
stry=stry1
printer.Line(strx-50,stry-30)-
(strx+kan-10,stry-30)printfirstrow
Else
stry=stry+linw
EndIf
Next
st=stry
Ifp$#@60;page1Then在最后頁剩余劃空行
Foro=pTopage1+1
strx=strx1
printer.Line(strx-50,stry-30)
-(strx+kan-10,stry-30)
stry=stry+linw
Next
EndIf
stry=stry1
strx=strx1
stry=stry1linecol
Forn=0To8
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打印結束
Endsub

這種方法通過靈活的編程可以方便地調整字體、字型、線形、頁面、紙張大小 等。可打印出比較滿意的效果。如果你的計算機上裝有MICROSOFTWORD和MICROEXCEL,最精彩 的用法還是把GRID的表格通過VB發送到MICROSOFTWORD及MICROEXCEL。生成MICROSOFTWORD 和MICROEXCEL表格。這樣就可以充分利用MICROSOFTWORD和MICROEXCEL的打印、編輯功能 打印出更理想的效果。下面逐一介紹。

方法三:通過生成MICROSOFTWORD表格打印

1、在declaration中寫入:

DimmswordAsObject

2、加入打印命令按 鈕(command2),CAPTION設為"生成WORD表格",寫入下面代碼,

PrivateSub command2_Click()

screen.MousePointer=11
Setmsword=CreateObject("word.basic")

DimAppID,ReturnValue
appID=Shell("d:office97officeWINWORD.EXE",1)
RunMicrosoftWord.

msword.AppActivate"MicrosoftWord"
msword.AppActivate"MicrosoftWord",1
full
Screen.MousePointer=0
EndSub

2、寫入以下過程full()

Subfull()
DimiAsInteger,jAsInteger,
colAsInteger,rowAsInteger
DimcellcontentAsString
Me.Hide
cols=4表格的列數
row=gridrow打印表的行數
msword.filenewdefault
msword.MsgBox"正在建立MS_WORD報表,
請稍候.......","",-1
msword.leftpara
msword.screenupdating0
msword.tableinserttable,col,row,,,16,167
msword.startofdocument
forj=0togridrow表格的行數
grid1.row=j
Fori=1Tocols
Gri1d.col=i
IfIsNull(grid1.text)Then
cellcontent$=""
Else
cellcontent$=grid1.text
EndIf
msword.Insertcellcontent$
msword.nextcell
Nexti
Nextj
msword.tabledeleterow
msword.startofdocument
msword.tableselectrow
msword.tableheadings1
msword.centerpara
msword.startdocument
msword.screenrefresh
msword.screenupdating1
msword.MsgBox"結束","",-1
Me.Show

EndSub

方法四:通過發送到MICROSOFTEXCEL實現表格打印

1、加入打印命令按鈕(command3),CAPTION設為"生成EXCEL表格",寫入下面代 碼

PrivateSubcommand3_Click()
DimiAsInteger
DimjAsInteger
DimxlAppAsExcel.Application
DimxlBookAsExcel.Workbook
DimxlSheetAsExcel.Worksheet

SetxlApp=CreateObject("Excel.Application")
xlApp.Visible=True
SetxlBook=xlApp.Workbooks.Add
OnErrorResumeNext
SetxlBook=xlApp.Workbooks.AddOpen("d:ext2.xls")
SetxlSheet=xlBook.Worksheets(1)
xlSheet.Cells(6,1)="i"
Fori=0Togridrow
grid1.Row=i
Forj=0To6
Grid1.Col=j

IfIsNull(Grid1.Text)=FalseThen
xlSheet.Cells(i+5,j+1)=Grid1.Text
EndIf
Nextj
Nexti
ExitSub

作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗