全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:3946
推到 Plurk!
推到 Facebook!

如何利用CopyRect結果列印?

答題得分者是:william
David
一般會員


發表:13
回覆:26
積分:7
註冊:2002-04-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-09-08 15:24:08 IP:61.221.xxx.xxx 未訂閱
請教各位先進: 我想試著用CopyRect得到的結果送到印表機,卻一直沒有印出東西。例如: Printer.Canvas.CopyRect(aTRect, aControl.Canvas, aSRect); 我有試著先送到Form,例如: Form1.Canvas.CopyRect(aTRect, aControl.Canvas, aSRect); 成功,有正確影像出現。 也送到TImage,例如: Image1.Canvas.CopyRect(aTRect, aControl.Canvas, aSRect); 成功,有正確影像出現。 但就是送到印表機時,印不出任何東西,也沒錯誤訊息,查了許多論壇似乎都沒提到這問題,都是用 Printer.Canvas.CopyRect(aTRect, aControl.Canvas, aSRect); 就可以,似乎沒人遇到我這種問題,請問這是印表機還是我Delphi 5 Professional版本問題(我未升級,不知和這是否有關)??? 不知哪位先進能幫幫我的忙... Thanks for your help. PS: 如果我將該影像存檔,用Image1.Picture.LoadFromFile載入再送去列印,例如: Image1.Picture.LoadFromFile('MyBitmap.bmp'); Printer.BeginDoc; Printer.Canvas.Draw(0, 0, Image1.Picture.Bitmap); Printer.EndDoc; 成功印出。 可是如果沒存檔,用CopyRect送到Image1,例如: BMP.Canvas.CopyRect(aTRect, aControl.Canvas, aSRect); Image1.Picture.Bitmap := BMP; Printer.BeginDoc; Printer.Canvas.Draw(0, 0, Image1.Picture.Bitmap); Printer.EndDoc; 還是不行... 但Image1上面有正確影像出現。 發表人 - David 於 2002/09/08 15:27:19
領航天使
站長


發表:12216
回覆:4186
積分:4084
註冊:2001-07-25

發送簡訊給我
#2 引用回覆 回覆 發表時間:2002-09-09 07:59:44 IP:61.219.xxx.xxx 未訂閱
我試過都可以喔! 可否告知您的aControl是那來的? ~~~Delphi K.Top討論區站長~~~
------
~~~Delphi K.Top討論區站長~~~
David
一般會員


發表:13
回覆:26
積分:7
註冊:2002-04-14

發送簡訊給我
#3 引用回覆 回覆 發表時間:2002-09-09 14:08:18 IP:61.221.xxx.xxx 未訂閱
引言: 我試過都可以喔! 可否告知您的aControl是那來的? ~~~Delphi K.Top討論區站長~~~
首先感謝站長的幫忙. 我的aControl是泛指任一可顯示的元件. 站長您說試過無論有無存檔都可以,而我剛剛從另一位前輩的信件,描述如下... From Delphi help: Delphi canvases use device-dependent bitmaps to implement the Draw, StretchDraw, or CopyRect methods. Because of this, you can’t use these methods with most printers. 這是否意指跟印表機有關//??// 不知站長您的看法如何? 再度謝您的幫忙.
領航天使
站長


發表:12216
回覆:4186
積分:4084
註冊:2001-07-25

發送簡訊給我
#4 引用回覆 回覆 發表時間:2002-09-09 17:27:46 IP:218.163.xxx.xxx 未訂閱
引言: 我的aControl是泛指任一可顯示的元件. 站長您說試過無論有無存檔都可以,而我剛剛從另一位前輩的信件,描述如下... From Delphi help: Delphi canvases use device-dependent bitmaps to implement the Draw, StretchDraw, or CopyRect methods. Because of this, you can’t use these methods with most printers. 這是否意指跟印表機有關//??// 不知站長您的看法如何?
我用下面的指令 var s,d:trect; begin s.left:=0; s.top:=0; s.right:=20; s.bottom:=20; d:=s; image1.canvas.copyrect(d,form1.canvas,s); printer.begindoc; printer.canvas.copyrect(d,image1.canvas,s); printer.enddoc; end; 果真Image1有Printer出不來,說不定那句話說的是真的! ~~~Delphi K.Top討論區站長~~~
------
~~~Delphi K.Top討論區站長~~~
David
一般會員


發表:13
回覆:26
積分:7
註冊:2002-04-14

發送簡訊給我
#5 引用回覆 回覆 發表時間:2002-09-09 17:42:40 IP:61.221.xxx.xxx 未訂閱
引言:
引言: 我的aControl是泛指任一可顯示的元件. 站長您說試過無論有無存檔都可以,而我剛剛從另一位前輩的信件,描述如下... From Delphi help: Delphi canvases use device-dependent bitmaps to implement the Draw, StretchDraw, or CopyRect methods. Because of this, you can’t use these methods with most printers. 這是否意指跟印表機有關//??// 不知站長您的看法如何?
我用下面的指令 var s,d:trect; begin s.left:=0; s.top:=0; s.right:=20; s.bottom:=20; d:=s; image1.canvas.copyrect(d,form1.canvas,s); printer.begindoc; printer.canvas.copyrect(d,image1.canvas,s); printer.enddoc; end; 果真Image1有Printer出不來,說不定那句話說的是真的! ~~~Delphi K.Top討論區站長~~~
站長您好: 我的做法跟您一樣,而且一開始是直接用 printer.canvas.copyrect(d,form1.canvas,s); 失敗。 再用您的方式先copy到一個Image1上,看得到卻印不出東西。 不知您一開始說 都可以 是怎麼做的?存檔再列印嗎?? 我在一些bcb/delphi論壇刊到一些前輩都用CopyRect,難道他們都沒注意到這個限制嗎?還是bcb沒這問題? 再次謝謝您的幫忙。
領航天使
站長


發表:12216
回覆:4186
積分:4084
註冊:2001-07-25

發送簡訊給我
#6 引用回覆 回覆 發表時間:2002-09-09 19:30:26 IP:61.219.xxx.xxx 未訂閱
引言: 不知您一開始說 都可以 是怎麼做的?存檔再列印嗎??
我是將image1在程式設計時就已經將BitMap從File中Load好了, 再直接用Printer.Canvas是可以看到的, 若一開始image是用copyrect就不行耶! 是不是先將Image1 Load好一個空白的圖檔, 之後copyrect至image的圖就可以放入printer.canvas中喔! ~~~Delphi K.Top討論區站長~~~
------
~~~Delphi K.Top討論區站長~~~
David
一般會員


發表:13
回覆:26
積分:7
註冊:2002-04-14

發送簡訊給我
#7 引用回覆 回覆 發表時間:2002-09-10 00:26:36 IP:61.221.xxx.xxx 未訂閱
引言:
引言: 不知您一開始說 都可以 是怎麼做的?存檔再列印嗎??
我是將image1在程式設計時就已經將BitMap從File中Load好了, 再直接用Printer.Canvas是可以看到的, 若一開始image是用copyrect就不行耶! 是不是先將Image1 Load好一個空白的圖檔, 之後copyrect至image的圖就可以放入printer.canvas中喔! ~~~Delphi K.Top討論區站長~~~
我測試了,可是還是不行。 這問題還真傷腦筋喔,只得先存檔再載入,最後印出。不知BCB是否也這樣???
領航天使
站長


發表:12216
回覆:4186
積分:4084
註冊:2001-07-25

發送簡訊給我
#8 引用回覆 回覆 發表時間:2002-09-10 07:53:35 IP:61.219.xxx.xxx 未訂閱
引言: 我測試了,可是還是不行。 這問題還真傷腦筋喔,只得先存檔再載入,最後印出。不知BCB是否也這樣???
我試過了可以阿? 1.設計階段先將image1事先Loadfile一個空白的.BMP檔 2.用image1.canvas.copyrect放入所要的圖 3.用printer.canvas.copyrect丟入印表機 4.可以印出來耶 ~~~Delphi K.Top討論區站長~~~
------
~~~Delphi K.Top討論區站長~~~
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#9 引用回覆 回覆 發表時間:2002-09-10 09:31:05 IP:210.3.xxx.xxx 未訂閱
I extracted some codes from a print preview component I created years ago. The key point is to create a DIB for output to the printer. Hope this help. (DrawCanvas should be the canvas of the selected printer)    
if Pic is TBitmap then with DrawCanvas do begin
    GetDIBSizes(TBitmap(Pic).Handle,InfoSize,ImageSize);
    {$ifdef Win32}
    GetMem(Info,InfoSize);
    GetMem(Image,ImageSize);
    {$else}
    Info  := MemAlloc(InfoSize);
    Image := MemAlloc(ImageSize);
    {$endif}
    GetDIB(TBitmap(Pic).Handle,TBitmap(Pic).Palette,Info^,Image^);
    with Info^.bmiHeader do
        StretchDIBits(Handle,x2,y2,x3,y3,0,0,
                      biWidth,biHeight,Image,Info^,
                      DIB_RGB_COLORS,SRCCOPY);
    FreeMem(Image,ImageSize);
    FreeMem(Info,InfoSize); 
end;
David
一般會員


發表:13
回覆:26
積分:7
註冊:2002-04-14

發送簡訊給我
#10 引用回覆 回覆 發表時間:2002-09-10 14:30:14 IP:61.221.xxx.xxx 未訂閱
引言:
引言: 我測試了,可是還是不行。 這問題還真傷腦筋喔,只得先存檔再載入,最後印出。不知BCB是否也這樣???
我試過了可以阿? 1.設計階段先將image1事先Loadfile一個空白的.BMP檔 2.用image1.canvas.copyrect放入所要的圖 3.用printer.canvas.copyrect丟入印表機 4.可以印出來耶 ~~~Delphi K.Top討論區站長~~~
Sorry,站長。 我知道為什麼我印不出來,因為之前一開始是直接用 Printer.CopyRect(... 作業,失敗後就先放到Bitmap內,所以後來都是用 Bitmap.Canvas.CopyRect(aTRect, RxDBGrid1.Canvas, aSRect); Image1.Picture.Bitmap.Assign( Bitmap ); 這種方式作業,所以即使先載入空白圖層也印不出來,因為Image1的內容已經更換了。 我想用空白圖層的列印方式應該和存檔載入方式同樣,就如同一般開啟圖檔,在於上面塗鴉,不存檔列印仍能把塗鴉內容印一般,有點像投影片功能,因為最後 printer.canvas.copyrect(.. 是把Image1.Canvas印出,但結論就是要先有個檔案。而且,需注意這個空白檔案的Width/Height,因為只能印出實際檔案的Width/Height內容,如果先前 Image1.canvas.copyrect(... 的內容大於原空白圖檔Width/Height,超出部分便沒印出,就像超出投影片以外印不出來,想想應是合理。 再次謝謝站長費時幫我解答,這幾天測這部分搞得亂亂的,剛剛看到william先進提供另外做法,也從Delphi6 Help找到一段說明: Note: Delphi canvases use device-dependent bitmaps to implement the Draw, StretchDraw, or CopyRect methods. Because of this, you can't use these methods with most printers. Instead, to create graphic images on the printer's canvas, create a device-independent bitmap and draw it directly to the canvas's Handle property. (可惜Delphi5 Help似乎沒這段,害我浪費部分時間...) 我正在研究 1create a device-independent bitmap 。what's device-independent? 2draw it directly to the canvas's Handle property。 從Help中提到 The Handle property specifies the Windows GDI handle to the device context for this canvas. Set Handle to the HDC for the device context the canvas must draw into. When a windowed control responds to a Windows paint message, the HDC for drawing is passed in to the PaintWindow method. In other cases, an HDC can be obtained for a window by calling the GetDeviceContext method of a control. Additionally, Windows provides API calls to obtain an HDC for a printer or for a memory image. Read the Handle property to supplement the drawing services provided by the TCanvas object with API calls that require a handle to a device context. Most of the Windows GDI calls require an HDC. TCanvas does not own the HDC. Applications must create an HDC and set the Handle property. Applications must release the HDC when the canvas no longer needs it. Setting the Handle property of a canvas that already has a valid HDC will not automatically release the initial HDC. Note: Some descendants of TCanvas, such as TControlCanvas, do own the HDC. Do not set the Handle property for these objects. They fetch and free their own Handle. 這裡又牽引出HDC,GetDeviceContext這些沒用過的概念,或許對熟悉用過的先進是很簡單的,但目前對我則是陌生需要花時間try try,我看到它附個Sample,與william先進提供的做法有些相關,等研究清楚或遇到問題在報告。 再次感謝站長與william先進的幫忙,現在另外一個傷腦筋的問題是:兩位先進都給了我解答的許多Hint,這分數不知該怎麼指定,應該用個多選,哈... Thanks for your help.Best regards.
系統時間:2024-04-25 14:26:09
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!