?? picfuns.pas
字號:
unit PicFuns;
interface
uses Windows,Classes,Forms,Controls,Graphics,Math,StdCtrls,
PicConst;
Function GetPicKeyState:TShiftState;
procedure InvalidatePicRect(var ACanvas:TCanvas;dRect: TRect);
Function GetCanvasTextWidth(txt:String;Font:TFont;WnotH:Boolean):Integer;
function CreateLineRgn(xStart, yStart, xEnd, yEnd: Integer): HRGN;
function CreateArcRgn(tl:TPoint;rb:TPoint;arcsp:TPoint;arcep:TPoint): HRGN;
function CreatePieRgn(tl:TPoint;rb:TPoint;arcsp:TPoint;arcep:TPoint; arct:Byte): HRGN;
function PointToCirclePoint(StartPoint:TPoint;EndPoint:TPoint;APoint:TPoint): TPoint;
function GetRectMaxFontSize(dRect:TRect;Text:String;Font:TFont;Flagtag:Integer):Integer;
implementation
Function GetPicKeyState:TShiftState;
begin
Result := [];
if GetKeyState(VK_SHIFT) < 0 then Result := Result + [ssShift];
if GetKeyState(VK_CONTROL) < 0 then Result := Result + [ssCtrl];
if GetKeyState(VK_MENU) < 0 then Result := Result + [ssAlt];
end;
procedure InvalidatePicRect(var ACanvas:TCanvas; dRect: TRect);
var
pp: array[1..2] of TPoint;
updateRect: TRect;
begin
with dRect do begin
pp[1].x := Left - 2 * FOCUS_SIZE;
pp[1].y := Top - 2 * FOCUS_SIZE;
pp[2].x := Right + 2 * FOCUS_SIZE;
pp[2].y := Bottom + 2 * FOCUS_SIZE;
end;
with updateRect do begin //增大區(qū)域,以保證焦點矩形框被清除
Left := pp[1].x - 2 * FOCUS_SIZE;
Top := pp[1].y - 2 * FOCUS_SIZE;
right := pp[2].x + 2 * FOCUS_SIZE;
Bottom := pp[2].y + 2 * FOCUS_SIZE;
end;
ACanvas.Brush.Color:=clBlack;
ACanvas.Brush.Style:=bsSolid;
ACanvas.FillRect(updateRect);
end;
Function GetCanvasTextWidth(txt:String;Font:TFont;WnotH:Boolean):Integer;
var
bt:TBitMap;
begin
bt:=TBitMap.Create;
try
bt.Canvas.Font.Assign(Font);
if WnotH then Result:=bt.Canvas.TextWidth(txt)+2 else Result:=bt.Canvas.TextHeight(Txt)+2;
finally
bt.Free;
end;
end;
function CreateLineRgn(xStart, yStart, xEnd, yEnd: Integer): HRGN;
var
xTemp, yTemp: Integer; //交換起點、終點時使用
diffDistance: Integer; //偏移量
pp: array[1..4] of TPoint;
begin
if xStart = xEnd then
Inc(xEnd);
if yStart = yEnd then
Inc(yEnd);
diffDistance := 5;
if abs(yStart - yEnd) > diffDistance then //直線比較垂直,沿x方向偏移
begin
//調(diào)整直線的點的位置,保證yEnd >=yStart
xTemp := xStart;
yTemp := yStart;
if yStart >= yEnd then
begin
xStart := xEnd;
yStart := yEnd;
xEnd := xTemp;
yEnd := yTemp;
end;
//四邊形的四個頂點賦值
pp[1].x := xStart - diffDistance;
pp[1].y := yStart;
pp[2].x := xStart + diffDistance;
pp[2].y := yStart;
pp[3].x := xEnd + diffDistance;
pp[3].y := yEnd;
pp[4].x := xEnd - diffDistance;
pp[4].y := yEnd;
end
else //直線比較水平,沿y方向偏移
begin
//調(diào)整直線的點的位置,保證XEnd >=XStart
xTemp := xStart;
yTemp := yStart;
if xStart >= xEnd then
begin
xStart := xEnd;
yStart := yEnd;
xEnd := xTemp;
yEnd := yTemp;
end;
pp[1].x := xStart;
pp[1].y := yStart - diffDistance;
pp[2].x := xStart;
pp[2].y := yStart + diffDistance;
pp[3].x := xEnd;
pp[3].y := yEnd + diffDistance;
pp[4].x := xEnd;
pp[4].y := yEnd - diffDistance;
end;
Result := createPolygonRgn(pp[1], 4, ALTERNATE);
end;
function CreateArcRgn(tl:TPoint;rb:TPoint;arcsp:TPoint;arcep:TPoint): HRGN;
var
centerPoint: TPoint; //圓心
asp: Single; //細(xì)長比
smallRadius, bigRadius: Integer; //內(nèi)外圓半徑
radiusY: Integer; //Y向半徑
diffRadius: Integer; //內(nèi)外半徑之差
arcToPolyAngle: Single; //圓弧轉(zhuǎn)換成多邊形的角度
arcToPolyNumber: Integer; //圓弧轉(zhuǎn)換成多邊形的邊數(shù)
arcVertexPoint: array of TPoint; //多邊形的頂點
startAngle, endAngle: Single; //起點和終點的夾角
dx, dy: Integer; //坐標(biāo)之差
aa: Single; //臨時角度
i, j: Integer; //循環(huán)變量
begin
//初始化參數(shù)
diffRadius := 5;
arcToPolyAngle := 5.0 * PI / 180;
//圓心
centerPoint.x := (rb.X + tl.X) div 2;
centerPoint.y := (tl.Y + rb.Y) div 2;
//Y向半徑
radiusY := abs(rb.Y - tl.Y) div 2;
//細(xì)長比
asp := abs((rb.X - tl.X) / (rb.Y - tl.Y));
//****** 計算角度 ******
//起點角度
dx := Arcsp.X - centerPoint.x;
dy := Arcsp.Y - centerPoint.y;
//起點角度的絕對值
if dx = 0 then startAngle := PI / 2 else startAngle := Abs(ArcTan(dy / dx * asp));
//確定起點角度的大小
if (dx > 0) and (dy < 0) then
startAngle := startAngle
else if (dx < 0) and (dy < 0) then
startAngle := PI - startAngle
else if (dx < 0) and (dy > 0) then
startAngle := PI + startAngle
else
startAngle := PI + PI - startAngle;
//終點角度
dx := arcep.X - centerPoint.x;
dy := arcep.Y - centerPoint.y;
//終點角度絕對值
if dx = 0 then endAngle := PI / 2 else endAngle := Abs(ArcTan(dy / dx * asp));
//確定終點角度的大小
if (dx > 0) and (dy < 0) then
endAngle := endAngle
else if (dx < 0) and (dy < 0) then
endAngle := PI - endAngle
else if (dx < 0) and (dy > 0) then
endAngle := PI + endAngle
else
endAngle := PI + PI - endAngle;
//計算內(nèi)外圓
smallRadius := radiusY - diffRadius;
bigRadius := radiusY + diffRadius;
//計算頂點數(shù)
if endAngle < startAngle then begin //終點角度小于起點角度
arcToPolyNumber := Trunc((2 * PI + (endAngle - startAngle)) / arcToPolyAngle) + 2;
end else begin //終點角度大于起點角度
arcToPolyNumber := Trunc((endAngle - startAngle) / arcToPolyAngle) + 2;
end;
//設(shè)置頂點數(shù)組
SetLength(arcVertexPoint, 2 * arcToPolyNumber + 2);
//計算外圓
for i := 1 to arcToPolyNumber - 1 do begin
aa := startAngle + (i - 1) * arcToPolyAngle;
arcVertexPoint[i].x := Round(centerPoint.x + bigRadius * cos(aa) * asp);
arcVertexPoint[i].y := Round(centerPoint.y - bigRadius * sin(aa));
end;
arcVertexPoint[arcToPolyNumber].x := Round(centerPoint.x + bigRadius * Cos(endAngle) * asp);
arcVertexPoint[arcToPolyNumber].y := Round(centerPoint.y - bigRadius * Sin(endAngle));
//計算內(nèi)圓
j := 1;
arcVertexPoint[arcToPolyNumber + j].x := Round(centerPoint.x + smallRadius * Cos(endAngle) * asp);
arcVertexPoint[arcToPolyNumber + j].y := Round(centerPoint.y - smallRadius * Sin(endAngle));
for i := arcToPolyNumber - 1 downto 1 do begin
Inc(j);
aa := startAngle + (i - 1) * arcToPolyAngle;
arcVertexPoint[arcToPolyNumber + j].x := Round(centerPoint.x + smallRadius * cos(aa) * asp);
arcVertexPoint[arcToPolyNumber + j].y := Round(centerPoint.y - smallRadius * sin(aa));
end;
arcVertexPoint[2 * arcToPolyNumber + 1] := arcVertexPoint[1];
arcVertexPoint[2 * arcToPolyNumber + 1].x := arcVertexPoint[1].x;
arcVertexPoint[2 * arcToPolyNumber + 1].y := arcVertexPoint[1].y;
for i := 0 to 2 * arcToPolyNumber do arcVertexPoint[i] := arcVertexPoint[i + 1];
Result := createPolygonRgn(arcVertexPoint[0], 2 * arcToPolyNumber, ALTERNATE);
end;
function CreatePieRgn(tl:TPoint;rb:TPoint;arcsp:TPoint;arcep:TPoint; arct:Byte): HRGN;
//arcShape =1 Chord arcShape =2 pie
var
centerPoint: TPoint; //圓心
asp: Single; //細(xì)長比
bigRadius: Integer; //外圓半徑
radiusY: Integer; //Y向半徑
diffRadius: Integer; //內(nèi)外半徑之差
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -