Печать текста и графики. Одностраничные распечатки

Страницы работы

5 страниц (Word-файл)

Содержание работы

Печать  текста  и  графики.     Одностраничные  распечатки 

Ruler.rh

#define ID_MENU  100

#define CM_PRINT 101

#define CM_PRINTERSETUP 102

Ruler.rc

#include <owl\window.rh>

#include <owl\printer.rh>

#include "ruler.rh"

#include <owl\printer.rc>

ID_MENU MENU

BEGIN

POPUP "&Demo"

BEGIN

MENUITEM "&Print", CM_PRINT

MENUITEM "P&rinter setup", CM_PRINTERSETUP

MENUITEM SEPARATOR

MENUITEM "E&xit", CM_EXIT

END

END   

Ruler.cpp

//Demonstrates single-page printing

#include <owl\applicat.h>

#include <owl\framewin.h>

#include <owl\printer.h>

#include <owl\scroller.h>

#include <owl\dc.h>

#include <classlib\stacks.h>

#include <string.h>

#include <cstring.h>

#include <stdio.h>

#pragma hdrstop

#include "ruler.rh"

//Printout class (represents printed document)

class TRulerPrintout: public TPrintout {

public:

TRulerPrintout(TWindow* window, const char* title);

protected:

void PrintPage(int page, TRect& rect, unsigned flags);

private:

TWindow* parentWindow;

};

//Constructor

TRulerPrintout::TRulerPrintout(TWindow* window, const char* title)

: TPrintout(title)

{

parentWindow = window;

}

//Print pages of document (one page)

void

TRulerPrintout::PrintPage(int /*page*/, TRect& rect, unsigned /*flags*/)

{

parentWindow -> Paint(*DC, FALSE, rect);

}

//The application's main window

class TRulerWin: public TFrameWindow {

public:

TRulerWin(TWindow* parent, const char far* title);

~TRulerWin();

protected:

bool StackEmpty()

{ return stack.IsEmpty(); }

void Line(int x1, int y1, int x2, int y2)

{ dc->MoveTo(x1, y1); dc->LineTo(x2, y2); }

void Rectangle(int left, int top, int right, int bottom)

{ dc->Rectangle(left, top, right, bottom); }

void TextAt(int x, int y, const char *s)

{ dc->TextOut(x, y, s, strlen(s)); }

void InchRuler(int xOutline, int yOutline, int numInches);

void Ruler(int l, int r, int h, int level);

void Push(int l, int r, int m, int h, int level);

void Pop(int& l, int& r, int& m, int& h, int& level);

void CmPrint();

void CmPrinterSetup();

void Paint(TDC& paintDC, bool erase, TRect& rect);

private:

TPrinter* printer;

TDC* dc;

int unitsPerInch;

int numDivisions;

int largeMarkerSize;

int smallMarkerIncr;

int smallMarkerSize;

int left, top, right, bottom;

TStackAsVector<int> stack;

DECLARE_RESPONSE_TABLE(TRulerWin);

};

DEFINE_RESPONSE_TABLE1(TRulerWin, TFrameWindow)

EV_COMMAND(CM_PRINT, CmPrint),

EV_COMMAND(CM_PRINTERSETUP, CmPrinterSetup),

END_RESPONSE_TABLE;

//Constructor

TRulerWin::TRulerWin(TWindow* parent, const char far* title)

:TFrameWindow(parent, title),

TWindow(parent, title)

{

//Window initializations

AssignMenu(ID_MENU);

Attr.Style |= WS_VSCROLL | WS_HSCROLL;

Scroller = new TScroller(this, 1, 1, 2000, 2000);

//Constant initialization

dc = 0;

unitsPerInch = 100;

numDivisions = 4;

smallMarkerIncr = 4;

left = top = right = bottom = 0;

//Pоsitions dependent initializations

smallMarkerSize = smallMarkerIncr + (smallMarkerIncr * numDivisions);

largeMarkerSize = smallMarkerSize + (smallMarkerIncr * 2);

//Create printer interface object

printer = new TPrinter;

}

//Destructor

TRulerWin::~TRulerWin()

{

delete printer;

}

//Print window contents

void

TRulerWin::CmPrint()

{

if (printer)  {

TRulerPrintout printout(this, "Ruler");

printer->Print(this, printout, TRUE);

}

}

//Display printer setup dialog   

void

TRulerWin::CmPrinterSetup()

{

if (printer)

printer->Setup(this);

}

void

TRulerWin::Ruler(int l, int r, int h, int level)

{

int m;

Push(l, r, 0, h, level);

while (!StackEmpty()) {

Pop(l, r, m, h, level);

while (level > 0)  {

if (h <= 0)

throw "Levels incomplete";

m = (l + r) / 2;

Line(m, -top, m, -(top + h));

h -= smallMarkerIncr;

level--;

Push(m, r, m, h, level);

r = m;

}

}

}

void

TRulerWin::InchRuler(int xOutline, int yOutline, int numInches)

{

int i;

int x, y;

char s[4];

//Initialize and draw ruler outline

left = xOutline;

top = yOutline;

right = left + (numInches * unitsPerInch);

bottom = top + (largeMarkerSize * 3);

Rectangle(left, -top, right, -bottom);

//Label main ruler markers at every inch

y = top + largeMarkerSize;

x = left;

for (i = 1; i < numInches; i++)  {

x += unitsPerInch;

Line(x, -top, x, -y);

sprintf(s, "%d", i);

TextAt(x, -y, s);

}

//Call Ruler() function to display ruler markings

x = left;

for (i = 0; i < numInches; i++) {

try {

Ruler(x, x + unitsPerInch, smallMarkerSize, numDivisions);

}

catch (const char *msg) {

throw TXOwl(msg);

}

x += unitsPerInch;

}

}

//Push integer arguments onto stack

void

TRulerWin::Push(int l, int r, int m, int h, int level)

{

stack.Push(l);

stack.Push(r);

stack.Push(m);

stack.Push(h);

stack.Push(level);

}

//Pop integer arguments from stack

void

TRulerWin::Pop(int& l, int& r, int& m, int& h, int& level)

{

level = stack.Pop();

h = stack.Pop();

m = stack.Pop();

r = stack.Pop();

l = stack.Pop();

}

//Respond to WM_PAINT messages

void

TRulerWin::Paint(TDC& paintDC, bool /*erase*/, TRect& /*rect*/)

{

dc = &paintDC;

//Initializa device context

dc->SaveDC();

dc->SetMapMode(MM_LOENGLISH);

InchRuler(3, 3, 6);  //x==3, y==3, length==6 inches

//Restore changes made to device context

dc->RestoreDC();

}

//The application class

class TRulerApp: public TApplication {

public:

TRulerApp(const char far* name)

: TApplication(name) {}

void InitMainWindow();

};

//Initialize the program's main window

void

TRulerApp::InitMainWindow()

{

EnableCtl3d();

MainWindow = new TRulerWin(0, "Sigle-Page Printing");

}

#pragma argsused

//Main program

int

OwlMain(int argc, char* argv[])

{

TRulerApp app("RulerApp");

return app.Run();

}  

Похожие материалы

Информация о работе