1. Home
  2. Computing & Technology
  3. Delphi Programming

{
Article: 
Locate, Display and Execute Control Panel Applets

http://delphi.about.com/library/weekly/aa062403a.htm

Interested in Delphi code to mimic the Windows Control Panel 
folder behavior? In this article you can learn how to find 
CPL files, how to extract description, name and even the 
applet icon. Even more, learn how to execute applets from 
your applications.
}


Download zipped project


MainForm.dfm

Select Form1 (empty project), Right Click to get context popup menu, Select View As Text, Paste the text into Editor, Select View As Form. object MainForm: TMainForm Left = 343 Top = 246 Width = 440 Height = 284 Caption = 'MainForm' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object lvCPL: TListView Left = 0 Top = 41 Width = 432 Height = 216 Align = alClient Columns = < item Caption = 'Name' end item Caption = 'Comment' end item Caption = 'FileName' end> TabOrder = 0 ViewStyle = vsReport end object pnlTop: TPanel Left = 0 Top = 0 Width = 432 Height = 41 Align = alTop BevelOuter = bvLowered TabOrder = 1 object btnLocateCPL: TButton Left = 8 Top = 8 Width = 75 Height = 25 Caption = 'Locate' TabOrder = 0 OnClick = btnLocateCPLClick end object btnExecute: TButton Left = 104 Top = 8 Width = 75 Height = 25 Caption = 'Execute' TabOrder = 1 OnClick = btnExecuteClick end end object cplIcons: TImageList Left = 216 Top = 8 end end

MainUnit.pas

unit MainUnit; interface uses CPL, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls, ImgList; type TMainForm = class(TForm) lvCPL: TListView; pnlTop: TPanel; btnLocateCPL: TButton; cplIcons: TImageList; btnExecute: TButton; procedure btnLocateCPLClick(Sender: TObject); procedure btnExecuteClick(Sender: TObject); procedure FormCreate(Sender: TObject); private procedure GetCPLList(FileList : TStrings); procedure ProcessCPLApplet(sCPLName : string); public { Public declarations } end; var MainForm: TMainForm; implementation {$R *.dfm} procedure Control_RunDLL(hwnd: THandle; hInst: THandle; CmdLine: PChar; CmdShow: integer); stdcall; external 'Shell32.dll'; (* //as "defined" in CPL.pas const CPL_DYNAMIC_RES = 0; CPL_INIT = 1; CPL_GETCOUNT = 2; CPL_INQUIRE = 3; CPL_SELECT = 4; CPL_DBLCLK = 5; CPL_STOP = 6; CPL_EXIT = 7; CPL_NEWINQUIRE = 8; CPL_STARTWPARMS = 9; CPL_SETUP = 200; type TCPlAppletFunc = function( hwndCPl: HWnd; // handle to Control Panel window uMsg: DWord; // message lParam1: Longint; // first message parameter lParam2: Longint // second message parameter ): Longint; stdcall; TCPLInfo = record idIcon : integer; idName : integer; idInfo : integer; lData : LongInt; end; *) (* function RunControlPanelApplet(const sAppletFileName : string) : integer; begin ShellExecute(Handle, PChar('open'), PChar('rundll32.exe'), PChar('shell32.dll,Control_RunDLL ' + PChar(sAppletFileName) + ',0'), nil, SW_NORMAL); end; *) function LoadStringFromModule(Module: HInst; ID: Integer): string; const MaxLen = 2048; var CharCount: Integer; begin SetLength(Result, MaxLen); CharCount := LoadString(Module, ID, PChar(Result), MaxLen); if CharCount > 0 then SetLength(Result, CharCount) end; procedure TMainForm.btnLocateCPLClick(Sender: TObject); var sl : TStrings; i : cardinal; begin lvCPL.Clear; cplIcons.Clear; sl:=TStringList.Create; try GetCPLList(sl); for i := 0 to -1 + sl.Count do begin Caption:='Processing ' + IntToStr(i) + '/' + IntToStr(-1 + sl.Count); ProcessCPLApplet(sl[i]); end; Caption:='CPL Locate and Execute with Delphi...'; finally sl.Free; end; end; procedure TMainForm.GetCPLList(FileList : TStrings); var CPLFileMask : String; SearchRec : TSearchRec; SystemPath: array[0..MAX_PATH + 1] of char; begin GetSystemDirectory(SystemPath, MAX_PATH); CPLFileMask := IncludeTrailingPathDelimiter(SystemPath) + '*.CPL'; //locate *.CPL files inside "System" folder if FindFirst(CPLFileMask, faAnyFile - faDirectory, SearchRec) = 0 then begin repeat FileList.Add(SearchRec.Name); until FindNext(SearchRec) <> 0; FindClose(SearchRec); end; end; procedure TMainForm.ProcessCPLApplet(sCPLName : string); var H: HInst; CPlApplet: TCPLApplet; //TCPlAppletFunc; NumberOfApplets: Integer; AppletInfo: TCPLInfo; I: Integer; Name, Comment: string; TheIcon : TIcon; begin // Load CPL H := LoadLibrary(PChar(sCPLName)); if H <> 0 then try // Locate CPlApplet Function from Module CPlApplet:= GetProcAddress(H, 'CPlApplet'); if Assigned(CPlApplet) then begin // Get Number of Applets contained in the .cpl file NumberOfApplets:= CPlApplet(Application.Handle, CPL_GetCount, 0, 0); for i := 0 to -1 + NumberOfApplets do begin // Pick Name and Comment CPlApplet(Application.Handle, CPL_INQUIRE, i, Longint(@AppletInfo)); Name:= LoadStringFromModule(H, AppletInfo.idName); Comment:= LoadStringFromModule(H, AppletInfo.idInfo); // Display info if Name <> '' then begin with lvCPL.Items.Add do begin TheIcon:=TIcon.Create; try //grab the Applet icon TheIcon.Handle:=LoadIcon(H, PChar(AppletInfo.idIcon)); ImageIndex := cplIcons.AddIcon(TheIcon); finally TheIcon.Free; end; //add to ListView Caption := Name; SubItems.Add(Comment); SubItems.Add(sCPLName); end; end; end; end; finally FreeLibrary(H); end; end; procedure TMainForm.btnExecuteClick(Sender: TObject); var li : TListItem; begin li:=lvCPL.Selected; if li <> nil then begin Control_RunDLL(Application.Handle, 0, PChar(li.SubItems[1] + ',' + li.Caption), SW_SHOW); end; end; procedure TMainForm.FormCreate(Sender: TObject); begin lvCPL.SmallImages := cplIcons; lvCPL.ReadOnly := True; lvCPL.RowSelect := True; end; end. { ******************************************** Zarko Gajic About.com Guide to Delphi Programming http://delphi.about.com email: delphi.guide@about.com free newsletter: http://delphi.about.com/library/blnewsletter.htm forum: http://forums.about.com/ab-delphi/start/ ******************************************** }
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.