跳轉到內容

從 Zip/列印中程式設計 Gambas

來自華夏公益教科書

在練習列印時,“列印到檔案”將節省紙張。您可以使用喜歡的 PDF 閱讀器(如 Okular)開啟生成的 PDF(行動式文件格式)檔案,並在螢幕上檢視您將在紙上得到的內容。

這是關於列印的最簡單的演示。在您的程式中,您需要一個“印表機”。我們使用過按鈕和表格檢視等物件。您可以看到它們。但是,印表機是不可見的。有一個印表機類,就像有一個按鈕類一樣。您將印表機拖放到表單上,就像拖放按鈕或任何其他物件一樣。在表單上,它看起來像一臺印表機,但在程式執行時,它不可見。它實際上只是一塊程式碼,內置於 Gambas 中,並執行印表機應該做的事情,即列印並照顧頁面大小和方向等。Printer 是一個聰明的物件。

首先,您告訴您的 Printer 物件配置,然後您告訴它列印。(“印表機,列印!”,或者正如我們在 Gambas 中寫的那樣,prn1.print)。當您告訴它列印時,它將發出 Draw 事件。在 draw 事件中,您放置要在頁面上列印的內容。您使用另一個類(Paint 類)的所有功能來執行此操作。Paint 類可以將內容放置在頁面上以進行列印,但它還有其他用途,例如在表單上的 DrawingArea 或 ScrollArea 中繪製。好了,我們開始了!

列印一些純文字

[編輯 | 編輯原始碼]

Printer object on a Gambas form Printer object on a Gambas form, running

Gambas printed page

此小型表單包含一個 Printer 物件和一個名為 bPlainPrint 的按鈕。

Public SimpleText As String

Public Sub Form_Open()

  SimpleText = "Countries of the World<br><br>Papua New Guinea<br><br>Papua New Guinea is a country that is north of Australia. It has much green rainforest. It has beautiful blue seas. Its capital, located along its southeastern coast, is Port Moresby.<br><br>This is plain text in Linux Libertine 12 point.<br><br>John Smith, editor"

End

Public Sub pr1_Draw()
  Paint.Font = Font["Linux Libertine,12"]
  Paint.DrawRichText(SimpleText, 960, 960, Paint.Width - 2 * 960)
End

Public Sub bPrintPlain_Click()
  If pr1.Configure() Then Return 'if user clicks Cancel, don't continue
  pr1.Print
End

當表單開啟時,一些文字被放入名為 SimpleText 的變數中以供列印。

單擊按鈕時,會告訴印表機 pr1 自行配置。如果使用者單擊 取消 按鈕,這將返回 True 值,因此我們不應再做任何事情。否則,親愛的友好印表機物件,請列印。

印表機物件 pr1 向我們傳送 Draw 事件。它在說,“我想畫點東西!請告訴我要在頁面上畫什麼!”。我們透過說:

Paint.Font = Font["Linux Libertine,12"]
Paint.DrawRichText(SimpleText, 960, 960, Paint.Width - 2 * 960)

Paint.Font 是描述字型的屬性。它是一個包含部分的屬性。我們使用 Font[something] 來組裝這些部分。Something 是一個字串。例如,Font["Bitstream Vera Serif,Bold,24"] 表示“組裝一個字型,它是 Bitstream Vera Serif,粗體,24 點”。這被放在 Paint 東西的 Font 屬性中。實際上,Paint 東西只是一組技能。它不是你可以看到的任何東西。它是一個不可見的類。小心不要在該字串中放置空格,除非它是字型名稱的一部分。 Gambas 幫助 會警告您這一點。在逗號兩側也不要放置空格!

Paint.DrawRichText(something)paint 的技能之一。它是一種它知道如何做的方法。它至少需要方括號中的三件事。它可以接受更多。這裡我們有四個“引數”或“方括號中的東西”。第一項:要列印的內容。第二項:開始列印的橫向距離。第三項:開始列印的縱向距離。960 將提供一英寸的邊距。每英寸 96 點是典型的預設印表機解析度。數字是“十分之一點”。(我希望我理解正確。)第四項:我的列印要多寬?答案:Paint 允許的全部寬度,減去左側和右側一英寸。每英寸是 960。減去兩個。

<br> 表示“換行”,這將轉到下一行。<br><br> 表示轉到新行,然後再次轉到新行。它給了我們一個空行。

