⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 prntfile.c

📁 软件简介: Windows环境下文本文件的载入保存编缉的C语言源程序
💻 C
📖 第 1 页 / 共 3 页
字号:

            /* Keep track of the current line on the current page */
            CurrentLine = 1;

            /* One way to output one line at a time is to retrieve
             * one line at a time from the edit control and write it
             * using the TextOut function. For each line,
             * advance one line space. Also, check for the
             * end of the page and start a new page if necessary.
             */
            for (dwIndex = IOStatus = 0; dwIndex < dwLines; dwIndex++)
            {
                pLine[0] = 128;               /* Maximum buffer size */
                pLine[1] = 0;
                LineLength = (int)SendMessage(hEditWnd, EM_GETLINE,
                                   (WPARAM)dwIndex, (LPARAM)((LPSTR)pLine));
                TextOut(hPr, 0, CurrentLine*LineSpace,
                                              (LPSTR)pLine, LineLength);
                if (++CurrentLine > LinesPerPage )
                {
                    CurrentLine = 1;
                    IOStatus = Escape(hPr, NEWFRAME, 0, 0L, 0L);
                    if (IOStatus<0 || bAbort)
                        break;
                }
            }

            if (IOStatus >= 0 && !bAbort)
            {
                Escape(hPr, NEWFRAME, 0, 0L, 0L);
                Escape(hPr, ENDDOC, 0, 0L, 0L);
            }
            EnableWindow(hWnd, TRUE);

            /* Destroy the Abort dialog box */
            DestroyWindow(hAbortDlgWnd);
            FreeProcInstance(lpAbortDlg);
            FreeProcInstance(lpAbortProc);
            DeleteDC(hPr);
            break;


        /* edit menu commands */
        case IDM_UNDO:
        case IDM_CUT:
        case IDM_COPY:
        case IDM_PASTE:
        case IDM_CLEAR:
            MessageBox(GetFocus(),
                "Command not implemented",
                "PrntFile Sample Application",
                MB_ICONASTERISK | MB_OK);
            break;

        case IDM_EXIT:
            QuerySaveFile(hWnd);
            DestroyWindow(hWnd);
            break;

        case IDC_EDIT:
            if (HIWORD (lParam) == EN_ERRSPACE)
                MessageBox(GetFocus(), "Out of memory.", 
                    "PrntFile Sample Application", MB_ICONHAND | MB_OK);
            break;
            }
        break;

    case WM_SETFOCUS:
        SetFocus (hEditWnd);
        break;

    case WM_SIZE:
        MoveWindow(hEditWnd, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
        break;

    case WM_QUERYENDSESSION:             /* message: to end the session? */
        return (QuerySaveFile(hWnd));

    case WM_CLOSE:                       /* message: close the window    */
        if (QuerySaveFile(hWnd))
            DestroyWindow(hWnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return (DefWindowProc(hWnd, message, wParam, lParam));
        }
    return (NULL);
}

/****************************************************************************

    FUNCTION: SaveAsDlg(HWND, unsigned, WORD, LONG)
    PURPOSE: Allows user to change name to save file to

    COMMENTS:

        This will initialize the window class if it is the first time this
        application is run. It then creates the window, and processes the
        message loop until a PostQuitMessage is received. It exits the
        application by returning the value passed by the PostQuitMessage.

****************************************************************************/

