Programmming examples











Calling HtmlHelp() from Borland C++
The HTMLHelp.lib file provided by Microsoft cannot directly be used in Borland C++, but the HTMLHelp control can be dynamicaly loaded and it's functions called.
New version of the htmlhelp.h file can be downloaded from OWLNext site: http://owlnext.sourceforge.net/add_ons/add_fil.zip

typedef HWND WINAPI (*HtmlHelpFunc)( HWND hwndCaller, LPCSTR pszFile,
UINT uCommand, DWORD dwData );

const char pszHelpOCXCtrl [] = "hhctrl.ocx";

hHelpOCX = ::LoadLibrary(pszHelpOCXCtrl);
if (!hHelpOCX) {
   ::MessageBox(hwndCaller, "Cannot use the html help!", "Error", MB_OK);
  return;
}

HTMLHelp = (HtmlHelpFunc)::GetProcAddress(hHelpOCX, ATOM_HTMLHELP_API_ANSI);

// Now call HTMLHelp functions:

HTMLHelp(hWnd, "myhelp.chm", HH_DISPLAY_TOPIC, 0);
HTMLHelp(hWnd, "myhelp.chm", HH_HELP_CONTEXT, id);



LaunchApp()
This is an example how to call external application (like PKZip or a MSDOS batch file) and wait till it completes it's execution.

void LaunchApp(const char *program, char *cmdline, const char *path)
{
  char *buf = 0;
  if (cmdline)    // The lpCommandLine parameter may be modified by the call to CreateProcess(),
  {               // so make a temporary buffer to pass it safely
    buf = new char[strlen(cmdline) + 1];
    strcpy(buf, cmdline);
  }

  STARTUPINFO info;
  ZeroMemory(&info, sizeof(STARTUPINFO));
  info.cb = sizeof(STARTUPINFO);

  PROCESS_INFORMATION process_info;

  CreateProcess(program, buf, 0, 0, false, CREATE_NEW_CONSOLE, 0, path, &info, &process_info);

  WaitForSingleObject(process_info.hProcess, INFINITE);

  delete [] buf;
}



BrowseForFolder()
The Windows API function BrowseForFolder() is used to prompt the user for a folder. This code example demonstrates how to use function, and how to set the initial folder. The code taken from the MSDN article, and modified to use the OWL/OWLNext TShell class.

// Callback for the BrowseForFolder, which purpose is to set the initial folder

int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM /*lParam*/, LPARAM lpData)
{
  switch (uMsg) 
  {
    case BFFM_INITIALIZED:
      ::SendMessage(hwnd, BFFM_SETSELECTION, (WPARAM)TRUE, lpData);
      break;
  }

  return 0;
}


// The initial folder, and the result of the function are contained in buf

bool BrowseForFolder(HWND hWnd, char *buf)
{
  // Global pointer to the shell's IMalloc interface.

  LPMALLOC g_pMalloc;

  // Get the shell's allocator.

  if (!SUCCEEDED(TShell::SHGetMalloc(&g_pMalloc)))
    return false;

  BROWSEINFO     bi;
  LPITEMIDLIST  pidlMyComputer;  // PIDL for MyComputer folder

  LPITEMIDLIST  pidlBrowse;      // PIDL selected by user


  // Get the PIDL for the Programs folder.

  if (!SUCCEEDED(TShell::SHGetSpecialFolderLocation(hWnd, CSIDL_DRIVES, &pidlMyComputer)))
    return false;

  // Fill in the BROWSEINFO structure.

  bi.hwndOwner = hWnd;
  bi.pidlRoot = pidlMyComputer;
  bi.pszDisplayName = buf;
  bi.lpszTitle = "Select root folder";
  bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_RETURNFSANCESTORS;
  bi.lpfn = BrowseCallbackProc;
  bi.lParam = (LPARAM)buf;

  // Browse for a folder and return its PIDL.

  pidlBrowse = TShell::SHBrowseForFolder(&bi);
  bool result = (pidlBrowse != 0);
  if (result) 
  {
    if (!TShell::SHGetPathFromIDList(pidlBrowse, buf))
      result = false;

    // Free the PIDL returned by SHBrowseForFolder.

    g_pMalloc->Free(pidlBrowse);
  }

  // Clean up.

  g_pMalloc->Free(pidlMyComputer);

  return result;
}



TFileOpenDialog with multiple files selection
When both the flags OFN_EXPLORER and OFN_ALLOWMULTISELECT are used in FileOpen dialog, and more than one file is selected, the returned result has the following format: The path to the directory, followed by the filenames of the selected files, using '\0' as delimiters. The last filename is followed by two '\0'. Here is an example how to work with the OWL class TFileOpenDialog:

  TOpenSaveDialog::TData FileData;
  FileData.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ALLOWMULTISELECT;
  FileData.SetFilter("VRML Files (*.vrml;*.wrl;*.wrz)|*.vrml;*.wrl;*.wrz|All Files (*.*)|*.*|");
  FileData.DefExt = "wrl";

  if (TFileOpenDialog(this, FileData).Execute() == IDOK)
  {
    char buf[_MAX_PATH + 1];

    char *p = FileData.FileName + strlen(FileData.FileName) + 1;

    do
    {
      strcpy(buf, FileData.FileName);
      if (*p)   // The case of multiple file selection
      {
        strcat(buf, "\\");
        strcat(buf, p);
      }

      // Process the file
      MessageBox(buf);

      // go to next file name
      if (*p)
        p = p + strlen(p) + 1;

    } while (*p);
  }


Back to home Back to main