2010. július 17., szombat

Set the Desktop as the initial directory


In a TOpenDialog, you can set the initial directory. How do I set it as the desktop? I could set it as c:\windows\desktop, but then what if Windows is not on the user's c drive?


There is a shell function that can be used to inquire about the location of several shell-related folders. You need to add ShlObj to the uses clause for this, plus ActiveX for the CoTaskMemFree.

procedure FreePidl(pidl: PItemIDList);

procedure TForm1.Button2Click(Sender: TObject);
  pidl: PItemIDList;
  buf: array[0..MAX_PATH] of Char;
  if Succeeded(ShGetSpecialFolderLocation(Handle, CSIDL_DESKTOP, pidl)) then
    if ShGetPathfromIDList(pidl, buf) then

See win32.hlp (or better msdn.microsoft.com, the list has been extended for Win98/2000) for a list of CSIDL values. There is also a newer ShGetSpecialFolderPath API that directly returns the path, but it is not available on older Win95 and NT installations.

Nincsenek megjegyzések:

Megjegyzés küldése