Hide

SftPrintPreview/DLL 2.0 - Print Preview Control for C/C++

Display
Print

PreviewRichEdit Sample (C)

This sample illustrates RichEdit control output.

The source code is located at C:\Program Files (x86)\Softelvdm\SftPrintPreview DLL 2.0\Samples\C\PreviewRichEdit\PreviewRichEdit.c or C:\Program Files\Softelvdm\SftPrintPreview DLL 2.0\Samples\C\PreviewRichEdit\PreviewRichEdit.c (on 32-bit Windows versions).


#include <windows.h>
#include <stdlib.h>
#include <Richedit.h>

#include <crtdbg.h>

#include "SftPrintPreview.h"                     /* SftPrintPreview/DLL Header File */

#include "resource.h"

/**********************************************************************/
/*                              Globals                               */
/**********************************************************************/

HINSTANCE g_hInst;                      // App Instance Handle
HWND m_hwndRichEdit;                    // Richedit Window

/**********************************************************************/
/*                   Center the main dialog                           */
/**********************************************************************/

static void CenterWindow(HWND hwnd)
{
    RECT rc;
    RECT rcOwner;
    int x, y;

    GetWindowRect(hwnd, &rc);
    GetWindowRect(GetDesktopWindow(), &rcOwner);

    // Calculate the starting x,y for the new window so that it would be centered.
    x = rcOwner.left + (((rcOwner.right - rcOwner.left) - (rc.right - rc.left)) / 2);
    y = rcOwner.top + (((rcOwner.bottom - rcOwner.top) - (rc.bottom - rc.top)) / 2);

    SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
}

/**********************************************************************/
/*                        Load RichEdit Window                        */
/**********************************************************************/

DWORD CALLBACK EditStreamCallback(SFTPRINTPREVIEW_DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
    HANDLE hFile = (HANDLE) dwCookie;
    DWORD dwdRead = 0;

    if (ReadFile(hFile, pbBuff, cb, &dwdRead, NULL)) {
        //_ASSERT(cb == dwdRead);
        *pcb = dwdRead;
        return 0;
    }
    return -1;
}

void LoadEditControlWithFile(HWND hwndDlg, LPCTSTR lpszFileName)
{
    HANDLE hFile = CreateFile(lpszFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile != INVALID_HANDLE_VALUE) {
        EDITSTREAM es;
        HWND hwndRichEdit = GetDlgItem(hwndDlg, IDC_RICHEDIT);

        HCURSOR oldCursor = SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_WAIT)));

        SetWindowText(hwndRichEdit, TEXT("Loading ..."));
        ShowWindow(GetParent(hwndRichEdit), SW_SHOW);
        UpdateWindow(GetParent(hwndRichEdit));

        es.dwCookie = (SFTPRINTPREVIEW_DWORD_PTR) hFile; // use our file handle as cookie
        es.dwError = 0;
        es.pfnCallback = EditStreamCallback;
        SendMessage(hwndRichEdit, EM_STREAMIN, SF_RTF, (LPARAM)&es); // load file
        CloseHandle(hFile);
    
        SetCursor(oldCursor);
    }
}

void LoadEditControl(HWND hwndDlg)
{
    TCHAR szFile[_MAX_PATH] = TEXT("");

    OPENFILENAME ofn;
    memset(&ofn, 0, sizeof(OPENFILENAME));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = hwndDlg;
    ofn.lpstrFilter = TEXT("RTF Files (*.rtf)\0*.rtf\0\0");
    ofn.lpstrFile = szFile;
    ofn.nMaxFile = sizeof(szFile)/sizeof(TCHAR);
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;

    if (GetOpenFileName(&ofn))
        LoadEditControlWithFile(hwndDlg, szFile);
    else {
        DWORD dwd = CommDlgExtendedError();
        if (dwd != 0)
            _ASSERT(0);
    }
}

/**********************************************************************/
/*                      Initialize Preview Window                     */
/**********************************************************************/

