アルファサード株式会社 代表取締役 野田 純生のブログ


用紙設定の拡大・縮小に対応したテキストの印刷を実装する(Xojo)


公開日 : 2014-03-22 21:55:08


ブログエディタに印刷機能は要らんと思うのですが、まぁ、エディタだからね。一応。

印刷対応

印刷設定でサイズを%指定した場合のプリントのサンプル。

動的に生成したTextAreaのStyledTextPrinterをコールするとNilObjectionのエラーになるので、ダミーのテキストエリアを配置しておいて隠しておく。TextAreaは両方ともStyledはOff。動的に生成したコントロールがNilになるところは納得いってない。バグじゃねーのとか思うけど、とにかくこれで動くのであるよ。

ダミーのテキストエリア

Sub Action() // Dim PageSetupSettings As String // PageSetupSettings 「ページ設定」から設定したものをプロパティとして保持しておく Dim G As Graphics Dim PageSetup As PrinterSetup = New PrinterSetup Dim PageWidth As Integer Dim PageHeight As Integer If PageSetupSettings <> "" Then // 前回の設定(「ページ設定」で設定した値)を使う PageSetup.SetupString = PageSetupSettings Else If PageSetup.PageSetupDialog Then PageSetupSettings = PageSetup.SetupString End If End If If PageSetupSettings = "" Then Beep Return End If PageWidth = PageSetup.Width PageHeight = PageSetup.Height G = OpenPrinterDialog If G = Nil Then Return End If Dim Scale As Double = 1.0 If G.Width <> PageWidth Then // 拡大/縮小設定をしている場合、G.Width と PageSetup.Widthが一致しない Scale = G.Width / PageWidth End If PrintArea.TextFont = StringArea.TextFont // ダミーのテキストエリアのフォントサイズを変更する Dim NewTextSize As Double If StringArea.TextSize = 0 Then // システムデフォルトサイズの場合、0にScaleを掛けても0なので... NewTextSize = 12 * Scale Else NewTextSize = StringArea.TextSize * Scale End If If Scale <> 1.0 Then PageHeight = G.Height PageWidth = G.Width End If // ダミーテキストエリアに設定して、プリントする PrintArea.TextSize = NewTextSize PrintArea.Text = StringArea.Text Dim Stp As StyledTextPrinter Stp = PrintArea.StyledTextPrinter( G, PageWidth ) Do Until Stp.EOF Stp.DrawBlock( 0, 0, PageHeight ) If Not Stp.EOF Then G.NextPage End If Loop
End Sub

カテゴリ


このブログを書いている人
野田純生の写真
野田 純生 (のだ すみお)

大阪府出身。ウェブアクセシビリティエバンジェリスト。 アルファサード株式会社の代表取締役社長であり、現役のプログラマ。経営理念は「テクノロジーによって顧客とパートナーに寄り添い、ウェブを良くする」。 プロフィール詳細へ