富文字理解 <br>。它還理解在文字中植入的許多其他符號。有一些符號可以使其使用“標題 1”樣式列印,“標題 2”等等。但是,您不能更改這些樣式的外觀。它們是內建的,就是這樣。您還可以在文字中的任何位置更改字型、列印大小和顏色。這些程式碼使列印以某種方式呈現。實際上,它本身就是一種語言:超文字標記語言,或 HTML。例如,要開啟 粗體,請放入此標籤:<b>。當您想關閉 粗體 時,請放入此標籤:</b>

列印富文字(包含 HTML 標籤)

[編輯 | 編輯原始碼]

不要使用 PlainText,而是讓它列印:

FancyText = "<h3>世界各國</h3><Small>巴布亞紐幾內亞</Small><hr><br>巴布亞紐幾內亞是一個位於<font color = #780373><b>澳大利亞</b>以北的國家。</font><font color = black>它擁有豐富的</font><font color = green>綠色雨林</font><font color = black>。它擁有美麗的<font color = blue>藍色海洋</font><font color = black>。它的首都位於東南海岸,是<Font Face=Times New Roman, Color=#FF0000>莫爾斯比港</font>。<br><br>這是用<font face = Arial>HTML<br></font><p align = right><font face = Times New Roman, Size=+1><b>約翰·史密斯</b></font>,<i>編輯</i></p>"

順便說一句,如果將該文字儲存在副檔名為 .html 的文字檔案中,它將在 Web 瀏覽器(如 FireFox)中開啟並顯示。您可以試試。

結果將是

Gambas Rich-text Printing Example


我使用了標題 3(<h3> … </h3>),因為標題 1 太大了。

文字中有很多標籤,使它看起來像那樣。Gambas 允許使用這些標籤。它只是從完整的 HTML 中選擇一小部分。在您的文字處理器中儲存一個 HTML 文件,在像 Kate 這樣的文字編輯器中開啟它,並驚歎不已。

可以在富文字中使用的標籤

[編輯 | 編輯原始碼]
<h1>,<h2>,<h3>,<h4>,<h5>,<h6> → 標題 <sup> → 上標
<b> → 粗體字型 <small> → 小字型
<i> → 斜體 <p> → 段落
<s> → 刪除線 <br> → 換行
<u> → 下劃線 <a> → 連結
<sub> → 下標 <font> → 字型

更改文字顏色和對齊段落的示例

[編輯 | 編輯原始碼]
<Font Color=#B22222> ... </Font> <p align=right> ... </p>
[編輯 | 編輯原始碼]

Gambas form with image and printer object Gambas program with image and printer object, running

Print.configure dialog
Print.Configure( )

An image you have located for Gambas Image property in Gambas

將圖片檔案拖放到 Data 資料夾中。將 PictureBox 的 Picture 屬性設定為它。

印表機名為 pr1

Public Pic As Picture

Public Sub pr1_Draw()
  Paint.DrawPicture(Pic, 960, 960, 3000, 3000)
End

Public Sub bPrint_Click()
  If pr1.Configure() Then Return 'if user clicks Cancel, don't continue
  Pic = PictureBox1.Picture
  pr1.Print
End

圖片被縮放為 3000 x 3000。當我列印到檔案時,解析度為每英寸 96 點(96 dpi)。圖片在距頂部和左側邊距 1 英寸的位置列印,並被縮放以適合大約 3 英寸 x 3 英寸(3000x3000)。

Gambas-printed page with image

[編輯 | 編輯原始碼]

Sample Class List to be Printed

在此程式中,40 個姓名被髮明並放入名為 Z[] 的陣列中。如果您是認真的,可以使使用者輸入姓名列表,或從姓名檔案載入,或從資料庫中獲取。

姓名將垂直列印在頁面上。需要留出邊距,這裡設定為一英寸(列印到 PDF 時為 960 個點)。它儲存在變數(窗體的私有屬性)SideMargin 中。左右邊距相同。上邊距為 TopMargin

列印姓名時,在列印下一個姓名之前要向下移動多少距離?LineSpacing 設定為 280。這大約相當於 0.3 英寸。(960 是 1 英寸)。

計劃如下:列印一個姓名。無論姓名有多長,都向前移動一點。這是水平線的起點。線長為頁面寬度減去右邊距。繪製這條線。向下移動一行間距。列印下一個姓名。繪製它的線。向下移動。列印一個姓名。繪製它的線,以此類推。

然後繪製垂直線以製作方框。從最長名稱寬度右側一點開始。移動 330 個點,繪製一條垂直線,再移動 330 個點,繪製下一條線,以此類推。不要超過水平線的終點。最後,為了使右側邊緣整齊,繪製最後一條垂直線。印表機稱為 Prn。按鈕為 bPrint

Gambas form with Print Class List button Gambas form with Print Class List button

Class List printed with Gambas

Private z As New String[]
Private LineSpacing As Integer = 280
Private TopMargin As Integer = 960
Private SideMargin As Integer = 960

Public Sub Prn_Draw()

  Dim s As String
  Dim i As Integer
  Dim NameWidth, HowFarDown, MaxNameWidth, MaxDistanceDown As Float
  Dim MaxWidth As Float = Paint.Width - 2 * SideMargin

  Paint.Font = Font["Linux Libertine,12"]
  Paint.Color(Color.Black)
  Paint.MoveTo(SideMargin, TopMargin) 'start here
  Paint.LineTo(Paint.Width - SideMargin, TopMargin) 'draw to here
  Paint.Stroke 'paint the top line
  For i = 0 To z.Max
    s = z[i]
    NameWidth = Paint.TextExtents(s).Width + 180 'gap at the end about 1/5 inch
    MaxNameWidth = Max(MaxNameWidth, NameWidth) 'remember the width of the longest name
    HowFarDown = TopMargin + (LineSpacing * (i + 1))
    Paint.DrawText(s, SideMargin, HowFarDown)
    Paint.MoveTo(SideMargin + NameWidth, HowFarDown) 'starting position
    Paint.LineTo(Paint.Width - SideMargin, HowFarDown) 'finishing position
    Paint.Stroke 'draw the line
  Next
  MaxDistanceDown = TopMargin + z.Count * LineSpacing 'vertical lines go down to here
  For i = SideMargin + MaxNameWidth + 100 To Paint.Width - SideMargin Step 330 'step across the page every 1/3 inch
    Paint.MoveTo(i, TopMargin)
    Paint.LineTo(i, MaxDistanceDown)
    Paint.Stroke
  Next
  Paint.MoveTo(Paint.Width - SideMargin, TopMargin)
  Paint.LineTo(Paint.Width - SideMargin, MaxDistanceDown)
  Paint.Stroke 'final line on right

End

Public Sub GetNames()

  Dim FN As String[] = ["Oliver", "Jack", "Harry", "Jacob", "Charlie", "Thomas", "George", "Oscar", "James", "William", "Amelia", "Olivia", "Isla", "Emily", "Poppy", "Ava", "Isabella", "Jessica", "Lily", "Sophie"]
  Dim SN As String[] = ["Smith", "Jones", "Williams", "Brown", "Wilson", "Taylor", "Moreton", "White", "Martin", "Anderson", "Johnson", "Walsh", "Miller", "Davis", "Burns", "Murphy", "Lee", "Roberts", "Singh", "Evans"]

  FN.Shuffle
  SN.Shuffle
  Dim i, n As Integer
  For i = 1 To 40
    z.Add(FN[n] & " " & SN[n])
    n += 1
    If n > FN.Max Then
      FN.Shuffle
      SN.Shuffle
      n = 0
    Endif
  Next

End

Public Sub bPrint_Click()

  Prn.OutputFile = User.Home &/ "Names.pdf" 'I'm printing to a pdf file
  If Prn.Configure() Then Return
  GetNames
  Prn.Print()

End
[編輯 | 編輯原始碼]

Gambas form for printing a calendar page Gambas program for printing a calendar page, running

Gambas being told where the photo is

Gambas-printed Calendar Page

該窗體包含一個名為 PictureBox1 的圖片框、一個名為 Prn 的印表機以及兩個名為 bPicturebPrint 的按鈕。

該程式列印當前月份的日曆。當您檢視要列印的頁面時,您會看到需要在不同位置列印的“內容”。有三個需要重複的內容:方框、每個方框左上角的數字以及一週中各天的名稱。

我為 PictureBox1 設定了一個預設圖片(程式執行後立即顯示)。首先,我將照片拖到 Data 資料夾中。然後,我將圖片框的 Picture 屬性設定為該照片。

如果您最初沒有圖片,使用者需要在點選 列印 按鈕之前點選 選擇圖片... 按鈕。圖片儲存在一個名為 Pic 的屬性中。如果它為空,列印將不會繼續。

Public Pic As Picture

Public Sub LoadPicture()

  Dim Path As String

  Dialog.Title = "Please Select a picture"
  Dialog.Filter = ["*.jpg", "*.png", "Image Files", "*", "All files"]
  Dialog.Path = User.Home
  If Dialog.OpenFile() Then Return
  Pic = Picture.Load(Dialog.Path)
  PictureBox1.Picture = Pic

End

Public Sub bPicture_Click()
  LoadPicture
  FMain.SetFocus
End

Public Sub bPrint_Click()

  Pic = PictureBox1.Picture 'This line can be deleted if you don't give your PictureBox a default picture.
  If IsNull(Pic) Then
    Message("Please select a photo first.")
  Else
    Prn.OutputFile = User.Home &/ "Calendar.pdf"
    If Prn.Configure() Then Return
    Prn.Print
  Endif

End

Public Sub Prn_Draw()

  Dim LeftMargin As Float = 480 'half inch
  Dim TopMargin As Float = 1200 'inch and a bit
  Dim row, col, DayNum, CellNum As Integer
  Dim s As String
  Dim ThisMonth As Integer = Month(Date(Now)) 'the month number of the date part of the NOW function; 1 to 12
  Dim ThisYear As Integer = Year(Date(Now)) 'current year
  Dim FirstOfMonth As Date = Date(ThisYear, ThisMonth, 1)
  Dim StartDay As Integer = WeekDay(Date(FirstOfMonth)) 'the weekday of the first of the month
  Dim TextHeight, TextWidth, GridTop As Float

  GridTop = 7.2 * 960

  'Big Photo
  Paint.DrawPicture(Pic, LeftMargin, TopMargin / 2, Paint.Width - 2 * LeftMargin, 5 * 960) '5 inch height

  'Month and Year title
  Paint.Font = Font["Copperplate33bc,32"]
  TextHeight = Paint.TextExtents("S").Height 'the height of a character
  s = Choose(ThisMonth, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") & " " & ThisYear
  Paint.DrawText(s, 0, GridTop - 1000, Paint.Width, TextHeight, Align.Center) 'inch above grid top

  'Grid
  Dim Side As Float = (Paint.Width - 2 * LeftMargin) / 7 'one-seventh of the width between margins
  For row = 0 To 4
    For col = 0 To 6
      Paint.DrawRect(LeftMargin + Side * Col, GridTop + Side * Row, Side, Side, Color.Black) 'each square
    Next
  Next

  'Days of the Week headings
  Paint.Font = Font["Apple Chancery,12"]
  TextHeight = Paint.TextExtents("S").Height 'the height of a character
  For col = 0 To 7
    s = Choose(col + 1, "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
    Paint.DrawText(s, LeftMargin + Side * Col, GridTop - TextHeight - 96, Side, TextHeight, Align.Center)
  Next

  'Dates
  Dim DaysInMonth As Integer
  If ThisYear Mod 4 = 0 And ThisMonth = 2 Then DaysInMonth = 29 Else DaysInMonth = Choose(ThisMonth, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
  Paint.Font = Font["Linux Libertine,20"]
  TextHeight = Paint.TextExtents("1").Height 'the height of a digit
  For row = 0 To 4
    For col = 0 To 6
      CellNum = 7 * row + col
      If CellNum >= StartDay Then
        DayNum += 1
        If DayNum > DaysInMonth Then Return 'Don't go to 35 days in the month!
        s = If(Col = 0, "<font color=#DD0000>" & DayNum & "</Font>", "<font color=#000000>" & DayNum & "</Font>")
        Paint.DrawRichText(s, LeftMargin + Side * Col + 96, GridTop + Side * Row + TextHeight + 96)
      Endif
    Next
  Next
  Row = 0
  Col = 0
  While DayNum < DaysInMonth 'Put extra dates up in the top left of the grid.
    DayNum += 1
    s = If(Col = 0, "<font color=#DD0000>" & DayNum & "</Font>", "<font color=#000000>" & DayNum & "</Font>")
    Paint.DrawRichText(s, LeftMargin + Side * Col + 96, GridTop + Side * Row + TextHeight + 96)
    Col += 1 'next column
  Wend

End


從壓縮檔案中程式設計 Gambas
 ← SQLite 列印 托盤圖示筆記本 → 
華夏公益教科書