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