史萊姆論壇

返回   史萊姆論壇 > 專業主討論區 > 程式語言討論區
忘記密碼?
論壇說明

歡迎您來到『史萊姆論壇』 ^___^

您目前正以訪客的身份瀏覽本論壇,訪客所擁有的權限將受到限制,您可以瀏覽本論壇大部份的版區與文章,但您將無法參與任何討論或是使用私人訊息與其他會員交流。若您希望擁有完整的使用權限,請註冊成為我們的一份子,註冊的程序十分簡單、快速,而且最重要的是--註冊是完全免費的!

請點擊這裡:『註冊成為我們的一份子!』

Google 提供的廣告


發文 回覆
 
主題工具 顯示模式
舊 2014-01-27, 07:44 PM   #1
魔術王子
版區管理員
 
魔術王子 的頭像
榮譽勳章
UID - 115097
在線等級: 級別:42 | 在線時長:1984小時 | 升級還需:37小時級別:42 | 在線時長:1984小時 | 升級還需:37小時
註冊日期: 2004-01-13
住址: 魔術學園
文章: 2945
精華: 0
現金: 14765 金幣
資產: 2678395 金幣
預設 程式 - [使用版本VB2008]使用PrintDocument設計通訊錄列印

這個程式其實要搭配資料庫才是最好用的
不然每次要列印前就必須輸入資料,很累人(因為程式沒有儲存功能)
首先是程式畫面:
https://imagizer.imageshack.us/v2/635x486q90/835/s3g8.jpg
這是列印畫面(幸好有電子書(PDF)的軟體,不然要設計這樣的印表格式,不曉得要浪費多少張紙)
https://imagizer.imageshack.us/v2/720x577q90/812/4c2q.jpg
程式元件很簡單:一個工具列(ToolStrip1)放2個ToolStripLabel,2個ToolStripComboBox一個按鈕ToolStripButton
然後一個DataGridView,裡面有3個欄位Column
再來就是列印元件PrintDialog和PrintDocument
引用:
作者: Designer
Private Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
Me.PrintDialog1 = New System.Windows.Forms.PrintDialog
Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument
Me.ToolStrip1 = New System.Windows.Forms.ToolStrip
Me.ToolStripLabel1 = New System.Windows.Forms.ToolStripLabel
Me.ToolStripComboBox1 = New System.Windows.Forms.ToolStripComboBox
Me.ToolStripLabel2 = New System.Windows.Forms.ToolStripLabel
Me.ToolStripComboBox2 = New System.Windows.Forms.ToolStripComboBox
Me.ToolStripButton1 = New System.Windows.Forms.ToolStripButton
Me.DataGridView1 = New System.Windows.Forms.DataGridView
Me.Column1 = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.Column2 = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.Column3 = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.ToolStrip1.SuspendLayout()
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'PrintDialog1
'
Me.PrintDialog1.Document = Me.PrintDocument1
Me.PrintDialog1.UseEXDialog = True
'
'PrintDocument1
'
'
'ToolStrip1
'
Me.ToolStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripLabel1, Me.ToolStripComboBox1, Me.ToolStripLabel2, Me.ToolStripComboBox2, Me.ToolStripButton1})
Me.ToolStrip1.Location = New System.Drawing.Point(0, 0)
Me.ToolStrip1.Name = "ToolStrip1"
Me.ToolStrip1.Size = New System.Drawing.Size(617, 25)
Me.ToolStrip1.TabIndex = 1
Me.ToolStrip1.Text = "ToolStrip1"
'
'ToolStripLabel1
'
Me.ToolStripLabel1.Name = "ToolStripLabel1"
Me.ToolStripLabel1.Size = New System.Drawing.Size(44, 22)
Me.ToolStripLabel1.Text = "印表機"
'
'ToolStripComboBox1
'
Me.ToolStripComboBox1.Name = "ToolStripComboBox1"
Me.ToolStripComboBox1.Size = New System.Drawing.Size(250, 25)
'
'ToolStripLabel2
'
Me.ToolStripLabel2.Name = "ToolStripLabel2"
Me.ToolStripLabel2.Size = New System.Drawing.Size(32, 22)
Me.ToolStripLabel2.Text = "紙匣"
'
'ToolStripComboBox2
'
Me.ToolStripComboBox2.Name = "ToolStripComboBox2"
Me.ToolStripComboBox2.Size = New System.Drawing.Size(121, 25)
'
'ToolStripButton1
'
Me.ToolStripButton1.Image = CType(resources.GetObject("ToolStripButton1.Image"), System.Drawing.Image)
Me.ToolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta
Me.ToolStripButton1.Name = "ToolStripButton1"
Me.ToolStripButton1.Size = New System.Drawing.Size(52, 22)
Me.ToolStripButton1.Text = "列印"
'
'DataGridView1
'
Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Column1, Me.Column2, Me.Column3})
Me.DataGridView1.Dock = System.Windows.Forms.DockStyle.Fill
Me.DataGridView1.Location = New System.Drawing.Point(0, 25)
Me.DataGridView1.Name = "DataGridView1"
Me.DataGridView1.RowTemplate.Height = 24
Me.DataGridView1.Size = New System.Drawing.Size(617, 420)
Me.DataGridView1.TabIndex = 2
'
'Column1
'
Me.Column1.HeaderText = "姓名"
Me.Column1.Name = "Column1"
Me.Column1.Width = 80
'
'Column2
'
Me.Column2.HeaderText = "電話號碼"
Me.Column2.Name = "Column2"
'
'Column3
'
Me.Column3.HeaderText = "住址"
Me.Column3.Name = "Column3"
Me.Column3.Width = 350
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(617, 445)
Me.Controls.Add(Me.DataGridView1)
Me.Controls.Add(Me.ToolStrip1)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
Me.ToolStrip1.ResumeLayout(False)
Me.ToolStrip1.PerformLayout()
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub
Friend WithEvents PrintDialog1 As System.Windows.Forms.PrintDialog
Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
Friend WithEvents ToolStrip1 As System.Windows.Forms.ToolStrip
Friend WithEvents ToolStripLabel1 As System.Windows.Forms.ToolStripLabel
Friend WithEvents ToolStripComboBox1 As System.Windows.Forms.ToolStripComboBox
Friend WithEvents ToolStripLabel2 As System.Windows.Forms.ToolStripLabel
Friend WithEvents ToolStripComboBox2 As System.Windows.Forms.ToolStripComboBox
Friend WithEvents ToolStripButton1 As System.Windows.Forms.ToolStripButton
Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
Friend WithEvents Column1 As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Column2 As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Column3 As System.Windows.Forms.DataGridViewTextBoxColumn
引用:
作者: Form1程式碼
Public Class Form1
Dim Rec_Count As Integer
Dim p_no As Single

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Single
ToolStripComboBox1.Items.Clear()
For i = 0 To Printing.PrinterSettings.InstalledPrinters.Count - 1
ToolStripComboBox1.Items.Add(Printing.PrinterSettings.InstalledPrinters.Item(i).ToString)
Next
ToolStripComboBox1.Text = PrintDialog1.PrinterSettings.PrinterName
'以下只是為了測試方便,可以省略
Randomize()
Dim n As Single, f As Single
For i = 0 To 100
Dim s As String = "00" & Format(i, "##")
n = 1 + Int(Rnd(1) * 500)
f = 1 + Int(Rnd(1) * 101)
DataGridView1.Rows.Add("阿哉" & s.Substring(s.Length - 2, 2), "00-1234" & 567 + f, "賣燜挖縣麥擱燜挖鄉賣歌債燜挖鎮" & n & "號" & f & "樓")
Next
End Sub

Private Sub ToolStripComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged
Dim i As Single, k As Single = 0
PrintDialog1.PrinterSettings.PrinterName = ToolStripComboBox1.Text
ToolStripComboBox2.Items.Clear()
For i = 0 To PrintDialog1.PrinterSettings.PaperSources.Count - 1
'ToolStripComboBox2.Items.Add(PrintDialog1.PrinterSettings.PaperSources.Item(i).SourceName)
ToolStripComboBox2.Items.Add(PrintDialog1.PrinterSettings.PaperSizes.Item(i).PaperName)
If Mid(PrintDialog1.PrinterSettings.PaperSizes.Item(i).PaperName, 1, 2) = "A4" Then k = i
Next
ToolStripComboBox2.SelectedIndex = k
PrintDocument1.PrinterSettings.PrinterName = ToolStripComboBox1.Text
End Sub

Private Sub ToolStripComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripComboBox2.SelectedIndexChanged
PrintDocument1.DefaultPageSettings.PaperSize = PrintDialog1.PrinterSettings.PaperSizes.Item(ToolStripComboBox2.SelectedIndex)
End Sub

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Rec_Count = 0
p_no = 0
PrintDocument1.PrinterSettings.Copies = 1
PrintDocument1.DocumentName = "通訊錄"
PrintDocument1.PrinterSettings.PrinterName = ToolStripComboBox1.Text
PrintDocument1.DefaultPageSettings.PaperSource = PrintDialog1.PrinterSettings.PaperSources.Item(ToolStripComboBox2.SelectedIndex)
PrintDocument1.DefaultPageSettings.Landscape = True
PrintDocument1.Print()
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim blackPen As New Pen(Color.Black, 0.2)
Dim inSidePen As New Pen(Color.Black, 0.6)
Dim tPos As Single = 40
Dim lPos As Single = 3
Dim BoxH = 25 + (30 * 24)
Dim i As Single, j As Single, x As Single, y As Single, k As Single, s As String, s1 As String
Dim d1 As DateTime = Now.Date
Dim drawFormat As New StringFormat
p_no += 1
d1 = Now.Date
e.Graphics.DrawString("印 表 : " & Format(d1.Year - 1911, "000") & "年" & Format(d1.Month, "00") & "月" & Format(d1.Day, "00") & "日", New System.Drawing.Font("新細明體", 8), Brushes.Black, e.PageBounds.Width - 200, 5, drawFormat)
e.Graphics.DrawString("頁 數 : " & p_no, New System.Drawing.Font("新細明體", 8), Brushes.Black, e.PageBounds.Width - 200, 16, drawFormat)
Dim pf As Font = New System.Drawing.Font("新細明體", 8)
x = lPos
y = tPos
drawFormat.Alignment = StringAlignment.Center
e.Graphics.DrawString("通訊錄", New System.Drawing.Font("標楷體", 15), Brushes.Black, e.PageBounds.Width / 2, 3, drawFormat)
e.Graphics.DrawRectangle(inSidePen, lPos, tPos, e.PageBounds.Width - 45, BoxH + 2)
e.Graphics.DrawRectangle(blackPen, lPos + 1, tPos + 1, e.PageBounds.Width - 47, BoxH)
e.Graphics.DrawLine(blackPen, lPos + 1, y + 23, lPos + e.PageBounds.Width - 46, y + 22)
e.Graphics.DrawString("NO.", pf, Brushes.Black, x + 10, y + 8, drawFormat)
e.Graphics.DrawLine(blackPen, x + 20, y + 1, x + 20, y + 1 + BoxH)
e.Graphics.DrawString("姓名", pf, Brushes.Black, x + 47, y + 8, drawFormat)
e.Graphics.DrawLine(blackPen, x + 75, y + 1, x + 75, y + 1 + BoxH)
e.Graphics.DrawString("電話號碼", pf, Brushes.Black, x + 111, y + 8, drawFormat)
e.Graphics.DrawLine(blackPen, x + 147, y + 1, x + 147, y + 1 + BoxH)
e.Graphics.DrawString("住 址", pf, Brushes.Black, x + 170, y + 8, drawFormat)
tPos = tPos + 25
For k = 0 To 29
y = tPos + k * 24
e.Graphics.DrawString(k + (p_no - 1) * 30 + 1, pf, Brushes.Black, x + 10, y + 6, drawFormat)
e.Graphics.DrawLine(blackPen, lPos + 1, y, lPos + e.PageBounds.Width - 46, y)
Next
drawFormat.Alignment = StringAlignment.Near
For i = 0 To 29
drawFormat.Alignment = StringAlignment.Far
y = tPos + i * 24
e.Graphics.DrawString(DataGridView1.Rows(Rec_Count).Cells(0).Value, pf, Brushes.Black, x + 23, y + 6, New StringFormat)
e.Graphics.DrawString(DataGridView1.Rows(Rec_Count).Cells(1).Value, pf, Brushes.Black, x + 78, y + 6, New StringFormat)
e.Graphics.DrawString(DataGridView1.Rows(Rec_Count).Cells(2).Value, pf, Brushes.Black, x + 150, y + 6, New StringFormat)
If Rec_Count < DataGridView1.Rows.Count - 1 Then
e.HasMorePages = True
Else
e.HasMorePages = False
Exit For
End If
Rec_Count += 1
Next
End Sub
End Class
如果要使用列印功能就必須用到PrintDialog和PrintDocument
PrintDialog是抓取及設定印表機狀態
PrintDocument就是規劃列印內容
魔術王子 目前離線  
送花文章: 1523, 收花文章: 1553 篇, 收花: 3892 次
回覆時引用此帖
發文 回覆



發表規則
不可以發文
不可以回覆主題
不可以上傳附加檔案
不可以編輯您的文章

論壇啟用 BB 語法
論壇啟用 表情符號
論壇啟用 [IMG] 語法
論壇禁用 HTML 語法
Trackbacks are 禁用
Pingbacks are 禁用
Refbacks are 禁用

相似的主題
主題 主題作者 討論區 回覆 最後發表
程式 - [使用版本VB2008]使用DataGridView元件設計菜單 魔術王子 程式語言討論區 0 2014-01-24 10:20 PM
程式 - [使用版本VB2008]ListView元件及Panel元件 魔術王子 程式語言討論區 0 2014-01-23 08:25 PM
程式 - [使用版本VB2008]程式設定選單 魔術王子 程式語言討論區 0 2014-01-22 06:19 AM
程式 - [使用版本VB2008]DataGridView元件不連接資料庫使用 魔術王子 程式語言討論區 1 2014-01-21 09:50 PM


所有時間均為台北時間。現在的時間是 12:46 PM


Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2024, Jelsoft Enterprises Ltd.


SEO by vBSEO 3.6.1