0

私はすべてを揃えようとしていますが、方法が完全にはわかりません...これは私が作ったものですが、アイテムの名前、価格、その他は揃えられていません...それらを揃える方法はありますか? これが私のコードです...

  Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim printFont As New Font("Courier New", 10)
    'Dim headingFont As New Font("Courier New", 10)
    Dim sngLineHeight As Single = printFont.GetHeight + 2
    Dim sngXLocation As Single = e.MarginBounds.Left
    Dim sngYLocation As Single = e.MarginBounds.Top
    Dim intItemCount As Integer
    e.Graphics.DrawString("                   XXXX  Restaurant                   ", printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight
    e.Graphics.DrawString("                       RECEIPT                         ", printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight
    e.Graphics.DrawString("========================================================", printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight

    e.Graphics.DrawString(DateTime.Now.ToString("hh:mm dddd, dd MMMM yyyy"), printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight
    sngYLocation += sngLineHeight

    Dim ptOne As Point
    Dim ptTwo As Point
    ptOne.X = 10
    ptOne.Y = 10

    ptTwo.X = 10
    ptTwo.Y = 200
    e.Graphics.DrawLine(Pens.Black, ptOne, ptTwo)
    intItemCount = frmSalesManagement.dgvSaleManagement.Rows.Count
    Dim strItemDesc As String
    Dim strItemDesc2 As String
    Dim strItemDesc3 As String
    Dim strItemDesc4 As String
    Dim strTableNo As String
    Dim strNoOfPerson As String


    strTableNo = frmSalesManagement.txtTableNo.Text
    strNoOfPerson = frmSalesManagement.txtNoOfPerson.Text
    If frmSalesManagement.lblMemberName.Text <> "" Then
        e.Graphics.DrawString("Member's Name     : " & frmSalesManagement.lblMemberName.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Member's Discount : 10%", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
    End If

    e.Graphics.DrawString("Table No.: " & strTableNo, printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight
    e.Graphics.DrawString("No of Person : " & strNoOfPerson, printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight
    sngYLocation += sngLineHeight
    e.Graphics.DrawString("ItemName" & vbTab & "" & vbTab & "Price" & vbTab & "" & vbTab & "Quantity" & vbTab & "" & vbTab & "Subtotal", printFont, Brushes.Black, sngXLocation, sngYLocation)
    sngYLocation += sngLineHeight
    sngYLocation += sngLineHeight
    For i As Integer = 0 To intItemCount - 1



        strItemDesc = frmSalesManagement.dgvSaleManagement.Rows(i).Cells(0).FormattedValue.ToString() + "" & vbTab & "" & vbTab
        strItemDesc2 = frmSalesManagement.dgvSaleManagement.Rows(i).Cells(1).FormattedValue.ToString() + "" & vbTab & "" & vbTab
        strItemDesc3 = frmSalesManagement.dgvSaleManagement.Rows(i).Cells(2).FormattedValue.ToString() + "" & vbTab & "" & vbTab
        strItemDesc4 = frmSalesManagement.dgvSaleManagement.Rows(i).Cells(3).FormattedValue.ToString() + "" & vbTab & "" & vbTab


        e.Graphics.DrawString(strItemDesc & strItemDesc2 & strItemDesc3 & strItemDesc4, printFont, Brushes.Black, sngXLocation, sngYLocation)


        sngYLocation += sngLineHeight
    Next

    If lblMethodOfPayment.Text = "Cash" Then
        e.Graphics.DrawString("Payment Method : " & lblMethodOfPayment.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Discount    :  " & frmSalesManagement.txtDiscount.Text.ToString() & "%", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight

        e.Graphics.DrawString("Due Amount  :  RM" & lblDueAmount.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Amount Paid :  RM" & txtAmountPaid.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Change      :  RM" & lblChange.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("======================= Thank You ======================", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("========================== End =========================", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
    ElseIf lblMethodOfPayment.Text = "Visa Card" Or lblMethodOfPayment.Text = "Master Card" Then
        e.Graphics.DrawString("Payment Method : " & lblMethodOfPayment.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Discount    :  " & frmSalesManagement.txtDiscount.Text.ToString() & "%", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight

        e.Graphics.DrawString("Due Amount  :  RM" & lblDueAmount.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("Amount Paid :  RM" & txtAmountPaid.Text.ToString, printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("======================= Thank You ======================", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
        e.Graphics.DrawString("========================== End =========================", printFont, Brushes.Black, sngXLocation, sngYLocation)
        sngYLocation += sngLineHeight
    End If
    sngYLocation += sngLineHeight


End Sub
4

2 に答える 2

2

Consolas や New Century などのフォントを使用しているため、テキスト エディターではおそらく問題なく配置されます。すべての文字の幅が同じフォント。そして、コード内のステートメントを整列させるために意図的に選択しました。

しかし、それはプリンターでは機能しません。可変ピッチ フォントで印刷することになります。この回答のテキストのように、文字 i は文字 W よりもはるかに少ないスペースを使用します: iiiiii vs WWWWWW.

したがって、DrawString() テキストにスペースを入れないでください。整列されません。代わりに、RectangleF と StringFormat を受け取る DrawString のオーバーロードを使用します。たとえば、StringAlignment.Center を使用して、四角形の完全に中央に配置された文字列を取得します。次の行で同じ長方形の幅を使用すると、テキストは前の行の中央に完全に配置されます。プリンターに固定ピッチのフォントを使用することでも問題は解決しますが、その見た目が好きな人はいません。

于 2013-07-21T10:28:33.497 に答える