Pelles C forum

C language => Expert questions => Topic started by: TimoVJL on June 19, 2010, 07:09:04 PM

Title: RichEdit preview problem
Post by: TimoVJL on June 19, 2010, 07:09:04 PM
I have problem with RichEdit preview.

OnPrintPreview2()
From where that needed 1700 (1,7 inch) comes from ?
Code: [Select]
fmtRange.rc.bottom = MulDiv(
//psd.ptPaperSize.y-psd.rtMargin.top-psd.rtMargin.bottom,
//psd.ptPaperSize.y-psd.rtMargin.top-psd.rtMargin.top-psd.rtMargin.bottom,
psd.ptPaperSize.y-psd.rtMargin.top*2-psd.rtMargin.bottom-nOfsY*2-1700, // ?????
1440, GetDeviceCaps(pd.hDC, LOGPIXELSY));

Have anyone time to look at it ?

EDIT: It's seems to be RichEdit FormatRange 'feature'

Example function using millimeters and A4 page.

Code: [Select]
int OnPrintPreview(HWND hWnd, HWND hWndEdit, HWND hPreview)
{
PAGESETUPDLG psd;
FORMATRANGE fmtRange;
int nMaxLen, nChar;
HENHMETAFILE hEmf;
HDC hdcEMF;
char szTmp[255];

InvalidateRect(hPreview, NULL, TRUE);
//CheckMsgQueue();
//if (!(nMaxLen = SendMessage(hWndEdit, WM_GETTEXTLENGTH, 0, 0)))
// return 2;
psd.Flags = 0;
psd.ptPaperSize.x = 21000; // A4 width
psd.ptPaperSize.y = 29700; // A4 height
psd.rtMargin.left = 2500;
psd.rtMargin.top = 2500;
psd.rtMargin.right = 600;
psd.rtMargin.bottom = 2500;
wsprintf(szTmp, "Flags: %X Paper Size: %dx%d Margins: %d %d %d %d",
psd.Flags,
psd.ptPaperSize.x/100, psd.ptPaperSize.y/100,
psd.rtMargin.left/100, psd.rtMargin.top/100,
psd.rtMargin.right/100, psd.rtMargin.bottom/100
);
SetWindowText(hStatus, szTmp);

// 1440 twips / 25.4 mm = 56,6929 twips/mm
float fmm2twipx = 0.89; //56.6929;
float fmm2twipy = 0.89; //56.6929;

HDC hdc;
hdc = GetDC(hPreview);

//fmtRange.rcPage.left = 0;
//fmtRange.rcPage.top = 0;
fmtRange.rcPage.right = psd.ptPaperSize.x * fmm2twipx;
fmtRange.rcPage.bottom = psd.ptPaperSize.y * fmm2twipy;
//
fmtRange.rcPage.left = psd.rtMargin.left * fmm2twipx;
fmtRange.rcPage.top = psd.rtMargin.top * fmm2twipy;
fmtRange.rcPage.right = (psd.ptPaperSize.x+psd.rtMargin.left) * fmm2twipx;
fmtRange.rcPage.bottom = (psd.ptPaperSize.y+psd.rtMargin.top) * fmm2twipy;


fmtRange.rc.left = psd.rtMargin.left * fmm2twipx;
fmtRange.rc.top = psd.rtMargin.top * fmm2twipy;
fmtRange.rc.right = (psd.ptPaperSize.x-psd.rtMargin.left*2-psd.rtMargin.right) * fmm2twipx;
fmtRange.rc.bottom = (psd.ptPaperSize.y-psd.rtMargin.top*2-psd.rtMargin.bottom-3800) * fmm2twipy;

/*
fmtRange.rc.left = 0;
fmtRange.rc.top = 0;
fmtRange.rc.right = (psd.ptPaperSize.x-psd.rtMargin.left) * fmm2twipx;
fmtRange.rc.bottom = (psd.ptPaperSize.y-psd.rtMargin.top) * fmm2twipy;

fmtRange.rcPage.left = fmtRange.rc.left;
fmtRange.rcPage.top = fmtRange.rc.top;
fmtRange.rcPage.right = fmtRange.rc.right;
fmtRange.rcPage.bottom = fmtRange.rc.bottom;
*/

fmtRange.hdc = hdc;
fmtRange.hdcTarget = hdc;
hdcEMF = CreateEnhMetaFile(0, "RE-TestPP2np.emf", &fmtRange.rcPage, NULL);
fmtRange.hdc = hdcEMF; // where to format
SendMessage(hWndEdit, EM_FORMATRANGE, 0, 0);
nChar = SendMessage(hWndEdit, EM_FORMATRANGE, TRUE, (LPARAM) &fmtRange);
SendMessage(hWndEdit, EM_FORMATRANGE, 0, 0);
RECT rcPage;
rcPage.left = 1;
rcPage.top = 1;
rcPage.right = psd.ptPaperSize.x * fmm2twipx + 1;
rcPage.bottom = psd.ptPaperSize.y * fmm2twipy + 1;
//FrameRect(hdcEMF, &rcPage, GetStockObject(BLACK_BRUSH));
FrameRect(hdcEMF, &rcPage, GetStockObject(WHITE_BRUSH));
hEmf = CloseEnhMetaFile(hdcEMF);

// show result in preview window
ENHMETAHEADER hdr;
GetEnhMetaFileHeader(hEmf, sizeof(hdr), &hdr);
//float fAspect = (float)(hdr.rclFrame.right - hdr.rclFrame.left)
// / (float)(hdr.rclFrame.bottom - hdr.rclFrame.top);
float fAspect = (float)(hdr.rclBounds.right - hdr.rclBounds.left)
/ (float)(hdr.rclBounds.bottom - hdr.rclBounds.top);

RECT rc;
GetClientRect(hPreview, &rc);
if (((rc.right / fAspect) / rc.bottom) > 1)
rc.right = (rc.bottom + 0.5) * fAspect;
else
rc.bottom = (rc.right + 0.5) / fAspect;

PlayEnhMetaFile(hdc, hEmf, &rc);
FrameRect(hdc, &rc, GetStockObject(BLACK_BRUSH));
ReleaseDC(hPreview, hdc);

DeleteEnhMetaFile(hEmf);

}