delphi 排列桌面图标


本文整理自网络,侵删。

  本文主要介绍了在delphi编程中如何玩转Windows桌面图标,下面来看具体的步骤: 
1、  新建一工程,在 uses 中加入 CommCtrl 单元,窗体上加一个按钮;
2、  声明一个取得桌面句柄的函数:
function TForm1.GetDesktopHand: THandle;
begin
  Result:=FindWindow(progman,nil);
  Result:=GetWindow(Result,GW_Child);
  Result:=GetWindow(Result,GW_Child);
end;
3、  声明一个设置图标文字颜色的过程:
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;
 
有了上面的两个方法,你已经可以对桌面动小手术了。下面介绍图标的排列方式。
 4、  以屏幕的中心为圆点作圆形排列:
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;
 5、  图标右对齐:
procedure 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;  // 工作区的宽(即屏幕的宽);
  DesktopHeight:= TempRect.Bottom - TempRect.Top;  // 工作区的高(即屏幕的高);
  I:=0;  // 图标所排的列数
  J:=0;
  For h:=0 to Listview_GetItemCount(Hand)-1 do
Begin
  Inc(j);
  If  j*rec>DesktopHeight  then  // 排完一列;
    Begin
      Inc(i);  // 换列
      J:=1;
    End;
  SendMessage(Hand,LVM_SetItemPosition,h,
MakeLparam(DesktopWidth-Rec*(I+1),Rec*(j-1));
End; //  for 循环结束;
End;
 6、  在按钮的单击事件中加入代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
  SetTextColor(clBlack,crNone); // 设置图标文字颜色;
  Circle(200);  // 把图标排列成半径为200的圆;
  // AlignRight(77); // 右对齐;
end;
 编译运行,单击按钮。哇塞!太棒了!你还可发挥你的想象力,对程序稍加改进,把图标排成蛇形、椭圆形、环形等等。



相关阅读 >>

Delphi 关闭icesword等

Delphi android检查互联网连接

Delphi b编码以及bt种子文件分析

Delphi最简化异步选择tcp服务器

Delphi编程时按回车键无效,无法换行解决办法

Delphi中将dbgrid中的内容输出到word中

Delphi通过setupapi.dll列举和停用硬件设备

Delphi 获取一个文件夹下的所有文件

Delphi idhttp post json

Delphi xe 打开andorid gps设置

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...