お世話になっております。
XP、VB6です。
DrawTextでプリンターに文字を出力したいのですが、
Ret = DrawText(Picture1.hdc, "abc", Len("abc"), Rect, Flag)
の、Picture1.hdc を Printer.hdc
に書き換えても、プリンターへ出力されません。
Picture1には、出力されます。
Printer.EndDoc
も行っておりますが、どうしてもプリンターへ出力できません。
何かたりない処理があるのでしょうか。
よろしくお願いします。
理由はわからないのですが、
プリンター描画処理のいちばん先頭に、
Printer.Print " "
をいれると、プリンターに出力されました。
ただ、これではあまりきれいではないので、他の方法で解決したいです。
引き続きよろしくお願いします。
極論を言えば、Printer オブジェクトを印刷には利用しないで、
全部 API 関数を使って hDC などを取得しそれに出力すること、
でしょうか。
# 情報の取得程度ならば Printer オブジェクトを使えます。
どうしても Printer オブジェクトを使いたいのであれば、
メタファイルを生成し、そこで DrawText などを用いて描画し、
そのメタファイルを Picture オブジェクトにして PaintPicture
で描画、とでもするとか。
K.J.K.さん
ありがとうございます。
hdcを確認しようと思い、試しに、
----------------------------------------------
Option Explicit
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As Rect, ByVal wFormat As Long) As Long
Private Type Rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Form_Load()
Dim Ret As Integer
Dim Rect As Rect
Dim Flag As Integer
Dim Phdc
Phdc = Printer.hdc
Flag = &H0
Rect.Left = 100
Rect.Top = 100
Rect.Right = 300
Rect.Bottom = 200
Ret = DrawText(Phdc, "abc1", Len("abc1"), Rect, Flag)
Print Printer.hdc
Printer.Print " "
Rect.Left = 100
Rect.Top = 200
Rect.Right = 300
Rect.Bottom = 300
Ret = DrawText(Phdc, "abc2", Len("abc2"), Rect, Flag)
Print Printer.hdc
Printer.EndDoc
End Sub
----------------------------------------------
としてみたところ、フォームには、同じ値のPrinter.hdcが2つ表示されましたが、プリンターからは、"abc2"の文字しか出力されませんでした。
hdcを取得するだけではだめなのでしょうか。
駄目でしょう。
http://support.microsoft.com/kb/175535/EN-US/ (英文)
http://support.microsoft.com/kb/175535/ja (自動翻訳日本語文)
などにあるとおりです。
K.J.K.さん
ありがとうございます。
教えていただいたサイトのコードで、印刷が出来ました。
いま、コードを解読中なのですが、今まで印刷できなかったのは、
印刷開始の宣言
result = StartDoc(hPrintDc, di) 'Start a new print document
result = StartPage(hPrintDc) 'Start a new page
が無かったということでしょうか?
試しに、
hPrintDc = CreateDC(Printer.DriverName, Printer.DeviceName, 0, 0)
を
' hPrintDc = CreateDC(Printer.DriverName, Printer.DeviceName, 0, 0)
として、
hPrintDc = Printer.hdc
としても、印刷できました。
printer.print " "
を行うと、裏で自動的に印刷開始の宣言をしているということでしょうか?
Print メソッドなどの描画メソッドの呼び出しは、内部では、
もし印刷開始処理がなされていなかったら中で行うようになって
いるはずです。
# でないと困りますよね。
やっぱりそうですよね。
APIを使わずにVBの命令で印刷する場合、
EndDoc はあるのに、
StartDoc がないのはおかしいな、と以前から思っていました。
とにかく印刷できました。
ありがとうございました。
やりたかったことの部分を抽出したコードを載せておきます。
-------------------------------------------------------
Option Explicit
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As Rect, ByVal wFormat As Long) As Long
Private Declare Function StartDoc Lib "gdi32" Alias "StartDocA" (ByVal hdc As Long, lpdi As DOCINFO) As Long
Private Declare Function StartPage Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function EndDoc Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function EndPage Lib "gdi32" (ByVal hdc As Long) As Long
Private Type Rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type DOCINFO
cbSize As Long
lpszDocName As String
lpszOutput As String
lpszDatatype As String
fwType As Long
End Type
Private Sub Form_Load()
Dim Ret As Integer
Dim Rect As Rect
Dim Flag As Integer
Flag = &H0
Rem StartDoc
Dim di As DOCINFO
Ret = StartDoc(Printer.hdc, di)
Ret = StartPage(Printer.hdc)
Rem
Rect.Left = 100
Rect.Top = 100
Rect.Right = 300
Rect.Bottom = 200
Ret = DrawText(Printer.hdc, "abc1", Len("abc1"), Rect, Flag)
Rem EndDoc
Ret = EndPage(Printer.hdc)
Ret = EndDoc(Printer.hdc)
End Sub
-------------------------------------------------------