int FAR PASCAL SaveAsDlg(hDlg, message, wParam, lParam)
HWND hDlg;
unsigned message;
WORD wParam;
LONG lParam;
{
    char TempName[128];

    switch (message)
        {
    case WM_INITDIALOG:
        /* If no filename is entered, don't allow the user to save to it */
        if (!FileName[0])
            bSaveEnabled = FALSE;
        else
        {
            bSaveEnabled = TRUE;

            /* Process the path to fit within the IDC_PATH field */
            DlgDirList(hDlg, DefPath, NULL, IDC_PATH, 0x4010);

            /* Send the current filename to the edit control */
            SetDlgItemText(hDlg, IDC_EDIT, FileName);

            /* Accept all characters in the edit control */
            SendDlgItemMessage(hDlg, IDC_EDIT, EM_SETSEL, 0,
            MAKELONG(0, 0x7fff));
        }

        /* Enable or disable the save control depending on whether the
         * filename exists.
         */
        EnableWindow(GetDlgItem(hDlg, IDOK), bSaveEnabled);

        /* Set the focus to the edit control within the dialog box */
        SetFocus(GetDlgItem(hDlg, IDC_EDIT));
        return (FALSE);                 /* FALSE since Focus was changed */

    case WM_COMMAND:
        switch (wParam)
            {
        case IDC_EDIT:
            /* If there was previously no filename in the edit
             * control, then the save control must be enabled as soon as
             * a character is entered.
             */
            if (HIWORD(lParam) == EN_CHANGE && !bSaveEnabled)
                 EnableWindow(GetDlgItem(hDlg, IDOK), bSaveEnabled = TRUE);
            return (TRUE);

        case IDOK:
            /* Get the filename from the edit control */
            GetDlgItemText(hDlg, IDC_EDIT, TempName, 128);

            /* If there are no wildcards, then separate the name into
             * path and name. If a path was specified, replace the
             * default path with the new path.
             */
            if (CheckFileName(hDlg, FileName, TempName))
            {
                SeparateFile(hDlg, (LPSTR)str, (LPSTR)DefSpec,
                    (LPSTR)FileName);

                if (str[0])
                    strcpy(DefPath, str);

                /* Tell the caller a filename was selected */
                EndDialog(hDlg, IDOK);
            }
            return (TRUE);

        case IDCANCEL:
            /* Tell the caller the user canceled the SaveAs function */
            EndDialog(hDlg, IDCANCEL);
            return (TRUE);
            }
        break;
        }

    return (FALSE);
}

/****************************************************************************

    FUNCTION: OpenDlg(HWND, unsigned, WORD, LONG)

    PURPOSE: Let user select a file, and open it.

****************************************************************************/

HANDLE FAR PASCAL OpenDlg(hDlg, message, wParam, lParam)
HWND hDlg;
unsigned message;
WORD wParam;
LONG lParam;
{
    HANDLE hFile;

    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);

            /* Open the file */
            if ((hFile = OpenFile(OpenName, (LPOFSTRUCT) &OfStruct,
                      OF_READ)) == -1)
            {
                sprintf(str, "Error %d opening %s.",
                     OfStruct.nErrCode, OpenName);
                MessageBox(hDlg, str, NULL, MB_OK | MB_ICONHAND);
            }
            else
            {
                /* Make sure there's enough room for the file */
                fstat(hFile, &FileStatus);

                if (FileStatus.st_size > MAXFILESIZE)
                {
                    sprintf(str,
                        "Not enough memory to load %s.\n%s exceeds %ld bytes.",
                         OpenName, OpenName, MAXFILESIZE);
                    MessageBox(hDlg, str, NULL, MB_OK | MB_ICONHAND);
                    return (TRUE);
                }

                /* File is opened and there is enough room so return
                 * the handle to the caller.
                 */
                strcpy(FileName, OpenName);
                EndDialog(hDlg, hFile);
                return (TRUE);
            }
            return (TRUE);

        case IDCANCEL:
          /* strcpy(DefPath, str);
             ChangeDefExt(DefExt, DefSpec);*/
             EndDialog(hDlg, NULL);
             return (TRUE);
             }
        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);

    /* To ensure that the listing is made for a subdir. of
     * current drive dir...
     */
    if (!strchr (DefPath, ':'))
        DlgDirList(hDlg, DefSpec, IDC_LISTBOX, IDC_PATH, 0x4010);

    /* Remove the '..' character from path if it exists, since this
     * will make DlgDirList move us up an additional level in the tree
     * when UpdateListBox() is called again.
     */
    if (strstr (DefPath, ".."))
        DefPath[0] = '\0';
    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);
}

/****************************************************************************

    FUNCTION: CheckFileName(HWND, PSTR, PSTR)

    PURPOSE: Check for wildcards, add extension if needed

    COMMENTS:

        Make sure you have a filename and that it does not contain any
        wildcards.  If needed, add the default extension.  This function is
        called whenever your application wants to save a file.

****************************************************************************/

BOOL CheckFileName(hWnd, pDest, pSrc)
HWND hWnd;
PSTR pDest, pSrc;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -