source: dev/trunk/ab5.0/abdev/abdev/Print.cpp

Last change on this file was 829, checked in by イグトランス (egtra), 11 years ago

svn:eol-styleとsvn:mime-type(文字コード指定含む)の設定

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain; charset=Shift_JIS
File size: 13.4 KB
Line 
1#include "stdafx.h"
2
3#include "common.h"
4
5HRESULT ApplyDialogTexture( HWND );
6
7int iPreviewNowPage;
8int iZoomNowPage;
9
10#define ZOOM_NUM 8
11#define ZOOM_INIT 4
12int array_Zoom[ZOOM_NUM]={
13    500,
14    200,
15    150,
16    100,
17    75,
18    50,
19    25,
20    10
21};
22
23
24CPage obj_Page;
25
26CPage::CPage(){
27    memset(&psd, 0, sizeof(PAGESETUPDLG));
28    psd.lStructSize = sizeof(PAGESETUPDLG);
29    psd.hwndOwner = hOwner;
30    psd.Flags = PSD_MARGINS |PSD_INHUNDREDTHSOFMILLIMETERS|PSD_DISABLEORIENTATION;
31    psd.rtMargin.left=2500;
32    psd.rtMargin.top=2500;
33    psd.rtMargin.right=2500;
34    psd.rtMargin.bottom=2500;
35    psd.ptPaperSize.x=21000;
36    psd.ptPaperSize.y=29700;
37}
38CPage::~CPage(){
39}
40
41BOOL CPage::SetupDlg(void){
42    //ページ設定ダイアログ
43    return PageSetupDlg(&psd);
44}
45
46void DrawPage(HDC memdc,BOOL bPreview,BOOL *lpbLastPage=0){
47    RECT rc;
48
49    //ページの幅、高さ(単位は0.1ミリメートル)
50    int iPageWidth,iPageHeight;
51    iPageWidth=obj_Page.psd.ptPaperSize.x/10;
52    iPageHeight=obj_Page.psd.ptPaperSize.y/10;
53
54    //マッピングモードをセット
55    SetMapMode(memdc,MM_LOMETRIC);
56
57    POINT px_pos;
58    px_pos.x=iPageWidth;
59    px_pos.y=iPageHeight;
60    LPtoDP(memdc,&px_pos,1);
61    px_pos.y=-px_pos.y;
62
63    int WndNum;
64    WndNum=GetWndNum(GetWindow(hClient,GW_CHILD));
65
66    //フォント生成
67    LOGFONT lf;
68    lf=pobj_nv->lfPrint;
69    lf.lfHeight=-MulDiv(pobj_nv->iPrintFontPointSize,GetDeviceCaps(memdc, LOGPIXELSY),960);
70    HFONT hFont;
71    hFont=CreateFontIndirect(&lf);
72
73    //フォントをセット
74    HFONT hOldFont;
75    hOldFont=(HFONT)SelectObject(memdc,hFont);
76
77    //マージン(単位は0.1ミリメートル)
78    RECT rcMargin;
79    rcMargin.left=obj_Page.psd.rtMargin.left/10;
80    rcMargin.top=obj_Page.psd.rtMargin.top/10;
81    rcMargin.right=obj_Page.psd.rtMargin.right/10;
82    rcMargin.bottom=obj_Page.psd.rtMargin.bottom/10;
83
84    //タブ文字数
85    DRAWTEXTPARAMS dtp;
86    dtp.cbSize=sizeof(DRAWTEXTPARAMS);
87    dtp.iLeftMargin=0;
88    dtp.iRightMargin=0;
89    dtp.iTabLength=pobj_nv->TabSize;
90    dtp.uiLengthDrawn=0;
91
92    char *temp;
93    temp=(char *)malloc(65535);
94
95    char *buffer;
96    buffer=MdiInfo[WndNum]->pMdiTextEdit->buffer;
97    int i=0,i2=0,iPage=0;
98    for(i=0,i2=0;;i++,i2++){
99        if(buffer[i]=='\0'||buffer[i]=='\r'&&buffer[i+1]=='\n'){
100            temp[i2]=0;
101
102            memset(&rc,0,sizeof(RECT));
103            rc.right=iPageWidth-(rcMargin.left + rcMargin.right);
104            DrawTextEx(memdc,temp,-1,&rc,DT_CALCRECT | DT_WORDBREAK|DT_TABSTOP|DT_EXPANDTABS|DT_NOPREFIX,&dtp);
105
106            if(rc.bottom<=-(iPageHeight-(rcMargin.top+rcMargin.bottom)) || buffer[i]=='\0'){
107
108                if(rc.bottom<-(iPageHeight-(rcMargin.top+rcMargin.bottom))){
109                    i--;i2--;
110                    while(i>0 && (!(buffer[i]=='\r'&&buffer[i+1]=='\n'))){
111                        i--;
112                        i2--;
113                    }
114                    temp[i2]=0;
115                }
116
117                rc.left=rcMargin.left;
118                rc.right=iPageWidth-rcMargin.right;
119                rc.top=-rcMargin.top;
120                rc.bottom=-(iPageHeight-rcMargin.bottom);
121
122                char szTempPage[255],*temp2,temp3[255];
123                lstrcpy(temp3,pobj_nv->szPageStr);
124                temp2=strstr(temp3,"&n");
125                if(temp2){
126                    temp2[0]='%';
127                    temp2[1]='d';
128
129                    sprintf(szTempPage,temp3,iPage+1);
130                }
131
132                if(bPreview&&iPage==iPreviewNowPage || bPreview==0){
133                    if(bPreview==0) StartPage(memdc);
134
135                    DrawTextEx(memdc,temp,-1,&rc,DT_LEFT | DT_WORDBREAK|DT_TABSTOP|DT_EXPANDTABS|DT_NOPREFIX,&dtp);
136
137                    if(pobj_nv->bShowPageNum){
138                        //ページ番号
139                        rc.top=-(iPageHeight-rcMargin.bottom);
140                        rc.bottom=-iPageHeight;
141                        DrawText(memdc,szTempPage,-1,&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_NOPREFIX);
142                    }
143
144                    if(bPreview==0) EndPage(memdc);
145
146                    if(lpbLastPage){
147                        if(buffer[i]=='\0'){
148                            *lpbLastPage=1;
149                        }
150                        else *lpbLastPage=0;
151                    }
152
153                    if(bPreview) break;
154                }
155
156                if(buffer[i]=='\0') break;
157
158                iPage++;
159
160                i++;
161                i2=-1;
162                continue;
163            }
164        }
165        temp[i2]=buffer[i];
166    }
167
168    free(temp);
169
170    SelectObject(memdc,hOldFont);
171}
172
173void CreatePreviewDCAndBmp(HDC *lphDC,HBITMAP *lphBmp,POINT *lpPxPos,BOOL *lpbLastPage){
174
175    //印刷ダイアログ
176    PRINTDLG pd;
177    memset(&pd,0,sizeof(PRINTDLG));
178    pd.lStructSize=sizeof(PRINTDLG);
179    pd.hwndOwner=hOwner;
180    pd.Flags=PD_RETURNDC|PD_RETURNDEFAULT;
181    pd.nFromPage=1;
182    pd.nToPage=1;
183    pd.nMinPage=1;
184    pd.nMaxPage=1;
185    pd.nCopies=1;
186    if(PrintDlg(&pd)==0){
187        return;
188    }
189
190    HDC memdc;
191    memdc=CreateCompatibleDC(pd.hDC);
192
193    //ページの幅、高さ(単位は0.1ミリメートル)
194    int iPageWidth,iPageHeight;
195    iPageWidth=obj_Page.psd.ptPaperSize.x/10;
196    iPageHeight=obj_Page.psd.ptPaperSize.y/10;
197
198    //マッピングモードをセット
199    SetMapMode(memdc,MM_LOMETRIC);
200
201    POINT px_pos;
202    px_pos.x=iPageWidth;
203    px_pos.y=iPageHeight;
204    LPtoDP(memdc,&px_pos,1);
205    px_pos.y=-px_pos.y;
206
207    HBITMAP hMemBmp;
208    hMemBmp=CreateCompatibleBitmap(pd.hDC,px_pos.x,px_pos.y);
209    SelectObject(memdc,hMemBmp);
210
211    //背景を初期化
212    HBRUSH hOldBrush;
213    hOldBrush=(HBRUSH)SelectObject(memdc,GetStockObject(WHITE_BRUSH));
214    PatBlt(memdc,0,0,px_pos.x,-px_pos.y,PATCOPY);
215    SelectObject(memdc,hOldBrush);
216
217    //メモリへ描画
218    DrawPage(memdc,1,lpbLastPage);
219
220    DeleteDC(pd.hDC);
221
222
223    *lphDC=memdc;
224    *lphBmp=hMemBmp;
225    *lpPxPos=px_pos;
226}
227
228void DrawPreview(HWND hPreview,HDC hdc,HDC memdc,POINT *lpPxPos){
229    SetMapMode(memdc,MM_TEXT);
230
231    POINT px_pos;
232    px_pos=*lpPxPos;
233
234    SIZE size;
235    RECT rc;
236    GetClientRect(hPreview,&rc);
237    size.cy=(long)((double)rc.bottom*0.9);
238    size.cx=(long)((double)size.cy*((double)obj_Page.psd.ptPaperSize.x/(double)obj_Page.psd.ptPaperSize.y));
239
240    SIZE size_hori;
241    size_hori.cx=(long)((double)rc.right*0.9);
242    size_hori.cy=(long)((double)size_hori.cx*((double)obj_Page.psd.ptPaperSize.y/(double)obj_Page.psd.ptPaperSize.x));
243
244    if(size.cx>size_hori.cx) size=size_hori;
245    size.cx=px_pos.x;
246    size.cy=px_pos.y;
247
248
249    //////////////////////////////////
250    // スクロールバー情報を取得
251    //////////////////////////////////
252
253    //垂直
254    SCROLLINFO si_vert;
255    si_vert.cbSize=sizeof(SCROLLINFO);
256    si_vert.fMask=SIF_PAGE|SIF_RANGE|SIF_POS;
257    GetScrollInfo(hPreview,SB_VERT,&si_vert);
258
259    //水平
260    SCROLLINFO si_horz;
261    si_horz.cbSize=sizeof(SCROLLINFO);
262    si_horz.fMask=SIF_PAGE|SIF_RANGE|SIF_POS;
263    GetScrollInfo(hPreview,SB_HORZ,&si_horz);
264
265
266
267    //表示率
268    double dblZoomRatio;
269    dblZoomRatio=((double)array_Zoom[iZoomNowPage]/(double)100)*0.18;
270
271
272    ///////////////////////////
273    // メモリへ描画
274    ///////////////////////////
275
276    HDC memdc2;
277    memdc2=CreateCompatibleDC(hdc);
278
279    HBITMAP hMemBmp;
280    hMemBmp=CreateCompatibleBitmap(hdc,(int)((double)size.cx*dblZoomRatio)+40, (int)((double)size.cy*dblZoomRatio)+40);
281
282    SelectObject(memdc2,hMemBmp);
283    RECT rc2;
284    rc2.left=0;rc2.top=0;
285    rc2.right=(int)((double)size.cx*dblZoomRatio)+40;
286    rc2.bottom=(int)((double)size.cy*dblZoomRatio)+40;
287    FillRect(memdc2,&rc2,(HBRUSH)GetStockObject(GRAY_BRUSH));
288
289    SetStretchBltMode(memdc2,HALFTONE);
290    StretchBlt(memdc2,
291        20, 20,
292        (int)((double)size.cx*dblZoomRatio), (int)((double)size.cy*dblZoomRatio),
293        memdc,0,0,px_pos.x,px_pos.y,SRCCOPY);
294
295    //画面へ描画
296    BitBlt(hdc,-si_horz.nPos,-si_vert.nPos,rc2.right,rc2.bottom,
297        memdc2,0,0,SRCCOPY);
298
299    DeleteDC(memdc2);
300    DeleteObject(hMemBmp);
301
302
303    //////////////////////////////
304    // スクロールバーを設定
305    //////////////////////////////
306    GetClientRect(hPreview,&rc);
307
308    //垂直
309    si_vert.nMin=0;
310    si_vert.nMax=rc2.bottom;
311    si_vert.nPage=rc.bottom;
312    SetScrollInfo(hPreview,SB_VERT,&si_vert,1);
313
314    //水平
315    si_horz.nMin=0;
316    si_horz.nMax=rc2.right;
317    si_horz.nPage=rc.right;
318    SetScrollInfo(hPreview,SB_HORZ,&si_horz,1);
319}
320#define WM_RESETPREVIEW WM_USER+100
321LRESULT CALLBACK PreviewWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
322    static HDC memdc;
323    static HBITMAP hMemBmp;
324    static POINT px_pos;
325    SCROLLINFO si;
326    int i;
327    RECT rc;
328    switch(message){
329        case WM_CREATE:
330            memdc=0;
331            hMemBmp=0;
332            SendMessage(hwnd,WM_RESETPREVIEW,0,0);
333            return 0;
334        case WM_PAINT:
335            HDC hdc;
336            PAINTSTRUCT ps;
337            hdc=BeginPaint(hwnd,&ps);
338            DrawPreview(hwnd,hdc,memdc,&px_pos);
339            EndPaint(hwnd,&ps);
340            return 0;
341        case WM_RESETPREVIEW:
342            if(memdc) DeleteDC(memdc);
343            if(hMemBmp) DeleteObject(hMemBmp);
344            BOOL bLastPage;
345            CreatePreviewDCAndBmp(&memdc,&hMemBmp,&px_pos,&bLastPage);
346            InvalidateRect(hwnd,NULL,1);
347
348            if(iPreviewNowPage==0) EnableWindow(GetDlgItem(GetParent(hwnd),IDC_PREV),0);
349            else EnableWindow(GetDlgItem(GetParent(hwnd),IDC_PREV),1);
350            if(bLastPage) EnableWindow(GetDlgItem(GetParent(hwnd),IDC_NEXT),0);
351            else EnableWindow(GetDlgItem(GetParent(hwnd),IDC_NEXT),1);
352
353            if(iZoomNowPage<=0) EnableWindow(GetDlgItem(GetParent(hwnd),IDC_ZOOMIN),0);
354            else EnableWindow(GetDlgItem(GetParent(hwnd),IDC_ZOOMIN),1);
355            if(iZoomNowPage>=ZOOM_NUM-1)
356                EnableWindow(GetDlgItem(GetParent(hwnd),IDC_ZOOMOUT),0);
357            else EnableWindow(GetDlgItem(GetParent(hwnd),IDC_ZOOMOUT),1);
358            return 0;
359        case WM_VSCROLL:
360        case WM_HSCROLL:
361            int fnBar;
362            if(message==WM_VSCROLL) fnBar=SB_VERT;
363            else fnBar=SB_HORZ;
364
365            si.cbSize=sizeof(SCROLLINFO);
366            si.fMask=SIF_POS|SIF_PAGE|SIF_RANGE;
367
368            GetScrollInfo(hwnd,fnBar,&si);
369            if(LOWORD(wParam)==SB_LINEUP) i=-1;
370            else if(LOWORD(wParam)==SB_LINEDOWN) i=1;
371            else if(LOWORD(wParam)==SB_PAGEUP) i=-(signed int)si.nPage;
372            else if(LOWORD(wParam)==SB_PAGEDOWN) i=si.nPage;
373            else if(LOWORD(wParam)==SB_THUMBTRACK) i=HIWORD(wParam)-si.nPos;
374            else i=0;
375            GetClientRect(hwnd,&rc);
376            if(message==WM_VSCROLL)
377                i=max(-si.nPos,min(i,si.nMax-rc.bottom-si.nPos));
378            else
379                i=max(-si.nPos,min(i,si.nMax-rc.right-si.nPos));
380            if(i!=0){
381                si.nPos+=i;
382                SetScrollInfo(hwnd,fnBar,&si,1);
383
384                InvalidateRect(hwnd,NULL,0);
385            }
386            return 0;
387        case WM_DESTROY:
388            DeleteDC(memdc);
389            DeleteObject(hMemBmp);
390            return 0;
391    }
392    return DefWindowProc(hwnd,message,wParam,lParam);
393}
394INT_PTR CALLBACK DlgPreview(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
395    static HWND hPreview;
396    char temporary[255];
397    switch(message){
398        case WM_INITDIALOG:
399            SetPosCenter(GetDesktopWindow(),hwnd);
400
401            hPreview=CreateWindowEx(WS_EX_STATICEDGE,"PreviewWindow",NULL,
402                WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL,
403                0,0,0,0,
404                hwnd,0,hInst,0);
405
406            RECT rc;
407            GetClientRect(hwnd,&rc);
408            SetWindowPos(hwnd,NULL,0,0,rc.right,rc.bottom,SWP_NOMOVE);
409
410            iPreviewNowPage=0;
411
412            SetDlgItemText(hwnd,IDC_FONT,pobj_nv->lfPrint.lfFaceName);
413
414            //ページ番号を表示するかどうか
415            if(pobj_nv->bShowPageNum) SendDlgItemMessage(hwnd,IDC_SHOWPAGENUM,BM_SETCHECK,BST_CHECKED,0);
416            SendMessage(hwnd,WM_COMMAND,IDC_SHOWPAGENUM,0);
417
418            //ページ番号文字列
419            SetDlgItemText(hwnd,IDC_PAGESTR,pobj_nv->szPageStr);
420
421            //拡大・縮小アイコンを表示
422            SendDlgItemMessage(hwnd,IDC_ZOOMOUT,BM_SETIMAGE,IMAGE_ICON ,
423                reinterpret_cast<LPARAM>(ActiveBasic::Resource::LoadIconAlt(hResInst, IDI_ZOOMOUT, 16, 16, LR_SHARED)));
424            SendDlgItemMessage(hwnd,IDC_ZOOMIN,BM_SETIMAGE,IMAGE_ICON ,
425                reinterpret_cast<LPARAM>(ActiveBasic::Resource::LoadIconAlt(hResInst, IDI_ZOOMIN, 16, 16, LR_SHARED)));
426            iZoomNowPage=ZOOM_INIT;
427
428            sprintf(temporary,"%d%%",array_Zoom[iZoomNowPage]);
429            SetDlgItemText(hwnd,IDC_ZOOMRATIO,temporary);
430
431            SendMessage(hPreview,WM_RESETPREVIEW,0,0);
432
433            ApplyDialogTexture(hwnd);
434            break;
435        case WM_COMMAND:
436            switch(LOWORD(wParam)){
437                case IDOK:
438                case IDCANCEL:
439                    EndDialog(hwnd,0);
440                    return 1;
441
442                case IDC_PREV:
443                    iPreviewNowPage--;
444                    SendMessage(hPreview,WM_RESETPREVIEW,0,0);
445                    return 1;
446                case IDC_NEXT:
447                    iPreviewNowPage++;
448                    SendMessage(hPreview,WM_RESETPREVIEW,0,0);
449                    return 1;
450
451                case IDC_ZOOMOUT:
452                case IDC_ZOOMIN:
453                    if(LOWORD(wParam)==IDC_ZOOMOUT)
454                        iZoomNowPage++;
455                    if(LOWORD(wParam)==IDC_ZOOMIN)
456                        iZoomNowPage--;
457                    SendMessage(hPreview,WM_RESETPREVIEW,0,0);
458
459                    sprintf(temporary,"%d%%",array_Zoom[iZoomNowPage]);
460                    SetDlgItemText(hwnd,IDC_ZOOMRATIO,temporary);
461                    return 1;
462
463                case IDC_PRINT:
464                    Printout();
465                    return 1;
466
467                case IDC_PAGESET:
468                    if(obj_Page.SetupDlg()){
469                        //再描画
470                        SendMessage(hPreview,WM_RESETPREVIEW,0,0);
471                    }
472                    return 1;
473
474                case IDC_FONT:
475                    int iPointSize;
476                    LOGFONT temp_lf;
477                    temp_lf=pobj_nv->lfPrint;
478                    if(!SetFontDialog(hwnd,&temp_lf,0,&iPointSize)) return 1;
479                    pobj_nv->lfPrint=temp_lf;
480                    pobj_nv->iPrintFontPointSize=iPointSize;
481
482                    SetDlgItemText(hwnd,IDC_FONT,pobj_nv->lfPrint.lfFaceName);
483
484                    //再描画
485                    SendMessage(hPreview,WM_RESETPREVIEW,0,0);
486                    return 1;
487
488                case IDC_SHOWPAGENUM:
489                    //ページ番号を表示するかどうか
490                    if(SendDlgItemMessage(hwnd,IDC_SHOWPAGENUM,BM_GETCHECK,0,0)){
491                        EnableWindow(GetDlgItem(hwnd,IDC_PAGESTR),1);
492                        pobj_nv->bShowPageNum=1;
493                    }
494                    else{
495                        EnableWindow(GetDlgItem(hwnd,IDC_PAGESTR),0);
496                        pobj_nv->bShowPageNum=0;
497                    }
498                    SendMessage(hPreview,WM_RESETPREVIEW,0,0);
499                    return 1;
500
501                case IDC_PAGESTR:
502                    //ページ番号文字列
503                    GetDlgItemText(hwnd,IDC_PAGESTR,pobj_nv->szPageStr,255);
504                    return 1;
505            }
506            return 0;
507
508        case WM_SIZE:
509            MoveWindow(hPreview,
510                140,40,
511                LOWORD(lParam)-155,
512                HIWORD(lParam)-55,0);
513            InvalidateRect(hwnd,NULL,0);
514            return 0;
515
516        case WM_DESTROY:
517            DestroyWindow(hPreview);
518            return 0;
519    }
520    return 0;
521}
522
523void Preview(void){
524    ActiveBasic::Resource::DialogBoxAlt(hResInst, IDD_PREVIEW, hOwner, DlgPreview);
525}
526
527
528void Printout(void){
529    int WndNum;
530    WndNum=GetWndNum(GetWindow(hClient,GW_CHILD));
531
532    //印刷ダイアログ
533    PRINTDLG pd;
534    memset(&pd,0,sizeof(PRINTDLG));
535    pd.lStructSize=sizeof(PRINTDLG);
536    pd.hwndOwner=hOwner;
537    pd.Flags=PD_ALLPAGES | PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE | PD_NOSELECTION | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE;
538    pd.nFromPage=1;
539    pd.nToPage=1;
540    pd.nMinPage=1;
541    pd.nMaxPage=1;
542    pd.nCopies=1;
543    if(PrintDlg(&pd)==0){
544        return;
545    }
546
547    //印刷ドキュメントをスタート
548    DOCINFO di;
549    memset(&di,0,sizeof(DOCINFO));
550    di.cbSize=sizeof(DOCINFO);
551    di.lpszDocName=MdiInfo[WndNum]->title.c_str();
552    if(StartDoc(pd.hDC,&di)==0){
553        MessageBox(hOwner,"印刷できません。プリンターの状況を確認してください。",APPLICATION_NAME,MB_OK | MB_ICONEXCLAMATION);
554        return;
555    }
556
557    //紙へ描画
558    DrawPage(pd.hDC,0);
559
560    //印刷ドキュメントを終了
561    EndDoc(pd.hDC);
562
563    DeleteDC(pd.hDC);
564}
Note: See TracBrowser for help on using the repository browser.