void InitPreviewWindow(HWND hwndCtl)
{
    // retrieve current control settings
    SFTPRINTPREVIEW_CONTROL Ctl;
    Ctl.cbSize = sizeof(SFTPRINTPREVIEW_CONTROL);
    if (!SftPrintPreview_GetControlInfo(hwndCtl, &Ctl)) {
        _ASSERT(0); // the SFTPRINTPREVIEW_CONTROL structure's cbSize member wasn't initialized properly
        return;
    }

    // set all desired options
    Ctl.fCenterOnClick = FALSE;
    Ctl.fDragPage  = TRUE;
    Ctl.iZoomStyle = SFTPRINTPREVIEW_ZOOMSTYLE_BOTHBUTTONS_EXACT;
    Ctl.numPageRows = 1;                // default to 1x2 pages
    Ctl.numPageGroups = 2;
    Ctl.zoom = 0;                       // start out with multiple pages
    lstrcpy(Ctl.szOutputName, TEXT("SftPrintPreview RichEdit Output")); // job name when printing
    lstrcpy(Ctl.szHeaderRight, TEXT("SftPrintPreview/DLL RichEdit Preview Sample"));
    lstrcpy(Ctl.szFooterLeft, TEXT("www.softelvdm.com"));

    if (!SftPrintPreview_SetControlInfo(hwndCtl, &Ctl)) {
        int errorValue = Ctl.errorValue;
        _ASSERT(0); // an error occurred, check errorValue for error code
        return;
    }
}

void DefineContents(HWND hwndCtl)
{
    SFTPRINTPREVIEW_CONTROL Ctl;

    Ctl.cbSize = sizeof(SFTPRINTPREVIEW_CONTROL);
    if (!SftPrintPreview_GetControlInfo(hwndCtl, &Ctl)) {
        _ASSERT(0); // the SFTPRINTPREVIEW_CONTROL structure's cbSize member wasn't initialized properly
        return;
    }

    // define desired contents to preview/print
    Ctl.lpDrawPageWorkArea = (SFTPRINTPREVIEW_DWORD_PTR) NULL;// no workarea needed
    Ctl.lpDrawInfoProc = SftPrintPreview_RenderRicheditWindow_Callback;// callback needed for RichEdit control
    Ctl.hwndContent = m_hwndRichEdit;   // We'll use this window
    Ctl.hwndData = NULL;                // no data window (content window is data window)
    Ctl.iContentSizing = SFTPRINTPREVIEW_CONTENTSIZING_SHRINKTOFIT;// shrink to fit on page

    if (!SftPrintPreview_SetControlInfo(hwndCtl, &Ctl)) {
        int errorValue = Ctl.errorValue;
        _ASSERT(0); // an error occurred, check errorValue for error codes
        return;
    }
}

/**********************************************************************/
/*                         Preview Dialog Proc                        */
/**********************************************************************/

BOOL CALLBACK PreviewDialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg) {

    case WM_INITDIALOG: {
        // Center this dialog
        CenterWindow(hwndDlg);

        // make sure main dialog has a sizing box
        SetWindowLong(hwndDlg, GWL_STYLE, GetWindowLong(hwndDlg, GWL_STYLE) | WS_SIZEBOX);
        ShowWindow(hwndDlg, SW_SHOWMAXIMIZED);

        // Initialize Preview Window
        InitPreviewWindow(GetDlgItem(hwndDlg, IDC_PREVIEW));
        DefineContents(GetDlgItem(hwndDlg, IDC_PREVIEW));

        // changing window styles above has suppressed setfocus call
        SetFocus(GetDlgItem(hwndDlg, IDC_PREVIEW));
        return FALSE;
     }

    case WM_DESTROY:
        break;

    case WM_COMMAND: {
        HWND hwndCtl = (HWND) lParam;
        int id = LOWORD(wParam);
        int code = HIWORD(wParam);
        if (hwndCtl) {
            switch (id) {
            case IDOK:
            case IDCANCEL:
                if (code == BN_CLICKED)
                    SendMessage(hwndDlg, WM_COMMAND, id, 0);
                break;
            case IDC_PREVIEW:
                switch (code) {
                case SFTPRINTPREVIEWN_STARTPRINTING:
                    SftPrintPreview_Print(hwndCtl, hwndDlg);
                    break;
                }
                break;
            }
        } else {
            switch (id) {
            case IDOK:
                EndDialog(hwndDlg, TRUE);
                break;
            case IDCANCEL:
                EndDialog(hwndDlg, FALSE);
                break;
            }    
        }
        break;
     }
    case WM_NOTIFY: {
        int id = (int) wParam; 
        LPNMHDR pnmh = (LPNMHDR) lParam;
        switch (id) {
        case IDC_PREVIEW:
            switch (pnmh->code) {
            case NM_SFTPRINTPREVIEW_CLOSE_CODE: { // user wants to close
                NM_SFTPRINTPREVIEW_CLOSE* pNtfy = (NM_SFTPRINTPREVIEW_CLOSE*) lParam;
                SendMessage(hwndDlg, WM_COMMAND, IDCANCEL, 0);
                break;
             }
            case NM_SFTPRINTPREVIEW_HELP_CODE: { // Help requested
                NM_SFTPRINTPREVIEW_HELP* pNtfy = (NM_SFTPRINTPREVIEW_HELP*) lParam;
                MessageBox(hwndDlg, TEXT("Sorry, this sample application doesn't include online help."), TEXT("SftPrintPreview/DLL"), MB_OK);
                break;
             }
            case NM_SFTPRINTPREVIEW_PAGESETUP_CODE: { // Page setup requested
                HWND hwndCtl = GetDlgItem(hwndDlg, IDC_PREVIEW);
                SftPrintPreview_PageSetup(hwndCtl, hwndDlg);
                break;
             }
            }
            break;
        }
        break;
     }

    case WM_SIZE: {
        HWND hwndCtl = GetDlgItem(hwndDlg, IDC_PREVIEW);
        RECT rect;
        GetClientRect(hwndDlg, &rect);
        MoveWindow(hwndCtl, 0, 0, rect.right-rect.left, rect.bottom-rect.top, TRUE);
        break;
     }
    }
    return FALSE;
}

/**********************************************************************/
/*                           Main Dialog Proc                         */
/**********************************************************************/

BOOL CALLBACK MainDialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg) {

    case WM_INITDIALOG: {
        HWND hwndRichEdit = GetDlgItem(hwndDlg, IDC_RICHEDIT);
        CHARRANGE cr;

        // Center this dialog
        CenterWindow(hwndDlg);
        // load Loading... text into edit control
        SendMessage(hwndRichEdit, EM_EXLIMITTEXT, 0, 0x7fffffff);//need all the space we can get

        // Load contents
        LoadEditControlWithFile(hwndDlg, TEXT("SftPrintPreview.rtf"));

        SetFocus(hwndRichEdit);
        cr.cpMin = 0; cr.cpMax = 0;
        SendMessage(hwndRichEdit, EM_EXSETSEL, 0, (LPARAM) &cr);

        return FALSE;
     }

    case WM_DESTROY:
        break;

    case WM_COMMAND: {
        HWND hwndCtl = (HWND) lParam;
        int id = LOWORD(wParam);
        int code = HIWORD(wParam);
        if (hwndCtl) {

            switch (id) {
            case IDCANCEL:
            case IDOK:
            case IDC_PREVIEW:
            case IDC_LOADFILE:
                if (code == BN_CLICKED)
                    SendMessage(hwndDlg, WM_COMMAND, id, 0);
                break;
            }

        } else {
            switch (id) {
            case IDOK:
                EndDialog(hwndDlg, TRUE);
                break;
            case IDCANCEL:
                EndDialog(hwndDlg, FALSE);
                break;
            case IDC_PREVIEW: {
                m_hwndRichEdit = GetDlgItem(hwndDlg, IDC_RICHEDIT);// save edit window for preview window
                DialogBox(g_hInst, MAKEINTRESOURCE(IDD_PREVIEW_DIALOG), hwndDlg, (DLGPROC)PreviewDialogProc);
                break;
             }
            case IDC_LOADFILE:
                LoadEditControl(hwndDlg);
                break;
            }    
        }
        break;
     }

    }
    return FALSE;
}

/**********************************************************************/
/*                              WinMain                               */
/**********************************************************************/

int PASCAL WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int cmdShow)
{
    HMODULE hRichEditDLL;

    g_hInst = hinst;

    SftPrintPreview_RegisterApp(hinst);   /* Register with SftPrintPreview/DLL */

    hRichEditDLL = LoadLibrary(TEXT("riched20.dll")); // load richedit dll so we have richedit support */
    _ASSERT(hRichEditDLL);

    // Initialize, run, and terminate the application
    DialogBox(g_hInst, MAKEINTRESOURCE(IDD_RICHEDIT_DIALOG), NULL, (DLGPROC)MainDialogProc);

    FreeLibrary(hRichEditDLL);
    
    SftPrintPreview_UnregisterApp(hinst); /* Unregister from SftPrintPreview/DLL */

    return 0;
}

Last Updated 08/13/2020 - (email)
© 2024 Softel vdm, Inc.