In VC, how does one implement a set of tools (such as printing blank items in a ticket )?

Source: Internet
Author: User
Tags textout

The following program implements the set of functions, of course, you can also print as you want; at the same time, it also realizes the independence of the printing device.

The print program source code put in: http://download.csdn.net/detail/dijkstar/4667098

At the same time, a tinypdf file is provided in the above link for friends who do not have a real printer.

The program idea is very simple. Use cprintdialog to create a standard Print dialog box, click OK, and print the position string at the top and left,

With these positions, the back end can be compared with the paper to be printed (to the light or the sun), it is easy to locate the print place. The following is the implementation code (the whole program is a function ):

Void ctestdlg: onbutton1 () {// todo: add your control notification handler code herecfont fontsmall; cpen penbold; cprintdialog dluplint (false); // Standard Print dialog box if (dluplint. domodal () = idok) {HDC hprintdc; try {hprintdc = dluplint. createprinterdc ();} catch (...) {afxmessagebox ("The Environment handle of the printer device cannot be created. \ N Please reset the printer. ", Mb_ OK | mb_iconwarning); hprintdc = NULL;} CDC printdc; If (hprintdc & printdc. attach (hprintdc) {printdc. m_bprinting = true; printdc. setmapmode (mm_text); // ing to "text": the coordinate origin is in the upper left corner, the Y axis is down, and the X axis is in printdc to the right. startdoc ("print Report"); int nvertres = printdc. getdevicecaps (vertres); // The unit is dotsint nvertsize = (INT) (float) printdc. getdevicecaps (vertsize) * 1440/25 .4); // The unit is twips, which indicates how many twipsint nhorzres = printdc in the vertical direction. getdevi Cecaps (horzres); // The unit is dotsint nhorzsize = (INT) (float) printdc. getdevicecaps (horzsize) * 1440/25 .4); // Number of twips in the horizontal direction? Float fmapratiov = (float) nvertres/nvertsize; // establishes a ing relationship between the [pixel] and [millimeter]. Float fmapratioh = (float) nhorzres/nhorzsize; // horizontal float cxfont = (105 * fmapratioh); // customize a font that specifies its height and width float cyfont = (210 * fmapratiov ); float cyline = cyfont/3.0; // spacing (optional) // create a fontsmall font Based on the Custom font height and width. createfont (cyfont, cxfont, 0, 0, fw_normal, false, default_charset, out_default_precis, condition, default_quality, default_pitch | ff_dontcare, ""); cfont * poldfont = printdc. selectObject (& fontsmall); float xnums = nhorzres/cxfont; // calculates the number of rows in the horizontal direction. Float ynums = nvertres/cyfont; // calculates the number of columns in the vertical printdc. startpage (); int I = 0; cstring STR; /// print "01234567890123456789 ............ "// for (I = 0; I <xnums; I ++) {Str. format ("% d", I % 10); printdc. textout (cxfont * I, 0, STR); // By The Way, draw the "column vertical line" if (I % 10 = 0) {printdc. moveTo (cxfont * I, 0); printdc. lineto (cxfont * I, nvertres) ;}/// print the vertical "01234567890123456789 ............ "// for (I = 0; I <ynums; I ++) {Str. format ("% d", I % 10); printdc. textout (0, (cyfont + 0) * I, STR); // By The Way, draw the "horizontal line" if (I % 10 = 0) {printdc. moveTo (0, (cyfont + 0) * I); printdc. lineto (nhorzres, (cyfont + 0) * I) ;}// when the first column of characters is vertically printed in front of the image, the first column of the vertical line is erased, redraw the vertical line {printdc. moveTo (0, 0); printdc. lineto (0, nvertres);} // you can find the location through the above function, start printing your own documents ////.............................. .......... // release printdc. selectObject (poldfont); printdc. endpage (); printdc. enddoc (); printdc. detach ();}}}

The above program has already found the printing position, namely (cxfont, cyfont). How can we do it later? No more.

Pay attention to the Code in the program that establishes the relationship between [pixel] and the real [millimeter]. Through that program, different paper formats and "WYSIWYG" under different DPI printers are implemented ", if you don't believe it, you can verify the experiment by setting different DPI and different paper sizes in tinypdf.

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.