1. TControlCanvas를 이용하는 방법
type TForm1 = class(TForm) Edit1: TEdit; ... private FCanvas: TCanvas; ... procedure TForm1.FormCreate(Sender: TObject); begin FCanvas := TControlCanvas.Create; TControlCanvas(FCanvas).Control := Edit1; end; procedure TForm1.자기함수... begin FCanvas.LineTo(Edit1.Width, Edit1.Height); end;
2. TCanvas를 이용하는 방법.
var EdtCanvas: TCanvas; begin EdtCanvas:=TCanvas.Create; EdtCanvas.Handle:=GetDC(Edit1.Handle); ... //원하는대로 사용하구.. EdtCanvas.MoveTo(.. EdtCanvas.LineTo(... ... ReleaseDC(EdtCanvas.Handle); EdtCanvas.Free; end;
TControlCanvas가 TCanvas를 상속 받아 DC를 할당하는 것을 구현해놓은 클래스이기 때문에, TCanvas를 이용하는 방법보다는 편함.
Controls.Pas에 포함되어 있는 TControlCanvas의 구현 부분 소스
{ TControlCanvas } const CanvasListCacheSize = 4; var CanvasList: TThreadList; // Free the first available device context procedure FreeDeviceContext; var I: Integer; begin with CanvasList.LockList do try for I := 0 to Count-1 do with TControlCanvas(Items[I]) do if TryLock then try FreeHandle; Exit; finally Unlock; end; finally CanvasList.UnlockList; end; end; procedure FreeDeviceContexts; var I: Integer; begin with CanvasList.LockList do try for I := Count-1 downto 0 do with TControlCanvas(Items[I]) do if TryLock then try FreeHandle; finally Unlock; end; finally CanvasList.UnlockList; end; end; destructor TControlCanvas.Destroy; begin FreeHandle; inherited Destroy; end; procedure TControlCanvas.CreateHandle; begin if FControl = nil then inherited CreateHandle else begin if FDeviceContext = 0 then begin with CanvasList.LockList do try if Count >= CanvasListCacheSize then FreeDeviceContext; FDeviceContext := FControl.GetDeviceContext(FWindowHandle); Add(Self); finally CanvasList.UnlockList; end; end; Handle := FDeviceContext; UpdateTextFlags; end; end; procedure TControlCanvas.FreeHandle; begin if FDeviceContext <> 0 then begin Handle := 0; CanvasList.Remove(Self); ReleaseDC(FWindowHandle, FDeviceContext); FDeviceContext := 0; end; end; procedure TControlCanvas.SetControl(AControl: TControl); begin if FControl <> AControl then begin FreeHandle; FControl := AControl; end; end; procedure TControlCanvas.UpdateTextFlags; begin if Control = nil then Exit; if Control.UseRightToLeftReading then TextFlags := TextFlags or ETO_RTLREADING else TextFlags := TextFlags and not ETO_RTLREADING; end;