2010-06-10 50 views
1

在德爾福2007年使用下面的代碼:灌裝區域繪製其關閉帆布


procedure TfrmTest.PaintBox1Paint(Sender: TObject); 
const 
    Rect_Size = 10; 
begin 
    PaintBox1.Canvas.Brush.Color := clYellow; 
    PaintBox1.Canvas.FillRect(Rect(0, 0, PaintBox1.width, PaintBox1.height)); 

    PaintBox1.Canvas.Brush.Color := clRed; 
    DrawARect(PaintBox1.Canvas, 0, 0, Rect_Size, Rect_Size); 
end; 

procedure TfrmTest.DrawARect(ACanvas: TCanvas; iLeft, iTop, iWidth, iHeight: Integer); 
var 
    rgnMain: HRGN; 
begin 
    rgnMain := CreateRectRgn(iLeft, iTop, iLeft + iWidth, iTop + iHeight); 
    try 
    SelectClipRgn(ACanvas.handle, rgnMain); 
    ACanvas.FillRect(ACanvas.ClipRect); 
    SelectClipRgn(ACanvas.handle, 0); 
    finally 
    DeleteObject(rgnMain); 
    end; 
end; 

我得到這個: (黃色區域表示PaintBox1的邊界)。

alt text http://www.freeimagehosting.net/uploads/62cf687d29.jpg

(圖像顯示在中心一個黃色的盒子[PaintBox1]一種形式。但是我的紅rectange [rgnMain]已在pos 0,0表格上繪製)

我的期望是紅色矩形將位於PaintBox1畫布的左上角,而不是窗體的畫布。爲什麼不是?區域只能用於具有Windows句柄的控件嗎?

謝謝

+0

我編輯了你的文章,使圖像顯示出來。 – 2010-06-10 02:56:40

+0

謝謝你,梅森 – Xanyx 2010-06-10 05:17:23

回答

2

設備上下文需要窗口句柄。對於非窗口控件,VCL所做的是通過在TWinControl.PaintControls中使用SetWindowOrgEx來抵消爲他們所在的TWinControl獲取的DC視圖端口。新的視圖端口是邏輯單元。因此,對於不從TWinControl下降的TGraphicControl,您可以使用在邏輯座標上工作的GDI函數。請參閱SelectClipRgn的備註部分,其中說明的座標應以設備單位指定。你會offset the region或座標。

+0

感謝塞爾特克,解釋它 – Xanyx 2010-06-10 05:17:00