1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Essentials|Books|Link Back
 
Locate, Display and Execute Control Panel Applets
Page 3: Executing Control Panel Applets from Delphi
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Page 1: CPL: the theory
• Page 2: "Inside" CPL
• Page 4: Full Project CODE
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Finding files with Delphi
• Using Windows API
• Message Handling

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.

Locating Control Panel applications ... done!
Finding and processing Applets .... done!
Executing Applets ...

   Executing Control Panel Applets from Delphi
What we are up to now, is executing the individual applet found inside the Control Panel application. Recall that .CPL files are special-purpose DLLs each carrying one or more Applets.

What first comes to mind is using the ShellExecute API call. Unfortunately, Control Panel Applets are not applications and cannot be opened using the ShellExecute call in a similiar way we would use it to start Windows Explorer, for example.

Control_RunDLL
To be able to execute Applets, a special purpose function named 'Control_RunDLL' has to be imported from the Shell32.DLL library (part of the Windows API).
This is what the importing looks like:

procedure Control_RunDLL(
      hwnd: THandle;  
      hInst: THandle; 
      CmdLine: PChar; 
      CmdShow: integer
      ); stdcall; external 'Shell32.dll';

The CmdLine will look different for each individual applet. Also, there is a tab number option that can be used with some applets to show a particular tab on the Applet.

Here are some examples:

//call the "Game Controllers" Applet
Control_RunDLL(Application.Handle, 0, PChar('joy.cpl'), SW_SHOW)

//call the "Keyboard Properties" Applet (contained inside the Main.cpl file)
Control_RunDLL(Application.Handle, 0, PChar('Main.cpl,Keyboard'), SW_SHOW)

//call the "Mouse Properties" Applet (contained inside the Main.cpl file)
Control_RunDLL(Application.Handle, 0, PChar('Main.cpl,Mouse'), SW_SHOW)

//call the Screen Saver tab of the "Display Properties" Applet
Control_RunDLL(Application.Handle, 0, PChar('Desk.cpl,,1'), SW_SHOW)

Here's how the call to Control_RunDLL looks inside our Delphi clone of the Control Panel folder:

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;

Note: the above procedure handles the OnClick event of the btnExecute TButton we added to our form. Also, the above code cannot be used to show a particular tab on the Applet.

ShellExecute?
I've said that you cannot use the ShellExecute API to execute an Applet. Well, you can. First we build a 'RunControlPanelApplet' function

function RunControlPanelApplet(const sAppletFileName : string) : integer;
begin
 ShellExecute
    (Handle, PChar('open'), 
    PChar('rundll32.exe'), 
    PChar('shell32.dll,Control_RunDLL ' + PChar(sAppletFileName)), 
    nil, SW_NORMAL);
end;

Then call it like:

RunControlPanelApplet('Main.cpl,Mouse');

And here's the Control Panel Applet, called from Delphi code:

Display Properties CPL

That's it!
All set. You now have a working version of the Control Panel folder. If you have any questions, do not hesitate to post on the Delphi Programming Forum.

Of course, don't forget to download all the source code for this project...

Next page > Full Project source code > Page 1, 2, 3, 4

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2003|2002|2001|2000|1999|1998 or by TOPIC.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?
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.