📄 bsutils.pas
字号:
function GetTextWidth(Str: WideString): Integer;
var
R: TRect;
begin
R := Rect(0, 0, 0, 0);
BSDrawSkinText(C, Str, R, DT_CALCRECT);
Result := RectWidth(R);
end;
var
j: Integer;
begin
j := Length(S);
if GetTextWidth(S) > w
then
begin
repeat
Delete(S, j, 1);
Dec(j);
until (GetTextWidth(S + '...') <= w) or (S = '');
S := S + '...';
end;
end;
procedure CorrectTextbyWidth(C: TCanvas; var S: String; W: Integer);
var
j: Integer;
begin
j := Length(S);
with C do
begin
if TextWidth(S) > w
then
begin
repeat
Delete(S, j, 1);
Dec(j);
until (TextWidth(S + '...') <= w) or (S = '');
S := S + '...';
end;
end;
end;
function Max(A, B: Longint): Longint;
begin
if A > B then Result := A
else Result := B;
end;
function Min(A, B: Longint): Longint;
begin
if A < B then Result := A
else Result := B;
end;
function ReplaceStr(const S, Srch, Replace: string): string;
var
I: Integer;
Source: string;
begin
Source := S;
Result := '';
repeat
I := Pos(Srch, Source);
if I > 0 then begin
Result := Result + Copy(Source, 1, I - 1) + Replace;
Source := Copy(Source, I + Length(Srch), MaxInt);
end
else Result := Result + Source;
until I <= 0;
end;
function BSDrawSkinText(ACanvas: TCanvas; AText: WideString; var Bounds: TRect; Flag: cardinal): integer;
var
AnsiText: string;
begin
if BS_PlatformIsUnicode
then
Result := Windows.DrawTextW(ACanvas.Handle, PWideChar(AText), Length(AText), Bounds, Flag)
else
begin
AnsiText := WideCharToString(PWideChar(AText));
Result := Windows.DrawText(ACanvas.Handle, PChar(AnsiText), Length(AnsiText), Bounds, Flag);
end;
end;
procedure BSDrawText4(Cnvs: TCanvas; S: String; R: TRect);
begin
if S = '' then Exit;
if S[1] = 'W' then Inc(R.Right, 2);
DrawText(Cnvs.Handle, PChar(S), Length(S), R,
DT_VCENTER or DT_SINGLELINE or DT_CENTER);
end;
procedure BSDrawText(Cnvs: TCanvas; S: String; R: TRect);
begin
if S = '' then Exit;
DrawText(Cnvs.Handle, PChar(S), Length(S), R,
DT_VCENTER or DT_SINGLELINE or DT_LEFT);
end;
procedure BSDrawText2(Cnvs: TCanvas; S: String; R: TRect);
var
TX, TY: Integer;
begin
if S = '' then Exit;
TX := R.Left + 2;
TY := R.Top + RectHeight(R) div 2 - Cnvs.TextHeight(S) div 2;
Cnvs.TextRect(R, TX, TY, S);
end;
procedure BSDrawText3(Cnvs: TCanvas; S: String; R: TRect; HorOffset: Integer);
var
TX, TY: Integer;
begin
if S = '' then Exit;
TX := R.Left + 2 + HorOffset;
TY := R.Top + RectHeight(R) div 2 - Cnvs.TextHeight(S) div 2;
Cnvs.TextRect(R, TX, TY, S);
end;
procedure DrawTrackArrowImage(Cnvs: TCanvas; R: TRect; Color: TColor);
var
X, Y, i: Integer;
begin
X := R.Left + RectWidth(R) div 2;
Y := R.Top + RectHeight(R) div 2 + 2;
for i := 2 downto 0 do
with Cnvs do
begin
Pen.Color := Color;
MoveTo(X - i, Y - i);
LineTo(X + i + 1, Y - i);
end;
end;
procedure DrawArrowImage(Cnvs: TCanvas; R: TRect; Color: TColor; Code: Integer);
var
i: Integer;
X, Y: Integer;
begin
with Cnvs do
begin
Pen.Color := Color;
case Code of
1:
begin
X := R.Left + RectWidth(R) div 2 - 2;
Y := R.Top + RectHeight(R) div 2;
for i := 0 to 3 do
begin
MoveTo(X + i, Y - i);
LineTo(X + i, Y + i + 1);
end;
end;
2:
begin
X := R.Left + RectWidth(R) div 2 + 2;
Y := R.Top + RectHeight(R) div 2;
for i := 3 downto 0 do
begin
MoveTo(X - i, Y + i);
LineTo(X - i, Y - i - 1);
end;
end;
3:
begin
X := R.Left + RectWidth(R) div 2;
Y := R.Top + RectHeight(R) div 2 - 2;
for i := 0 to 3 do
begin
MoveTo(X - i, Y + i);
LineTo(X + i + 1, Y + i);
end;
end;
4:
begin
X := R.Left + RectWidth(R) div 2;
Y := R.Top + RectHeight(R) div 2 + 2;
for i := 3 downto 0 do
begin
MoveTo(X - i, Y - i);
LineTo(X + i + 1, Y - i);
end;
end;
end;
end;
end;
procedure DrawRadioImage(Cnvs: TCanvas; X, Y: Integer; Color: TColor);
begin
with Cnvs do
begin
Pen.Color := Color;
Brush.Color := Color;
Rectangle(X, Y, X + 6, Y + 6);
end;
end;
procedure DrawCheckImage(Cnvs: TCanvas; X, Y: Integer; Color: TColor);
var
i: Integer;
begin
with Cnvs do
begin
Pen.Color := Color;
for i := 0 to 2 do
begin
MoveTo(X, Y + 5 - i);
LineTo(X + 2, Y + 7 - i);
LineTo(X + 7, Y + 2 - i);
end;
end;
end;
procedure Frm3D;
procedure DoRect;
var
TopRight, BottomLeft: TPoint;
begin
with Canvas, Rect do
begin
TopRight.X := Right;
TopRight.Y := Top;
BottomLeft.X := Left;
BottomLeft.Y := Bottom;
Pen.Color := TopColor;
PolyLine([BottomLeft, TopLeft, TopRight]);
Pen.Color := BottomColor;
Dec(BottomLeft.X);
PolyLine([TopRight, BottomRight, BottomLeft]);
end;
end;
begin
Canvas.Pen.Width := 1;
Dec(Rect.Bottom); Dec(Rect.Right);
DoRect;
end;
procedure GetWindowsVersion(var Major, Minor: Integer);
var
Ver : Longint;
begin
Ver := GetVersion;
Major := LoByte(LoWord(Ver));
Minor := HiByte(LoWord(Ver));
end;
function Is9XOS: Boolean;
var
Major, Minor : Integer;
begin
GetWindowsVersion(major, minor);
Result := (major < 5);
end;
function IsVistaOs: Boolean;
var
Major, Minor : Integer;
begin
GetWindowsVersion(major, minor);
Result := (major >= 6);
end;
function CheckWXP: Boolean;
var
Major, Minor : Integer;
begin
GetWindowsVersion(major, minor);
Result := (major > 5) or ((major = 5) and (minor = 1));
end;
function CheckW2kWXP: Boolean;
var
Major, Minor : Integer;
begin
GetWindowsVersion(major, minor);
Result := (major > 5) or ((major = 5) and ((minor = 0) or (minor = 1) or (minor = 2)));
end;
procedure SetAlphaBlendTransparent;
var
User32: Cardinal;
SetLayeredWindowAttributes: function (hwnd: LongInt; crKey: byte; bAlpha: byte; dwFlags: LongInt): LongInt; stdcall;
begin
User32 := LoadLibrary('USER32');
if User32 <> 0
then
try
SetLayeredWindowAttributes := GetProcAddress(User32, 'SetLayeredWindowAttributes');
if @SetLayeredWindowAttributes <> nil
then
SetLayeredWindowAttributes(WHandle, 0, Value, LWA_ALPHA);
finally
FreeLibrary(User32);
end;
end;
procedure GetScreenImage(X, Y: Integer; B: TBitMap);
var
DC: HDC;
begin
DC := GetDC(0);
BitBlt(B.Canvas.Handle, 0, 0,
B.Width, B.Height, DC, X, Y, SrcCopy);
ReleaseDC(0, DC);
end;
function EqPoints(const Pt1, Pt2: TPoint): Boolean;
begin
Result := (Pt1.X = Pt2.X) and (Pt1.Y = Pt2.Y);
end;
function EqRects(R1, R2: TRect): Boolean;
begin
Result := (R1.Left = R2.Left) and (R1.Top = R2.Top) and
(R1.Right = R2.Right) and (R1.Bottom = R2.Bottom);
end;
function NullRect: TRect;
begin
Result := Rect(0, 0, 0, 0);
end;
function IsNullRect(R: TRect): Boolean;
begin
Result := (R.Right - R.Left <= 0) or (R.Bottom - R.Top <= 0)
end;
function IsNullPoint(P: TPoint): Boolean;
begin
Result := (P.X = 0) or (P.Y = 0);
end;
function PointInRect(R: TRect; P: TPoint): Boolean;
begin
Result := (P.X >= R.Left) and (P.Y >= R.Top) and
(P.X <= R.Right) and (P.Y <= R.Bottom);
end;
function RectInRect(R1, R2: TRect): Boolean;
begin
Result := PtInRect(R2, R1.TopLeft) and PtInRect(R2, R1.BottomRight)
end;
function RectToCenter(var R: TRect; Bounds: TRect): TRect;
begin
OffsetRect(R, -R.Left, -R.Top);
OffsetRect(R, (RectWidth(Bounds) - RectWidth(R)) div 2, (RectHeight(Bounds) - RectHeight(R)) div 2);
OffsetRect(R, Bounds.Left, Bounds.Top);
Result := R;
end;
function RectWidth;
begin
Result := R.Right - R.Left;
end;
function RectHeight;
begin
Result := R.Bottom - R.Top;
end;
const
nums = '1234567890';
symbols = ', ';
function GetPoint(S: String): TPoint;
var
i, j: Integer;
S1: String;
SA: array[1..2] of String;
begin
S1 := '';
j := 1;
for i := 1 to Length(S) do
begin
if S[i] = ','
then
begin
SA[j] := S1;
S1 := '';
Inc(j);
end
else
if Pos(S[i], nums) <> 0 then S1 := S1 + S[i];
end;
SA[j] := S1;
Result := Point(StrToInt(SA[1]), StrToInt(SA[2]));;
end;
function GetRect(S: String): TRect;
var
i, j: Integer;
S1: String;
SA: array[1..4] of String;
begin
S1 := '';
j := 1;
for i := 1 to Length(S) do
begin
if S[i] = ','
then
begin
SA[j] := S1;
S1 := '';
Inc(j);
end
else
if Pos(S[i], nums) <> 0 then S1 := S1 + S[i];
end;
SA[j] := S1;
Result := Rect(StrToInt(SA[1]), StrToInt(SA[2]),
StrToInt(SA[3]), StrToInt(SA[4]));
end;
function SetRect(R: TRect): String;
begin
Result := IntToStr(R.Left) + ',' +
IntToStr(R.Top) + ',' + IntToStr(R.Right) + ',' +
IntToStr(R.Bottom);
end;
function ReadRect;
var
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -