Developing with Graphics/zh TW: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Developing with Graphics/zh TW
| Old | New | Line | |
|---|---|---|---|
| 1 | + | 本頁敘述在 Lazarus 下繪圖時所使用到的基本類別與技巧。更多特定的主題將另文說明。 | |
| 2 | + | ||
| 3 | + | ## 其他繪圖文章 | |
| 4 | + | ||
| 5 | + | - [[BGRABitmap]] - 繪製各種圖案,點陣圖加透明效果,直接存取圖像圖素等。 | |
| 6 | + | - [[GLScene]] - 視覺化 OpenGL 圖形函式庫交流站 [GLScene](http://www.glscene.org) | |
| 7 | + | - [[TAChart]] - Lazarus 的圖表元件 | |
| 8 | + | - [[PascalMagick]] - 使用 [ImageMagick](http://www.imagemagick.org) 應用程式介面的簡單範例,建立一個跨平台,可編輯點陣圖檔的自由軟體。 | |
| 9 | + | - [[PlotPanel]] - 製作動態的圖表繪製 | |
| 10 | + | - [[LazRGBGraphics]] - 該套件提供對記憶體影像的處理與圖像圖素的操作 (例如掃瞄線)。 | |
| 11 | + | - [[Perlin Noise]] - 一篇於 LCL 使用 Perlin Noise 實作的應用程式。 | |
| 12 | + | ||
| 13 | + | ## 使用 TBitmap 工作 | |
| 14 | + | ||
| 15 | + | 在某些作業系統,點陣圖資料並非儲存在記憶體裡,所以無法直接存取,當 Lazarus 想要做為一個可以跨平台獨立作業的應用軟體時,TBitmap 類別就無法提供像是掃瞄線這樣的內容。這裡還有個 GetDataLineStart 函式,相等於掃瞄線 (Scanline ) 的功能,但僅能利用於記憶體裡的影像,或像使用內建的 TrawImage TLazIntfImage。 | |
| 16 | + | ||
| 17 | + | 總結說來,你只能透過記憶體裡的影像去間接去修改點陣圖,然後再轉換成可繪製的點陣圖。這當然會比較慢。不然使用 Lazarus 內建的 [[Developing with Graphics|TLazIntfImage]] 或是使用外部的函式庫,像是[[BGRABitmap]],[[LazRGBGraphics]]與[[Current conversion projects|Graphics32]] 可以用來直接存取點陣圖。 | |
| 18 | + | ||
| 19 | + | 註:當你建立一個點陣圖時,你必須指定他寬和高,不然你所繪製的東西都會歸零。 | |
| 20 | + | ||
| 21 | + | ### 直接存取圖像圖素 | |
| 22 | + | ||
| 23 | + | 在 Delphi 裡,或用 TBitmap.Scanline 來存取圖像圖素。因為內容是無法再傳遞給別人的。Lazarus 有其他的辦法。可以參考 TLazIntfImage 而不是 TBitmap.Pixels,這非常慢。 | |
| 24 | + | ||
| 25 | + | ### 在點陣圖裡繪製透明色 | |
| 26 | + | ||
| 27 | + | Lazarus 0.9.11 的特點之一,就是可以在點陣圖裡繪製透明色,點陣圖檔案 (*.BMP) 無法儲存透明的資訊,但若你的圖裡有透明色的設定,在 Win32 裡大多的應用程式都可以辨別的出來。 | |
| 28 | + | ||
| 29 | + | 接下來的範例會從 Windows 的資源裡載入一張點陣圖,把其中一個顏色指定為透明 (clFuchsia),然後在畫面上繪圖。 | |
| 30 | + | ||
| 31 | + | ``` pascal | |
| 32 | + | procedure MyForm.MyButtonOnClick(Sender: TObject); | |
| 33 | + | var | |
| 34 | + | buffer: THandle; | |
| 35 | + | bmp: TBitmap; | |
| 36 | + | memstream: TMemoryStream; | |
| 37 | + | begin | |
| 38 | + | bmp := TBitmap.Create; | |
| 39 | + | ||
| 40 | + | buffer := Windows.LoadBitmap(hInstance, MAKEINTRESOURCE(ResourceID)); | |
| 41 | + | ||
| 42 | + | if (buffer = 0) then exit; // 載入點陣圖檔出錯 | |
| 43 | + | ||
| 44 | + | bmp.Handle := buffer; | |
| 45 | + | memstream := TMemoryStream.create; | |
| 46 | + | try | |
| 47 | + | bmp.SaveToStream(memstream); | |
| 48 | + | memstream.position := 0; | |
| 49 | + | bmp.LoadFromStream(memstream); | |
| 50 | + | finally | |
| 51 | + | memstream.free; | |
| 52 | + | end; | |
| 53 | + | ||
| 54 | + | bmp.Transparent := True; | |
| 55 | + | bmp.TransparentColor := clFuchsia; | |
| 56 | + | ||
| 57 | + | MyCanvas.Draw(0, 0, bmp); | |
| 58 | + | ||
| 59 | + | bmp.Free; // 釋放資料分派的空間 | |
| 60 | + | end; | |
| 61 | + | ``` | |
| 62 | + | ||
| 63 | + | 注意到記憶體的操作用到 [[doc:rtl/classes/tmemorystream.html|TMemoryStream]]。它在載入影像到記憶體裡的時候必須用到。 | |
| 64 | + | ||
| 65 | + | ### 擷取螢幕畫面 | |
| 66 | + | ||
| 67 | + | 從 Lazarus 0.9.16 開始之後你可以使用跨平台的 LCL 功能來擷取畫面,下面的範例可以達成此作業。(使用 gtk2 和 win32,但不是 gtk1): | |
| 68 | + | ||
| 69 | + | ``` pascal | |
| 70 | + | uses Graphics, LCLIntf, LCLType; | |
| 71 | + | ||
| 72 | + | ... | |
| 73 | + | ||
| 74 | + | var | |
| 75 | + | MyBitmap: TBitmap; | |
| 76 | + | ScreenDC: HDC; | |
| 77 | + | begin | |
| 78 | + | MyBitmap := TBitmap.Create; | |
| 79 | + | ScreenDC := GetDC(0); | |
| 80 | + | MyBitmap.LoadFromDevice(ScreenDC); | |
| 81 | + | ReleaseDC(ScreenDC); | |
| 82 | + | ||
| 83 | + | ... | |
| 84 | + | ``` | |
| 85 | + | ||
| 86 | + | ## 使用 TLazIntfImage 作業 | |
| 87 | + | ||
| 88 | + | ### 淡出的範例 | |
| 89 | + | ||
| 90 | + | 使用 TLazIntfImage 做出淡出效果的範例 | |
| 91 | + | ||
| 92 | + | ``` pascal | |
| 93 | + | { 這段程式碼可以在這個專案 $LazarusPath/examples/lazintfimage/fadein1.lpi 裡看到。 } | |
| 94 | + | uses LCLType, // HBitmap 類型 | |
| 95 | + | IntfGraphics, // TLazIntfImage 類型 | |
| 96 | + | fpImage; // TFPColor 類型 | |
| 97 | + | ... | |
| 98 | + | procedure TForm1.FadeIn(ABitMap: TBitMap); | |
| 99 | + | var | |
| 100 | + | SrcIntfImg, TempIntfImg: TLazIntfImage; | |
| 101 | + | ImgHandle,ImgMaskHandle: HBitmap; | |
| 102 | + | FadeStep: Integer; | |
| 103 | + | px, py: Integer; | |
| 104 | + | CurColor: TFPColor; | |
| 105 | + | TempBitmap: TBitmap; | |
| 106 | + | begin | |
| 107 | + | SrcIntfImg:=TLazIntfImage.Create(0,0); | |
| 108 | + | SrcIntfImg.LoadFromBitmap(ABitmap.Handle,ABitmap.MaskHandle); | |
| 109 | + | TempIntfImg:=TLazIntfImage.Create(0,0); | |
| 110 | + | TempIntfImg.LoadFromBitmap(ABitmap.Handle,ABitmap.MaskHandle); | |
| 111 | + | TempBitmap:=TBitmap.Create; | |
| 112 | + | for FadeStep:=1 to 32 do begin | |
| 113 | + | for py:=0 to SrcIntfImg.Height-1 do begin | |
| 114 | + | for px:=0 to SrcIntfImg.Width-1 do begin | |
| 115 | + | CurColor:=SrcIntfImg.Colors[px,py]; | |
| 116 | + | CurColor.Red:=(CurColor.Red*FadeStep) shr 5; | |
| 117 | + | CurColor.Green:=(CurColor.Green*FadeStep) shr 5; | |
| 118 | + | CurColor.Blue:=(CurColor.Blue*FadeStep) shr 5; | |
| 119 | + | TempIntfImg.Colors[px,py]:=CurColor; | |
| 120 | + | end; | |
| 121 | + | end; | |
| 122 | + | TempIntfImg.CreateBitmaps(ImgHandle,ImgMaskHandle,false); | |
| 123 | + | TempBitmap.Handle:=ImgHandle; | |
| 124 | + | TempBitmap.MaskHandle:=ImgMaskHandle; | |
| 125 | + | Canvas.Draw(0,0,TempBitmap); | |
| 126 | + | end; | |
| 127 | + | SrcIntfImg.Free; | |
| 128 | + | TempIntfImg.Free; | |
| 129 | + | TempBitmap.Free; | |
| 130 | + | end; | |
| 131 | + | ``` | |
| 132 | + | ||
| 133 | + | ### 影像檔格式特定範例 | |
| 134 | + | ||
| 135 | + | 如果你知道 TBitmap 的藍色使用 8bit,綠色 8bit,紅色 8bit,你可以直接對位元存取,這樣比較快: | |
| 136 | + | ||
| 137 | + | ``` pascal | |
| 138 | + | uses LCLType, // HBitmap 類型 | |
| 139 | + | IntfGraphics, // TLazIntfImage 類型 | |
| 140 | + | fpImage; // TFPColor 類型 | |
| 141 | + | ... | |
| 142 | + | type | |
| 143 | + | TRGBTripleArray = array[0..32767] of TRGBTriple; | |
| 144 | + | PRGBTripleArray = ^TRGBTripleArray; | |
| 145 | + | ||
| 146 | + | procedure TForm1.FadeIn2(aBitMap: TBitMap); | |
| 147 | + | var | |
| 148 | + | IntfImg1, IntfImg2: TLazIntfImage; | |
| 149 | + | ImgHandle,ImgMaskHandle: HBitmap; | |
| 150 | + | FadeStep: Integer; | |
| 151 | + | px, py: Integer; | |
| 152 | + | CurColor: TFPColor; | |
| 153 | + | TempBitmap: TBitmap; | |
| 154 | + | Row1, Row2: PRGBTripleArray; | |
| 155 | + | begin | |
| 156 | + | IntfImg1:=TLazIntfImage.Create(0,0); | |
| 157 | + | IntfImg1.LoadFromBitmap(aBitmap.Handle,aBitmap.MaskHandle); | |
| 158 | + | ||
| 159 | + | IntfImg2:=TLazIntfImage.Create(0,0); | |
| 160 | + | IntfImg2.LoadFromBitmap(aBitmap.Handle,aBitmap.MaskHandle); | |
| 161 | + | ||
| 162 | + | TempBitmap:=TBitmap.Create; | |
| 163 | + | ||
| 164 | + | //用到類似掃瞄線的功能 | |
| 165 | + | for FadeStep:=1 to 32 do begin | |
| 166 | + | for py:=0 to IntfImg1.Height-1 do begin | |
| 167 | + | Row1 := IntfImg1.GetDataLineStart(py); //類似 Delphi TBitMap.ScanLine | |
| 168 | + | Row2 := IntfImg2.GetDataLineStart(py); //類似 Delphi TBitMap.ScanLine | |
| 169 | + | for px:=0 to IntfImg1.Width-1 do begin | |
| 170 | + | Row2^[px].rgbtRed:= (FadeStep * Row1^[px].rgbtRed) shr 5; | |
| 171 | + | Row2^[px].rgbtGreen := (FadeStep * Row1^[px].rgbtGreen) shr 5; // 淡出 | |
| 172 | + | Row2^[px].rgbtBlue := (FadeStep * Row1^[px].rgbtBlue) shr 5; | |
| 173 | + | end; | |
| 174 | + | end; | |
| 175 | + | IntfImg2.CreateBitmaps(ImgHandle,ImgMaskHandle,false); | |
| 176 | + | ||
| 177 | + | TempBitmap.Handle:=ImgHandle; | |
| 178 | + | TempBitmap.MaskHandle:=ImgMaskHandle; | |
| 179 | + | Canvas.Draw(0,0,TempBitmap); | |
| 180 | + | end; | |
| 181 | + | ||
| 182 | + | IntfImg1.Free; | |
| 183 | + | IntfImg2.Free; | |
| 184 | + | TempBitmap.Free; | |
| 185 | + | end; | |
| 186 | + | ``` | |
| 187 | + | ||
| 188 | + | ### 於 TLazIntfImage 與 TBitmap 之間轉換 | |
| 189 | + | ||
| 190 | + | 自從 Lazarus 沒有 TBitmap.ScanLines 這內容後,想要最妥當的存取圖像圖素的方法就是讀跟寫都使用 TLazIntfImage。TBitmap 可以使用 TBitmap.CreateIntfImage() 轉換到 TLazIntfImage,再修改圖素後還可以再用TBitmap.LoadFromIntfImage() 轉回到 TBitmap; 這裡的範例就是從 TBitmap 建立一個 TLazIntfImage,修改後,再轉回到 TBitmap。 | |
| 191 | + | ||
| 192 | + | ``` pascal | |
| 193 | + | uses | |
| 194 | + | ...GraphType, IntfGraphics, LCLType, LCLProc, LCLIntf ... | |
| 195 | + | ||
| 196 | + | procedure TForm1.Button4Click(Sender: TObject); | |
| 197 | + | var | |
| 198 | + | b: TBitmap; | |
| 199 | + | t: TLazIntfImage; | |
| 200 | + | begin | |
| 201 | + | b := TBitmap.Create; | |
| 202 | + | try | |
| 203 | + | b.LoadFromFile('test.bmp'); | |
| 204 | + | t := b.CreateIntfImage; | |
| 205 | + | ||
| 206 | + | // 對圖素讀或寫 | |
| 207 | + | t.Colors[10,20] := colGreen; | |
| 208 | + | ||
| 209 | + | b.LoadFromIntfImage(t); | |
| 210 | + | finally | |
| 211 | + | t.Free; | |
| 212 | + | b.Free; | |
| 213 | + | end; | |
| 214 | + | end; | |
| 215 | + | ``` | |
| 216 | + | ||
| 217 | + | ## 動態的圖形 - 要怎麼避免閃爍 | |
| 218 | + | ||
| 219 | + | 許多程式在他們的 GUI 畫面用到 2D 的繪圖。但若這些圖像想要有動態的變化,你馬上就會面臨到一個問題:快速的圖片轉換你的螢幕會閃爍,這會讓你的使用者有時只看到你的圖的部份而不是全部。這一定會發生,因為處理圖片需要時間。 | |
| 220 | + | ||
| 221 | + | 但我要如何才能使繪圖達到最佳的效果而避免閃爍呢?當然你首先可以利用 OpenGL 圖形加速器,但這對小程式來講,程式碼會負擔變重,這個範例我們使用 TCanvas 來繪圖。如果你有需要到 OpenGL 的協助,那請再參看 Lazarus 對於 OpenGL 的文件,你也可以用 A.J. Venter's gamepack,這個繪圖元件有用到雙緩衝的功能。 | |
| 222 | + | ||
| 223 | + | 現在我們來看看這幾個繪圖選項: | |
| 224 | + | ||
| 225 | + | - TImage 繪圖 | |
| 226 | + | - 於表單的 OnPaint 事件中進行繪圖,用到 TPaintBox 或其他控制項 | |
| 227 | + | - 建立一個會自動繪圖的自訂控項 | |
| 228 | + | - 使用 A.J. Venter's gamepack | |
| 229 | + | ||
| 230 | + | ### TImage 繪圖 | |
| 231 | + | ||
| 232 | + | TImage 包含兩個部份:TGraphic,通常也就是 TBitmap,在每一個 OnPaint 事件中負起在畫面保持一個可以繪圖的區域。TImage 下進行重新取樣並**不會**真的將點陣圖變更尺寸。 圖形 (或說點陣圖) 可以透過 Image1.Picture.Graphic (或 Image1.Picture.Bitmap) 存取,但控制畫面畫布區為 Image1.Picture.Bitmap.Canvas。 TImage 畫布的可視範圍可以在 Image1.OnPaint 事件中透過 Image1.Canvas 存取。 | |
| 233 | + | ||
| 234 | + | **重要**:千萬別在 Image1 的 OnPaint 事件中繪製 TImage 點陣圖。TImage 的圖形是存在緩衝區中的,所以你要繪製它的時候就直接在那執行,也會即時生效,但如果你會需要常常重新繪製它,影像就會閃爍,這樣的話你就得選用另一個方法。TImage 繪圖被視為比其他的方法都慢。 | |
| 235 | + | ||
| 236 | + | #### TImage 的點陣圖重新取樣 | |
| 237 | + | ||
| 238 | + | 註:請勿於 OnPaint 事件時使用。 | |
| 239 | + | ||
| 240 | + | ``` pascal | |
| 241 | + | with Image1.Picture.Bitmap do begin | |
| 242 | + | Width:=100; | |
| 243 | + | Height:=120; | |
| 244 | + | end; | |
| 245 | + | ``` | |
| 246 | + | ||
| 247 | + | #### 繪製 TImage 點陣圖 | |
| 248 | + | ||
| 249 | + | 註:請勿於 OnPaint 事件時使用。 | |
| 250 | + | ||
| 251 | + | ``` pascal | |
| 252 | + | with Image1.Picture.Bitmap.Canvas do begin | |
| 253 | + | // 將目前的區域填滿紅色 | |
| 254 | + | Brush.Color := clRed; | |
| 255 | + | FillRect(0, 0, Width, Height); | |
| 256 | + | end; | |
| 257 | + | ``` | |
| 258 | + | ||
| 259 | + | 註:在 Image1.OnPaint 裡,Image1.Canvas 指到的是有時效性的可見區域,而在 Image1.OnPaint 之外 Image1.Canvas 指到 Image1.Picture.Bitmap.Canvas。 | |
| 260 | + | ||
| 261 | + | 另一個範例: | |
| 262 | + | ||
| 263 | + | ``` pascal | |
| 264 | + | procedure TForm1.BitBtn1Click(Sender: TObject); | |
| 265 | + | var | |
| 266 | + | x, y: Integer; | |
| 267 | + | begin | |
| 268 | + | // 繪製背景 | |
| 269 | + | MyImage.Canvas.Pen.Color := clWhite; | |
| 270 | + | MyImage.Canvas.Rectangle(0, 0, Image.Width, Image.Height); | |
| 271 | + | ||
| 272 | + | // 繪製方形 | |
| 273 | + | MyImage.Canvas.Pen.Color := clBlack; | |
| 274 | + | for x := 1 to 8 do | |
| 275 | + | for y := 1 to 8 do | |
| 276 | + | MyImage.Canvas.Rectangle(Round((x - 1) * Image.Width / 8), Round((y - 1) * Image.Height / 8), | |
| 277 | + | Round(x * Image.Width / 8), Round(y * Image.Height / 8)); | |
| 278 | + | end; | |
| 279 | + | ``` | |
| 280 | + | ||
| 281 | + | #### 於有時效性的可見區域繪製 TImage | |
| 282 | + | ||
| 283 | + | 在 OnPaint 裡你只能在固定區域裡作畫。當區域無法繪製時 OnPaint 最後會自動被 LCL 呼叫,你可以用 Image1.Invalidate 來自訂無效的區域,這不會立即呼叫 OnPaint,而且你可以多次地做無效的指定。 | |
| 284 | + | ||
| 285 | + | ``` pascal | |
| 286 | + | procedure TForm.Image1Paint(Sender: TObject); | |
| 287 | + | begin | |
| 288 | + | // 畫一條線 | |
| 289 | + | Canvas.Pen.Color := clRed; | |
| 290 | + | Canvas.Line(0, 0, Width, Height); | |
| 291 | + | end; | |
| 292 | + | ``` | |
| 293 | + | ||
| 294 | + | ### 在 OnPaint 事件中繪圖 | |
| 295 | + | ||
| 296 | + | 這裡的範例裡所有的繪圖作業都在表單的 OnPaint 事件發生時完成,或是某個另外的控制項。這無需像 TImage 的需要緩衝,它需要在事件處理器被呼叫的時候把所有工作一次做完。 | |
| 297 | + | ||
| 298 | + | ``` pascal | |
| 299 | + | procedure TForm.Form1Paint(Sender: TObject); | |
| 300 | + | begin | |
| 301 | + | // 繪出一條線 | |
| 302 | + | Canvas.Pen.Color := clRed; | |
| 303 | + | Canvas.Line(0, 0, Width, Height); | |
| 304 | + | end; | |
| 305 | + | ``` | |
| 306 | + | ||
| 307 | + | ### 建立自訂一個控制項用來自動繪圖 | |
| 308 | + | ||
| 309 | + | 建立一個自訂的控制項的好處在於加強你程式的結構,控制項也可以用來重覆利用。這很快就能做到,但如果你不是在 TBitmap 下先繪圖再轉移上畫布區,這個做法還是會閃爍。在這裡我們就用不到 OnPaint 這個事件的控制項了。 | |
| 310 | + | ||
| 311 | + | 以下即為自訂控制項的範例: | |
| 312 | + | ||
| 313 | + | ``` pascal | |
| 314 | + | uses | |
| 315 | + | Classes, SysUtils, Controls, Graphics, LCLType; | |
| 316 | + | ||
| 317 | + | type | |
| 318 | + | TMyDrawingControl = class(TCustomControl) | |
| 319 | + | public | |
| 320 | + | procedure EraseBackground(DC: HDC); override; | |
| 321 | + | procedure Paint; override; | |
| 322 | + | end; | |
| 323 | + | ||
| 324 | + | implementation | |
| 325 | + | ||
| 326 | + | procedure TMyDrawingControl.EraseBackground(DC: HDC); | |
| 327 | + | begin | |
| 328 | + | // 取消註解就能開啟預設是抺白的背景 | |
| 329 | + | //繼承抺除背景 EraseBackground(DC); | |
| 330 | + | end; | |
| 331 | + | ||
| 332 | + | procedure TMyDrawingControl.Paint; | |
| 333 | + | var | |
| 334 | + | x, y: Integer; | |
| 335 | + | Bitmap: TBitmap; | |
| 336 | + | begin | |
| 337 | + | Bitmap := TBitmap.Create; | |
| 338 | + | try | |
| 339 | + | // 初始點陣圖尺寸 | |
| 340 | + | Bitmap.Height := Height; | |
| 341 | + | Bitmap.Width := Width; | |
| 342 | + | ||
| 343 | + | // 繪製背景 | |
| 344 | + | Bitmap.Canvas.Pen.Color := clWhite; | |
| 345 | + | Bitmap.Canvas.Rectangle(0, 0, Width, Height); | |
| 346 | + | ||
| 347 | + | // 繪製方形 | |
| 348 | + | Bitmap.Canvas.Pen.Color := clBlack; | |
| 349 | + | for x := 1 to 8 do | |
| 350 | + | for y := 1 to 8 do | |
| 351 | + | Bitmap.Canvas.Rectangle(Round((x - 1) * Width / 8), Round((y - 1) * Height / 8), | |
| 352 | + | Round(x * Width / 8), Round(y * Height / 8)); | |
| 353 | + | ||
| 354 | + | Canvas.Draw(0, 0, Bitmap); | |
| 355 | + | finally | |
| 356 | + | Bitmap.Free; | |
| 357 | + | end; | |
| 358 | + | ||
| 359 | + | inherited Paint; | |
| 360 | + | end; | |
| 361 | + | ``` | |
| 362 | + | ||
| 363 | + | 然後我們在表單上建立它: | |
| 364 | + | ||
| 365 | + | ``` pascal | |
| 366 | + | procedure TMyForm.FormCreate(Sender: TObject); | |
| 367 | + | begin | |
| 368 | + | MyDrawingControl := TMyDrawingControl.Create(Self); | |
| 369 | + | MyDrawingControl.Height := 400; | |
| 370 | + | MyDrawingControl.Width := 500; | |
| 371 | + | MyDrawingControl.Top := 0; | |
| 372 | + | MyDrawingControl.Left := 0; | |
| 373 | + | MyDrawingControl.Parent := Self; | |
| 374 | + | MyDrawingControl.DoubleBuffered := True; | |
| 375 | + | end; | |
| 376 | + | ``` | |
| 377 | + | ||
| 378 | + | 物件最後會自動釋放,因為擁有者我們設定為自己 (Self)。 | |
| 379 | + | ||
| 380 | + | 將上邊界與左邊界定位點設定為零這個步驟到不一定需要,這只是個基準點,但這控制項放在哪其實都一樣。 | |
| 381 | + | ||
| 382 | + | "MyDrawingControl.Parent := Self;" 這步非常重要,如果不這麼做你會看不到你的控制項。 | |
| 383 | + | ||
| 384 | + | "MyDrawingControl.DoubleBuffered := True;" 是在 Windows 下,用來避免閃爍用的,在 gtk 下沒有效果。 | |
| 385 | + | ||
| 386 | + | ### 使用 A.J. Venter's gamepack | |
| 387 | + | ||
| 388 | + | 該元件用於畫布上繪圖時會啟用雙緩衝功能,當在你一切都就緒的時候更新畫布上的內容,這在程式碼上要下點功夫,但這當在用於要快速大量的切換畫面的時候非常有用。如果你有這的需求,那就對 A.J. Venter's gamepack 一定有興趣了,該套件裡的元件常用來在 Lazarus 做遊戲開發,在畫面輸出像精靈 (sprite) 元件一樣做雙緩衝處理,設計可以將兩者組合在一起用。你可以透過 subversion 在這裡找到 gamepack:\ | |
| 389 | + | `svn co `[`svn://silentcoder.co.za/lazarus/gamepack`](svn://silentcoder.co.za/lazarus/gamepack) | |
| 390 | + | ||
| 391 | + | 這個網頁給你更多的資訊,文件與下載:[首頁](http://outkastsolutions.co.za/outkast/index.php?option=com_openwiki&Itemid=&id=gamepack)。 | |
| 392 | + | ||
| 393 | + | ## Image formats | |
| 394 | + | ||
| 395 | + | 這個表提供每個影像格式所要使用的適當類別。 | |
| 396 | + | ||
| 397 | + | | 格式 | 影像類別 | 單元 | | |
| 398 | + | |----------------------|-------------------------|------| | |
| 399 | + | | 游標 (cur) | TCursor | 圖形 | | |
| 400 | + | | 點陣圖檔 (bmp) | TBitmap | 圖形 | | |
| 401 | + | | Windows 圖示 (ico) | TIcon | 圖形 | | |
| 402 | + | | Mac OS X 圖示 (icns) | TicnsIcon | 圖形 | | |
| 403 | + | | Pixmap (xpm) | TPixmap | 圖形 | | |
| 404 | + | | 可傳遞網路圖形 (png) | TPortableNetworkGraphic | 圖形 | | |
| 405 | + | | JPEG (jpg, jpeg) | TJpegImage | 圖形 | | |
| 406 | + | | PNM (pnm) | TPortableAnyMapGraphic | 圖形 | | |
| 407 | + | ||
| 408 | + | 也可參見[[fcl-image|fcl-image 支援格式列表]]。 | |
| 409 | + | ||
| 410 | + | ### 轉換格式 | |
| 411 | + | ||
| 412 | + | 有時候無法避免要將圖檔的格式轉換到另一個。 轉換圖檔有一個方法是使用中介格式,然後再轉換到 TBitmap。 大多數的格式都可以從 TBitmap 上再去建立。 | |
| 413 | + | ||
| 414 | + | 轉換點陣圖檔到 PNG 格式然後再儲存它: | |
| 415 | + | ||
| 416 | + | ``` pascal | |
| 417 | + | procedure SaveToPng(const bmp: TBitmap; PngFileName: String); | |
| 418 | + | var | |
| 419 | + | png : TPortableNetworkGraphic; | |
| 420 | + | begin | |
| 421 | + | png := TPortableNetworkGraphic.Create; | |
| 422 | + | try | |
| 423 | + | png.Assign(bmp); | |
| 424 | + | png.SaveToFile(PngFileName); | |
| 425 | + | finally | |
| 426 | + | png.Free; | |
| 427 | + | end; | |
| 428 | + | end; | |
| 429 | + | ``` | |
| 430 | + | ||
| 431 | + | ## 圖像圖素格式 | |
| 432 | + | ||
| 433 | + | ### TColor | |
| 434 | + | ||
| 435 | + | 在 LCL 裡,為 TColor 內建的圖素格式是為 XXBBGGRR 格式,其符合 Windows 的原生格式,且也與大部份的函式庫 AARRGGBB 格式對衝。XX 是用來定義如果顏色為固定的色盤顏色,像 XX 若為 00 則代表他是系統預設的顏色之一,並沒有為 Alpha 頻道預留任何空間。 | |
| 436 | + | ||
| 437 | + | 要將各別的 RGB 頻道轉換到 TColor 時使用: | |
| 438 | + | ||
| 439 | + | ``` pascal | |
| 440 | + | RGBToColor(RedVal, GreenVal, BlueVal); | |
| 441 | + | ``` | |
| 442 | + | ||
| 443 | + | 分別使用 Red,Green,Blue 函式取得此三個頻道的 TColor 變數值。 | |
| 444 | + | ||
| 445 | + | ``` pascal | |
| 446 | + | RedVal := Red(MyColor); | |
| 447 | + | GreenVal := Green(MyColor); | |
| 448 | + | BlueVal := Blue(MyColor); | |
| 449 | + | ``` | |
| 450 | + | ||
| 451 | + | ### TFPColor | |
| 452 | + | ||
| 453 | + | TFPColor 使用 AARRGGBB 格式,普遍見於各種函式庫。 | |
| 454 | + | ||
| 455 | + | ## 使用 TCanvas 作業 | |
| 456 | + | ||
| 457 | + | ### 使用預設的 GUI 字型 | |
| 458 | + | ||
| 459 | + | 以下簡單的程式碼就可以達成: | |
| 460 | + | ||
| 461 | + | ``` pascal | |
| 462 | + | SelectObject(Canvas.Handle, GetStockObject(DEFAULT_GUI_FONT)); | |
| 463 | + | ``` | |
| 464 | + | ||
| 465 | + | ### 在固定寬度內繪製文字 | |
| 466 | + | ||
| 467 | + | 使用 DrawText,先加入 DT_CALCRECT 然後再排除它。 | |
| 468 | + | ||
| 469 | + | ``` pascal | |
| 470 | + | // 首先要計算文字的尺寸才再進行繪製 | |
| 471 | + | TextBox := Rect(0, currentPos.Y, Width, High(Integer)); | |
| 472 | + | DrawText(ACanvas.Handle, PChar(Text), Length(Text), | |
| 473 | + | TextBox, DT_WORDBREAK or DT_INTERNAL or DT_CALCRECT); | |
| 474 | + | ||
| 475 | + | DrawText(ACanvas.Handle, PChar(Text), Length(Text), | |
| 476 | + | TextBox, DT_WORDBREAK or DT_INTERNAL); | |
| 477 | + | ``` | |
| 478 | + | ||
| 479 | + | ### 繪製有銳角的文字 (無反鋸齒補償) | |
| 480 | + | ||
| 481 | + | 某些工具可以達到這個效果 | |
| 482 | + | ||
| 483 | + | ``` pascal | |
| 484 | + | Canvas.Font.Quality := fqNonAntialiased; | |
| 485 | + | ``` | |
| 486 | + | ||
| 487 | + | 有些工具,像是 gtk2 並不支援此效果,他繪製出來的永遠都有反鋸齒補償。這裡有一個簡單的方讓你使用 gtk2 畫出這種銳角。這並不代表所有的情況,只是其中一種概念: | |
| 488 | + | ||
| 489 | + | ``` pascal | |
| 490 | + | procedure PaintAliased(Canvas: TCanvas; x,y: integer; const TheText: string); | |
| 491 | + | var | |
| 492 | + | w,h: integer; | |
| 493 | + | IntfImg: TLazIntfImage; | |
| 494 | + | Img: TBitmap; | |
| 495 | + | dy: Integer; | |
| 496 | + | dx: Integer; | |
| 497 | + | col: TFPColor; | |
| 498 | + | FontColor: TColor; | |
| 499 | + | c: TColor; | |
| 500 | + | begin | |
| 501 | + | w:=0; | |
| 502 | + | h:=0; | |
| 503 | + | Canvas.GetTextSize(TheText,w,h); | |
| 504 | + | if (w<=0) or (h<=0) then exit; | |
| 505 | + | Img:=TBitmap.Create; | |
| 506 | + | IntfImg:=nil; | |
| 507 | + | try | |
| 508 | + | // 在點陣圖中繪製文字 | |
| 509 | + | Img.Masked:=true; | |
| 510 | + | Img.SetSize(w,h); | |
| 511 | + | Img.Canvas.Brush.Style:=bsSolid; | |
| 512 | + | Img.Canvas.Brush.Color:=clWhite; | |
| 513 | + | Img.Canvas.FillRect(0,0,w,h); | |
| 514 | + | Img.Canvas.Font:=Canvas.Font; | |
| 515 | + | Img.Canvas.TextOut(0,0,TheText); | |
| 516 | + | // get memory image | |
| 517 | + | IntfImg:=Img.CreateIntfImage; | |
| 518 | + | // 取代灰色的圖素 | |
| 519 | + | FontColor:=ColorToRGB(Canvas.Font.Color); | |
| 520 | + | for dy:=0 to h-1 do begin | |
| 521 | + | for dx:=0 to w-1 do begin | |
| 522 | + | col:=IntfImg.Colors[dx,dy]; | |
| 523 | + | c:=FPColorToTColor(col); | |
| 524 | + | if c<>FontColor then | |
| 525 | + | IntfImg.Colors[dx,dy]:=colTransparent; | |
| 526 | + | end; | |
| 527 | + | end; | |
| 528 | + | // 建立點陣圖 | |
| 529 | + | Img.LoadFromIntfImage(IntfImg); | |
| 530 | + | // 繪製 | |
| 531 | + | Canvas.Draw(x,y,Img); | |
| 532 | + | finally | |
| 533 | + | IntfImg.Free; | |
| 534 | + | Img.Free; | |
| 535 | + | end; | |
| 536 | + | end; | |
| 537 | + | ``` | |
| 538 | + | ||
| 539 | + | ## fcl-image 繪圖 | |
| 540 | + | ||
| 541 | + | 如果你想要畫的圖不需要顯示在畫面上,你可以不用 LCL,直接採用 fcl-image。舉例來說像不透過 X11 在網路伺服器上執行的程式,就可以不用依賴視覺化函式庫。FPImage (又叫 fcl-image) 在 Pascal 下非常廣泛地被使用,函式庫也非常完整。事實上 LCL 在從檔案載入圖案來編輯的時候也是利用 FPImage 實作的函式來製作工具 (winapi,gtk,carbon...)。另一方面 Fcl-image 也可以做繪圖例行程序。 | |
| 542 | + | ||
| 543 | + | 需要更多資訊,請閱讀 [[fcl-image]] 文章。 | |
| 544 | + | ||
| 545 | + | --- | |
| 546 | + | *From the Free Pascal wiki, page "Developing with Graphics/zh TW", as it stood on 2020-02-13 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
