unit unitFrmMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, CommCtrl;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
// 声明一个取得桌面句柄的函数:
function GetDesktopHand: THandle;
// 声明一个设置图标文字颜色的过程:
procedure SetTextColor(ForeClr, BackClr: TColor);
public
{ Public declarations }
// 以屏幕的中心为圆点作圆形排列:
procedure Circle(r: integer); // 形参 r 为半径;
// 图标右对齐:
procedure AlignRight(Rec: Integer); // 形参 Rec 为一个图标所占区域大小,一般为77;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
// 声明一个取得桌面句柄的函数:
function TForm1.GetDesktopHand: THandle;
begin
Result := FindWindow('progman', nil);
Result := GetWindow(Result, GW_Child);
Result := GetWindow(Result, GW_Child);
end;
// 声明一个设置图标文字前景和背景颜色的过程:
procedure TForm1.SetTextColor(ForeClr, BackClr: TColor);
var Hand: THandle;
begin
Hand := GetDesktopHand;
Listview_SetTextColor(Hand, ForeClr); // 设置文字前景色;
Listview_SetTextBkColor(Hand, BackClr); // 设置文字背景色,crNone 为透明;
Listview_RedrawItems(Hand, 0, Listview_GetItemCount(Hand)); // 重画;
end;
// 以屏幕的中心为圆点作圆形排列:
procedure TForm1.Circle(r: integer); // 形参 r 为半径;
var
i, Count, CenterX, CenterY, TempR: integer;
Hand: THandle;
Radian: double;
TempRect: TRect;
DesktopHeight, DesktopWidth: integer;
X, Y: Word;
begin
Hand := GetDesktopHand;
SystemParametersInfo(SPI_GetWorkArea, 0, @TempRect, 0); // 取得工作区域;
DesktopWidth := TempRect.Right - TempRect.Left; // 工作区的宽(即屏幕的宽);
DesktopHeight := TempRect.Bottom - TempRect.Top; // 工作区的高(即屏幕的高);
CenterX := DesktopWidth div 2; // 取得圆心 X 坐标;
CenterY := DesktopHeight div 2; // 圆心 Y 坐标;
if CenterX > CenterY then
TempR := CenterY
else
TempR := CenterX;
if r > TempR then r := TempR; // 半径不能超过屏幕中心点到四边的最短距离;
Count := Listview_GetItemCount(Hand); // 桌面上图标个数;
Radian := 2 * 3.14159 / Count; // 相邻图标间的弧度;
for i := 0 to Count - 1 do
begin
// 第一个图标排在正上方;
X := Integer(CenterX + Trunc(r * Sin(i * Radian))); // 图标的X坐标;
Y := Integer(CenterY + Trunc(r * Cos(i * Radian))); // 图标的Y坐标;
SendMessage(Hand, LVM_SetItemPosition, i, MakeLparam(X, y)); // 设置坐标;
end;
end;
// 图标右对齐:
procedure Tform1.AlignRight(Rec: Integer); // 形参 Rec 为一个图标所占区域大小,一般为77;
var Hand: THandle;
h, I, j, DesktopHight, DesktopWidth: integer;
TempRect: TRect;
begin
Hand := GetDesktopHand;
SystemParametersInfo(SPI_GetWorkArea, 0, @TempRect, 0); // 取得工作区域;
DesktopWidth := TempRect.Right - TempRect.Left; // 工作区的宽(即屏幕的宽)
DesktopHight := TempRect.Bottom - TempRect.Top; // 工作区的高(即屏幕的高);
I := 0; // 图标所排的列数
J := 0;
for h := 0 to Listview_GetItemCount(Hand) - 1 do
begin
Inc(j);
if j * rec > DesktopHight then // 排完一列;
begin
Inc(i); // 换列
J := 1;
end;
SendMessage(Hand, LVM_SetItemPosition, h,
MakeLparam(DesktopWidth - Rec * (I + 1), Rec * (j - 1)));
end; // for 循环结束;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetTextColor(clYellow, crNone); // 设置图标文字颜色;
Circle(300); // 把图标排列成半径为200的圆
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
AlignRight(70); // 右对齐
end;
end.