MFC realizes graphic printing __MFC

Source: Internet
Author: User
Tags drawtext

MFC to achieve graphic printing

2011-07-06 15:27 4 people read comments (0) favorites Edit Delete

Disclaimer: This feature is not original, the author only in the original based on some improvement to achieve the purpose of development projects.

The following content, perhaps some people have seen, but since I posted here, there must be some things you have not seen, this paper mainly to achieve graphics and text printing.

To implement printing steps under MFC:

/****

The Cprintframe and Cprintview classes provided in this article are a reusable class in which developers only need to copy four files from these two classes to the directory where the project files are located (Printframe.h,pringtview.h,printframe.cpp, PrintView.cpp), and add the four files to the project and add them to the code where you need to perform the printing function.

#include "PrintFrame.h"

#include "PrintView.h"

1. Create a Cprintframe object, set the pointer for the object to be pframe, and pass the dialog box pointer to the object's M_pcallerdlg, that is, Pframe->m_pcallerdlg = this;

2. The CREATE function of the calling object creates a frame window, such as Pframe->create (NULL, "Spectrum Printing", Ws_overlappedwindow,crect (0,0,0,0));

3. If you want to perform printing, call Pframe->m_pview->onmyprint ();

4. If you want to perform print preview, call:

Pframe->m_pview->onmyprintpreview ();

For example:

void Cdlgprintdlg::onprint ()//performing printing functions

{

Cprintframe *pframe = new Cprintframe;

Pframe->m_pcallerdlg = this;

Pframe->create (NULL, "Curve

Print ", Ws_overlappedwindow,crect (0,0,0,0));

Pframe->m_pview->onmyprint ();

}

void Cdlgprintdlg::onprintpreview ()//Perform print preview function

{

Cprintframe *pframe = new Cprintframe;

Pframe->m_pcallerdlg = this;

Pframe->create (NULL, "Curve Print

Preview ", Ws_overlappedwindow,crect (0,0,0,0));

Pframe->m_pview->onmyprintpreview ();

}

5. In the dialog box in response to wm_begin_printing, wm_end_printing,wm_my_print messages, respectively, to complete the initialization of the printing, release and specific printing operations;

If you have added three message response functions to the sample program to perform this function.

On_message (wm_begin_printing,onbeginprinting)

On_message (wm_end_printing,onendprinting)

On_message (Wm_my_print,onmyprint)

Where Onmyprint is the key to the specific content of what to print developers to focus on the completion of the code, you can print tables, pictures, data, as long as the GDI drawing can be done here can be completed. Because part of the print preview is done in the CView class, you need only the appropriate Wm_my_print message in the user program to perform the print preview function without having to write a separate print preview code.

Then follow these 5 steps to achieve a fully functional print program, using the above class to implement dialog box printing not only saves the developer a lot of time, but also the function is very powerful, can reach the very professional level, but this method has one shortcoming, the author discovers if the developer uses the statically connected MFC library to have the error, Applies only to the use of MFC in a shelled DLL, and must make the program a debug version.

Let's say how I add code to Onmyprint (WPARAM Wparam,lparam LPARAM)

The first argument, passing the handle, the second argument ... No use to.

First look at the effect of print preview:


The following code explains the implementation:

Intnwidth=pdc->getdevicecaps (Horzres);

Intnheight=pdc->getdevicecaps (Vertres);

Intghdf=pdc->getdevicecaps (Logpixelsy);

Lresult Cexamineeyedlg::onmyprint (Wparamwparam,lparam LPARAM)
{

cdc* PDC = (cdc*) WParam;
CFont font;//Font Type
CFont *oldfont;
CPoint Point;
CString ss= "Use Print Preview in dialog box";
Intnwidth=pdc->getdevicecaps (horzres)//Get the width of the paper
Intnheight=pdc->getdevicecaps (vertres)//Get the width of the paper
Intghdf=pdc->getdevicecaps (logpixelsy);/Get the pixel dpi of the paper
Crectdrawrect (0,0,nwidth,nheight), Rect,temprect,temprect1;
Drawrect.inflaterect ( -100,-nheight/8,-100,-50); The//inflaterect function increases or decreases the width and height of the specified rectangle
The order of the parameters is upper left and lower right
Pdc->rectangle (&drawrect); Draw a rectangle
Temprect1=temprect;
Font. CreateFont (-muldiv (25,-pdc->getdevicecaps (Logpixelsy), 72),
0,0,0,fw_normal,0,0,0,gb2312_charset,
Out_stroke_precis,clip_stroke_precis,draft_quality,
variable_pitch| ff_swiss,_t ("Young Circle"));//25 to font size
Oldfont=pdc->selectobject (&font);//Set Font
Temprect=drawrect;
Temprect.inflaterect (0,nheight/12,0,0);
Pdc->drawtext (str1[9],&temprect,dt_center| dt_top| Dt_singleline);//Draw the text out


Pdc->selectobject (Oldfont);
Font. DeleteObject ()//need to remove font to reallocate memory
Font. CreateFont (-muldiv (18,-pdc->getdevicecaps (Logpixelsy), 72),
0,0,0,fw_normal,0,0,0,gb2312_charset,
Out_stroke_precis,clip_stroke_precis,draft_quality,
variable_pitch| ff_swiss,_t ("blackbody"));
Pdc->selectobject (&font);
Temprect.inflaterect (0,-nheight/20,0,0);

Pdc->drawtext (str1[8],&temprect,dt_center| dt_top| Dt_singleline);


Font. DeleteObject ();
Font. CreateFont (-muldiv (12,-pdc->getdevicecaps (Logpixelsy), 72),
0,0,0,fw_normal,0,0,0,gb2312_charset,
Out_stroke_precis,clip_stroke_precis,draft_quality,
variable_pitch| ff_swiss,_t ("Song Body"));
Pdc->selectobject (&font);
Temprect.inflaterect ( -100,-nheight/24,-300,0);
Pdc->moveto (drawrect.left,drawrect.top+ (drawrect.bottom-drawrect.top)/28);//This one and the following one to achieve the drawing line
Pdc->lineto (drawrect.right,drawrect.top+ (drawrect.bottom-drawrect.top)/28);
Pdc->drawtext (str1[0],&temprect,dt_left| dt_top| Dt_singleline);//Where dt_left represents left alignment
Pdc->drawtext (str1[1],&temprect,dt_center| dt_top| Dt_singleline);
Pdc->drawtext (str1[2],&temprect,dt_right| dt_top| Dt_singleline);
Temprect.inflaterect (0,-nheight/30,0,0);
Pdc->moveto (drawrect.left,drawrect.top+ (drawrect.bottom-drawrect.top)/28*2);
Pdc->lineto (drawrect.right,drawrect.top+ (drawrect.bottom-drawrect.top)/28*2);
Pdc->drawtext (str1[3],&temprect,dt_left| dt_top| Dt_singleline);
Pdc->drawtext (str1[4],&temprect,dt_center| dt_top| Dt_singleline);
Pdc->drawtext (str1[5],&temprect,dt_right| dt_top| Dt_singleline);
Temprect.inflaterect (0,-nheight/30,0,0);
Pdc->moveto (drawrect.left,drawrect.top+ (drawrect.bottom-drawrect.top)/28*3);
Pdc->lineto (drawrect.right,drawrect.top+ (drawrect.bottom-drawrect.top)/28*3);
Pdc->drawtext (str1[6],&temprect,dt_left| dt_top| Dt_singleline);

Pdc->selectobject (Oldfont);
Above for display text


The following is the display picture
Hbitmap hbitmap = (hbitmap) loadimage (NULL, Pform->getcappath () + "Pic1.bmp", Image_bitmap,
0, 0, lr_createdibsection | Lr_defaultsize | Lr_loadfromfile);

When the original author displays the picture, the function used is LoadBitmap (), the function is limited, and must load the picture into the bitmap in MFC, for want to animate the picture words, very inconvenient, I adopted the LoadImage () function

Bitmapbitmap;
:: GetObject (Hbitmap,sizeof (BITMAP), &bitmap);
Doubledscale= (double) m_cxwidth/bitmap.bmwidth;
int NSCALEDWIDTH=M_CXWIDTH/2;
intnscaledheight= (int) (Bitmap.bmheight*dscale)/2;

HDC Dcmem;
Dcmem=::createcompatibledc (PDC->M_HDC);
Hbitmapholdbmp= (HBITMAP):: SelectObject (Dcmem,hbitmap);

int Nvertcenterpos =pdc->getdevicecaps (vertres)/2;
:: StretchBlt (Pdc->m_hdc,drawrect.left+100,nheight/3,nscaledwidth,nscaledheight,dcmem,0,0,bitmap.bmwidth, Bitmap.bmheight,srccopy);
:: StretchBlt (Pdc->m_hdc,nwidth/2+100,nheight/3,nscaledwidth,nscaledheight,dcmem,0,0,bitmap.bmwidth, Bitmap.bmheight,srccopy);

The second and third parameters of StretchBlt can adjust the position of the picture, and the fourth and fifth parameters can resize the picture

:: SelectObject (Dcmem,holdbmp);
::D Eletedc (DCMEM);
::D eleteobject (HBITMAP);

return TRUE;
}

Finish these, you can finish printing, but there is a problem has not been tested, that is, the design of a good format on the machine, on other machines will be disrupted, I do not know, to be verified.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.