/**************************************************************************** PROGRAM: FileOpen.c PURPOSE: FUNCTIONS: OpenDlg() - let user select a file, and open it. UpdateListBox() - Update the list box of OpenDlg ChangeDefExt() - Change the default extension SeparateFile() - Separate filename and pathname AddExt() - Add default extension ****************************************************************************/ #define _WINDOWS #include #include #define NOCOMM #define NOKANJI #define NOATOMS #define NOBITMAP #define NOSOUND #include "windows.h" #include "\winsrc\wtools\fileopen.h" char FileName[128]; char PathName[128]; char OpenName[128]; char DefPath[128]; char DefSpec[13]; char DefExt[4]; char str[255]; // Local prototypes HANDLE FAR PASCAL OpenDlg(HWND, unsigned, WORD, LONG); void SeparateFile(HWND, LPSTR, LPSTR, LPSTR); void UpdateListBox(HWND); void AddExt(PSTR, PSTR); void ChangeDefExt(PSTR, PSTR); /**************************************************************************** FUNCTION: GetFileName(PSTR) PURPOSE: Return the name of the file selected. ****************************************************************************/ int GetFileName(hInst, hWnd, szSpec, szName) HANDLE hInst; HWND hWnd; PSTR szSpec, szName; { FARPROC lpProcOpen; int retVal; FileName[0] = '\0'; PathName[0] = '\0'; OpenName[0] = '\0'; DefPath[0] = '\0'; DefExt[0] = '\0'; strcpy( DefSpec, szSpec ); lpProcOpen = MakeProcInstance((FARPROC) OpenDlg, hInst); retVal = DialogBox(hInst, "Open", hWnd, lpProcOpen); FreeProcInstance(lpProcOpen); if (retVal) strcpy(szName, OpenName); return retVal; } /**************************************************************************** FUNCTION: OpenDlg(HWND, unsigned, WORD, LONG) PURPOSE: Let user select a file, and return. Open code not provided. ****************************************************************************/ HANDLE FAR PASCAL OpenDlg(hDlg, message, wParam, lParam) HWND hDlg; unsigned message; WORD wParam; LONG lParam; { WORD index; PSTR pTptr; HANDLE hFile=1; /* Temp value for return */ switch (message) { case WM_COMMAND: switch (wParam) { case IDC_LISTBOX: switch (HIWORD(lParam)) { case LBN_SELCHANGE: /* If item is a directory name, append "*.*" */ if (DlgDirSelect(hDlg, str, IDC_LISTBOX)) strcat(str, DefSpec); SetDlgItemText(hDlg, IDC_EDIT, str); SendDlgItemMessage(hDlg, IDC_EDIT, EM_SETSEL, NULL, MAKELONG(0, 0x7fff)); break; case LBN_DBLCLK: goto openfile; } return (TRUE); case IDOK: openfile: GetDlgItemText(hDlg, IDC_EDIT, OpenName, 128); if (strchr(OpenName, '*') || strchr(OpenName, '?')) { SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec, (LPSTR) OpenName); if (str[0]) strcpy(DefPath, str); ChangeDefExt(DefExt, DefSpec); UpdateListBox(hDlg); return (TRUE); } if (!OpenName[0]) { MessageBox(hDlg, "No filename specified.", NULL, MB_OK | MB_ICONHAND); return (TRUE); } AddExt(OpenName, DefExt); /* The routine to open the file would go here, and the */ /* file handle would be returned instead of NULL. */ EndDialog(hDlg, hFile); return (TRUE); case IDCANCEL: EndDialog(hDlg, NULL); return (FALSE); } break; case WM_INITDIALOG: /* message: initialize */ UpdateListBox(hDlg); SetDlgItemText(hDlg, IDC_EDIT, DefSpec); SendDlgItemMessage(hDlg, /* dialog handle */ IDC_EDIT, /* where to send message */ EM_SETSEL, /* select characters */ NULL, /* additional information */ MAKELONG(0, 0x7fff)); /* entire contents */ SetFocus(GetDlgItem(hDlg, IDC_EDIT)); return (FALSE); /* Indicates the focus is set to a control */ } return FALSE; } /**************************************************************************** FUNCTION: UpdateListBox(HWND); PURPOSE: Update the list box of OpenDlg ****************************************************************************/ void UpdateListBox(hDlg) HWND hDlg; { strcpy(str, DefPath); strcat(str, DefSpec); DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010); SetDlgItemText(hDlg, IDC_EDIT, DefSpec); } /**************************************************************************** FUNCTION: ChangeDefExt(PSTR, PSTR); PURPOSE: Change the default extension ****************************************************************************/ void ChangeDefExt(Ext, Name) PSTR Ext, Name; { PSTR pTptr; pTptr = Name; while (*pTptr && *pTptr != '.') pTptr++; if (*pTptr) if (!strchr(pTptr, '*') && !strchr(pTptr, '?')) strcpy(Ext, pTptr); } /**************************************************************************** FUNCTION: SeparateFile(HWND, LPSTR, LPSTR, LPSTR) PURPOSE: Separate filename and pathname ****************************************************************************/ 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; } /**************************************************************************** FUNCTION: AddExt(PSTR, PSTR); PURPOSE: Add default extension /***************************************************************************/ void AddExt(Name, Ext) PSTR Name, Ext; { PSTR pTptr; pTptr = Name; while (*pTptr && *pTptr != '.') pTptr++; if (*pTptr != '.') strcat(Name, Ext); }