5.2.3 繪圖功能的實(shí)現(xiàn)
繪圖軟件常根據(jù)用戶的要求改變繪圖工具。Graphex.dpr例程中,當(dāng)用戶按下某個(gè)按鈕時(shí),可選擇繪圖工具中的畫筆或畫刷,在程序類型說(shuō)明部分定義了五種繪圖工具。
type
TDrawingTool = (dtLine,dtRectangle,dtEllips,dtRoundRect,dtPolygon);
當(dāng)選中某種按鈕,則選中了相應(yīng)的繪圖工具,如:
procedure TForm1.LineButtonClick(Sender: TObject);
begin
DrawingTool := dtLine;
end;
procedure TForm1.RectangleButtonClick(Sender: TObject);
begin
DrawingTool := dtRectangle;
end;
procedure TForm1.EllipseButtonClick(Sender: TObject);
begin
DrawingTool := dtEllipse;
end;
procedure TForm1.RoundRectButtonClick(Sender: TObject);
begin
DrawingTool := dtRoundRect;
end;
procedure TForm1.PolygonButtonClick(Sender: TObject);
begin
DrawingTool :=dtPolygon;
end;
DrawShape過(guò)程定義了每種繪圖工具的動(dòng)作:
procedure TForm1.DrawShape(TopLeft, BottomRight: TPoint; AMode: TPenMode);
begin
with Image.Canvas do
begin
Pen.Mode := AMode;
case DrawingTool of
dtLine: begin
MoveTo(TopLeft.X, TopLeft.Y);
LineTo(BottomRight.X, BottomRight.Y);
end;
dtRectangle: Rectangle(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y);
dtEllipse: Ellipse(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y);
dtRoundRect: RoundRect(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y,
(TopLeft.X - BottomRight.X) div 2, (TopLeft.Y - BottomRight.Y) div 2);
dtPolygon:Polygon([Point(0,0),TopLeft,BottomRight]); end;
end;
end;
程序剛運(yùn)行時(shí),只有一個(gè)工具欄。當(dāng)用戶單擊畫筆和畫刷時(shí),則出現(xiàn)相應(yīng)的工具欄,如圖5.4。其代碼如下:
procedure TForm1.PenButtonClick(Sender: TObject);
begin
PenBar.Visible := PenButton.Down;
end;
procedure TForm1.BrushButtonClick(Sender: TObject);
begin
BrushBar.Visible := BrushButton.Down;
end;
在設(shè)計(jì)繪圖程序時(shí),還要解決一些問(wèn)題。如為了在鼠標(biāo)移動(dòng)時(shí)能觀測(cè)圖形的變化,我們定義了OnMouseMove事件。但會(huì)出現(xiàn)這樣的現(xiàn)象,當(dāng)鼠標(biāo)進(jìn)入繪圖區(qū)時(shí),用戶未按下鼠標(biāo)鍵,畫布上卻出現(xiàn)繪制的圖形,這是我們不希望看到的。其原因是沒有對(duì)鼠標(biāo)按鈕是否按下進(jìn)行判斷。因此在窗體對(duì)象中定義了drawing的域,當(dāng)鼠標(biāo)按鈕按下時(shí),drawing 設(shè)置成真值。只有drawing為真,鼠標(biāo)移動(dòng)才執(zhí)行繪圖功能;當(dāng)鼠標(biāo)鍵松開時(shí),drawing設(shè)置成假,鼠標(biāo)移動(dòng)將不執(zhí)行繪圖動(dòng)作。
另外一個(gè)問(wèn)題是, 我們希望得到的是鼠標(biāo)按鈕按下和松開這兩點(diǎn)所形成的圖形,但OnMouseMove卻把鼠標(biāo)軌跡上各點(diǎn)與起始點(diǎn)所形成的所有圖形畫在屏幕上,這同樣是我們不希望看到的,為了解決這些問(wèn)題,程序定義了鼠標(biāo)的三個(gè)事件:
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Drawing := True;
Image.Canvas.MoveTo(X, Y);
Origin := Point(X, Y);
MovePt := Origin;
OriginPanel.Caption := Format('Origin: (%d, %d)', [X, Y]);
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Drawing then
DrawShape(Origin, Point(X, Y), pmCopy);
Drawing := False;
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if Drawing then
begin
DrawShape(Origin, MovePt, pmNotXor);
MovePt := Point(X, Y);
DrawShape(Origin, MovePt, pmNotXor);
end;
MovePt用來(lái)記錄鼠標(biāo)當(dāng)前位置。當(dāng)下次鼠標(biāo)移動(dòng)時(shí), 就能在上次鼠標(biāo)繪制的圖形上畫一個(gè)形狀、大小一樣的圖形,并把畫筆顏色設(shè)置成PmNotXor,使上次繪制的圖形顏色變成了屏幕顏色,從而達(dá)到“橡皮擦”的效果。
將畫筆、畫刷的Style屬性設(shè)置成用戶希望的值,可實(shí)現(xiàn)對(duì)畫筆和畫刷風(fēng)格的選擇。
procedure TForm1.SetBrushStyle(Sender: TObject);
begin
with Image.Canvas.Brush do
begin
if Sender = SolidBrush then Style := bsSolid
else if Sender = ClearBrush then Style := bsClear
else if Sender = HorizontalBrush then Style := bsHorizontal
else if Sender = VerticalBrush then Style := bsVertical
else if Sender = FDiagonalBrush then Style := bsFDiagonal
else if Sender = BDiagonalBrush then Style := bsBDiagonal
else if Sender = CrossBrush then Style := bsCross
else if Sender = DiagCrossBrush then Style := bsDiagCross;
end;
procedure TForm1.SetPenStyle(Sender: TObject);
begin
with Image.Canvas.Pen do
begin
if Sender = SolidPen then Style := psSolid
else if Sender = DashPen then Style := psDash
else if Sender = DotPen then Style := psDot
else if Sender = DashDotPen then Style := psDashDot
else if Sender = DashDotDotPen then Style := psDashDotDot
else if Sender = ClearPen then Style := psClear;
end;
end;
相關(guān)推薦:2010年9月計(jì)算機(jī)等級(jí)考試試題及答案解析專題北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |