Add Helper Functions

You need to add several functions to your C-language source file to support the OpenDlg dialog function. These functions are listed as follows:

UpdateListBox

Fills the list box in the Open dialog box with the specified files.

SeparateFile

Divides a pathname into separate path and filename parts.

ChangeDefExt

Copies the filename extension from a filename to a buffer, as long as the extension has no wildcard characters.

AddExt

Appends an extension to a filename if one does not already exist.

The UpdateListBox function builds a pathname by concatenating the default path and filename, then passes this pathname to the list box using the DlgDirList function. This function fills the list box with the names of the files and directories identified by the pathname. Add the following statements to the C-language source file:

void UpdateListBox(hDlg)

HWND hDlg;

{

strcpy(str, DefPath);

strcat(str, DefSpec);

DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010);

SetDlgItemText(hDlg, IDC_EDIT, DefSpec);

}

The SetDlgItemText function copies the default filename to the dialog box's edit control.

The SeparateFile function divides a pathname into two parts and copies them to separate buffers. It first moves to the end of the pathname and uses the AnsiPrev function to back through it, looking for a drive or directory separator. Add the following statements to your C-language source file:

void SeparateFile(hDlg, lpDestPath, lpDestFileName, lpSrcFileName)

HWND hDlg;

LPSTR lpDestPath, lpDestFileName, lpSrcFileName;

{

LPSTR lpTmp;

CHAR cTmp;

lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);

while(*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)

lpTmp = AnsiPrev(lpSrcFileName, lpTmp);

if(*lpTmp != ':' && *lpTmp != '\\') {

lstrcpy(lpDestFileName, lpSrcFileName);

lpDestPath[0] = 0;

return;

}

lstrcpy(lpDestFileName, lpTmp + 1);

cTmp = *(lpTmp + 1);

lstrcpy(lpDestPath, lpSrcFileName);

*(lpTmp + 1) = cTmp;

lpDestPath[(lpTmp - lpSrcFileName) + 1] = 0;

}

The ChangeDefExt and AddExt functions all use standard C-language statements to carry out their tasks. Add the following statements to the C-language source file:

void ChangeDefExt(Ext, Name)

PSTR Ext, Name;

{

PSTR pTptr;

pTptr = Name;

while(*pTptr && *pTptr != '.')

pTptr++;

if(*pTptr) /* true if this is an extension */

if (!strchr(pTptr, '*') && !strchr(pTptr, '?'))

strcpy(Ext, pTptr); /* Copies the extension */

}

void AddExt(Name, Ext)

PSTR Name, Ext;

{

PSTR pTptr;

pTptr = Name;

while(*pTptr && *pTptr != '.')

pTptr++;

if(*pTptr != '.') /* If no extension, add the default */

strcat(Name, Ext);

}