Category : C++ Source Code
Archive   : TI1783.ZIP
Filename : TI1783.ASC

 
Output of file : TI1783.ASC contained in archive : TI1783.ZIP







PRODUCT : Borland C++ NUMBER : 1783
VERSION : ALL
OS : WIN
DATE : March 8, 1994 PAGE : 1/8

TITLE : AN EXAMPLE OF PRINTING IN WINDOWS






An Example of Printing in Windows
---------------------------------

Printing in Windows 3.1 has been simplified greatly. The steps
are as follows:

1. Create the printer DC
2. StartDoc()
3. For each page
a. StartPage()
b. Initialize printer DC for the page
c. DrawPage
d. EndPage()
4. EndDoc()
5. Delete the printer DC

To create the printer DC, you will need to know which printer
the user wants to print to. Most applications will ask the user
by using the common dialog for printer setup, PrintDlg().
Alternatively, the application can print to the current default
printer that is set by the printer applet in the control panel.
This is the method used by the example program. See the function
GetPrinterSettings() in the example program on how to extract
the strings from Win.ini.

Once you have the DC, you need to call StartDoc(), which
requires a DOCINFO as a parameter. This parameter must be
initialized and most importantly, the cbSize member must be set
to the size of DOCINFO.

Then for each page of your output, you will need to call
StartPage(). This will notify the printer that a new page is
about to start. After this call, be sure to initialize the
printer DC to your needs. NOTE that after each page, you will
need to re-initialize the printer DC because the DC forgets its
settings after each page.

Now you can start printing on the page by using the GDI
functions. In the example, a sample screen is displayed and when
you double-click in the client area, the sample screen is
printed. It is important for the application to know what













PRODUCT : Borland C++ NUMBER : 1783
VERSION : ALL
OS : WIN
DATE : March 8, 1994 PAGE : 2/8

TITLE : AN EXAMPLE OF PRINTING IN WINDOWS




capabilities the printer has. For example, dot matrix printers
do not support bitblts of bitmaps. For these situations, you
need to use different techniques for printing.

When you're done with the page, EndPage() must be called. This
tells the printer to eject the page. The printer DC is
re-initialized to the starting state, so be sure to re-initialize
the printer DC for the next page. Finally, when you're done with
the printout, call EndDoc().

This technique will work with most printers, but not all.
Generally, printer with memory (postscript) can use this
technique. If you're using either PCL or dot matrix, you may
have to use a technique called banding. For more information
about banding, please take a look at the Microsoft Software
Development Kit Guide to Programming.



#
# makefile
#
.autodepend

ROOT = Printer
OBJ = $(ROOT).obj
LIB =
CC = bcc -H=$(ROOT).pch -ml -w -v

$(ROOT).exe: $(ROOT).res $(ROOT).def $(OBJ)
tlink /m /s /c /C /v @&&!
c0wl $(OBJ)
$(ROOT).exe

import.lib $(LIB) mathwl.lib cwl.lib
$(ROOT).def
!
rc $(ROOT).exe
buildsym $(ROOT).exe

.c.obj:
$(CC) -c {$< }














PRODUCT : Borland C++ NUMBER : 1783
VERSION : ALL
OS : WIN
DATE : March 8, 1994 PAGE : 3/8

TITLE : AN EXAMPLE OF PRINTING IN WINDOWS




.cpp.obj:
$(CC) -c {$< }



// printer.h
#ifndef Printer_H
#define Printer_H 1

// ---------------------------------------------------------- //
// Global defines //
// ---------------------------------------------------------- //
#define lpszAPPSTR "Printer"
#define lpszMENUNAME NULL
#define lpszCLASSNAME "PrinterClass"
#define lpszTITLE "Printer Window"

// ---------------------------------------------------------- //
// function prototypes //
// ---------------------------------------------------------- //
int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);


// ---------------------------------------------------------- //
// Main Window functions //
// ---------------------------------------------------------- //
LRESULT FAR PASCAL _export
MainWndProc(HWND, UINT, WPARAM, LPARAM);
void MainWnd_OnDestroy(HWND);
void MainWnd_OnPaint(HWND);
void MainWnd_OnLBDblClk(HWND, BOOL, int, int, UINT);

void GetPrinterSettings(char *, int, char **, char **, char **);
void DrawStuff(HDC, RECT);

#endif


// printer.cpp
#define WIN31
#define STRICT













PRODUCT : Borland C++ NUMBER : 1783
VERSION : ALL
OS : WIN
DATE : March 8, 1994 PAGE : 4/8

TITLE : AN EXAMPLE OF PRINTING IN WINDOWS




#include
#include
#pragma hdrstop
#include
#include "Printer.h"

// ---------------------------------------------------------- //
// Save the program's instance handle //
// ---------------------------------------------------------- //
HINSTANCE hInst;


// ---------------------------------------------------------- //
// Starting point of all Windows programs //
// ---------------------------------------------------------- //
#pragma argsused
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;

// Any previous instance?
if (!hPrevInstance)
// Initialize app-specific stuff
if (!InitApplication(hInstance))
return FALSE;
if (!InitInstance(hInstance, nCmdShow)) // Instance-specific
return FALSE;

// Standard Windows message loop
while (GetMessage(&msg, NULL, NULL, NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}


// ---------------------------------------------------------- //
// Initialize application-specific stuff //
// and register the class //
// ---------------------------------------------------------- //













PRODUCT : Borland C++ NUMBER : 1783
VERSION : ALL
OS : WIN
DATE : March 8, 1994 PAGE : 5/8

TITLE : AN EXAMPLE OF PRINTING IN WINDOWS




BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wc;

wc.style = CS_DBLCLKS;
wc.lpfnWndProc = (WNDPROC) MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = lpszMENUNAME;
wc.lpszClassName = lpszCLASSNAME;
return (RegisterClass(&wc));
}


// ---------------------------------------------------------- //
// Initialize instance and display the main window //
// ---------------------------------------------------------- //
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance;
hWnd = CreateWindow(lpszCLASSNAME, lpszTITLE,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);

if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}


// ---------------------------------------------------------- //
// Standard message-handler routine for main window //
// ---------------------------------------------------------- //
LRESULT FAR PASCAL _export MainWndProc(HWND hWnd, UINT message,













PRODUCT : Borland C++ NUMBER : 1783
VERSION : ALL
OS : WIN
DATE : March 8, 1994 PAGE : 6/8

TITLE : AN EXAMPLE OF PRINTING IN WINDOWS




WPARAM wParam, LPARAM lParam)
{
switch (message)
{
HANDLE_MSG(hWnd, WM_DESTROY, MainWnd_OnDestroy);
HANDLE_MSG(hWnd, WM_PAINT, MainWnd_OnPaint);
HANDLE_MSG(hWnd, WM_LBUTTONDBLCLK, MainWnd_OnLBDblClk);
}
return DefWindowProc(hWnd, message, wParam, lParam);
}


// ---------------------------------------------------------- //
// WM_DESTROY //
// ---------------------------------------------------------- //
#pragma argsused
void MainWnd_OnDestroy(HWND hWnd)
{
PostQuitMessage(0);
}


// ---------------------------------------------------------- //
// WM_PAINT //
// ---------------------------------------------------------- //
void MainWnd_OnPaint(HWND hWnd)
{
HDC hdc;
PAINTSTRUCT ps;
RECT r;

hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &r);
DrawStuff(hdc, r);
EndPaint(hWnd, &ps);
}


// ---------------------------------------------------------- //
// WM_LBUTTONDBLCLK //
// ---------------------------------------------------------- //
#pragma argsused
void MainWnd_OnLBDblClk(HWND hWnd, BOOL, int, int, UINT)













PRODUCT : Borland C++ NUMBER : 1783
VERSION : ALL

OS : WIN
DATE : March 8, 1994 PAGE : 7/8

TITLE : AN EXAMPLE OF PRINTING IN WINDOWS




{
HDC hdcPrn;
DOCINFO DocInfo;
char Temp[80];
char *PDevice, *PDriver, *PPort;

GetPrinterSettings(Temp, 80, &PDevice, &PDriver, &PPort);
hdcPrn = CreateDC(PDriver, PDevice, PPort, NULL);
if (hdcPrn != NULL)
{
DocInfo.cbSize = sizeof(DocInfo);
DocInfo.lpszDocName = "Test";
DocInfo.lpszOutput = NULL;
StartDoc(hdcPrn, &DocInfo);
StartPage(hdcPrn);

RECT r;
// left and top CANNOT be changed
r.left = r.top = 0;
r.right = 640;
r.bottom = 480;
DrawStuff(hdcPrn, r);

EndPage(hdcPrn);
EndDoc(hdcPrn);
DeleteDC(hdcPrn);
}
}


// ---------------------------------------------------------- //
// GetPrinterSettings //
// ---------------------------------------------------------- //
void GetPrinterSettings(char *lpPrintInfo, int size,
char **lpPrintDevice, char **lpPrintDriver, char **PrintPort)
{
GetProfileString("windows", "device", "", lpPrintInfo, size);
*lpPrintDevice = strtok(lpPrintInfo, ",");
lpPrintInfo += strlen(*lpPrintDevice) + 1;
*lpPrintDriver = strtok(lpPrintInfo, ",");
lpPrintInfo += strlen(*lpPrintDriver) + 1;
*PrintPort = lpPrintInfo;
}













PRODUCT : Borland C++ NUMBER : 1783
VERSION : ALL
OS : WIN
DATE : March 8, 1994 PAGE : 8/8

TITLE : AN EXAMPLE OF PRINTING IN WINDOWS




// ---------------------------------------------------------- //
// DrawStuff //
// ---------------------------------------------------------- //
void DrawStuff(HDC hdc, RECT r)
{
if (GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASDISPLAY)
{
// for the screen
SetMapMode(hdc, MM_TEXT);
}
else if (GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASPRINTER)
{
SetMapMode(hdc, MM_ANISOTROPIC);
SetViewportExt(hdc, GetDeviceCaps(hdc, HORZRES),
GetDeviceCaps(hdc, VERTRES));
SetWindowExt(hdc, r.right-r.left, r.bottom-r.top);
}
else return;

SetWindowOrg(hdc, 0, 0);

// draw here
MoveTo(hdc, 0, 0);
LineTo(hdc, 200, 350);
MoveTo(hdc, 0, 400);
LineTo(hdc, 356, 0);

char *msg = "0123456789";
TextOut(hdc, 20, 20, "0123456789", lstrlen(msg));
Rectangle(hdc, 200, 235, 350, 300);
Ellipse(hdc, 80, 100, 100, 120);
}



DISCLAIMER: You have the right to use this technical information
subject to the terms of the No-Nonsense License Statement that
you received with the Borland product to which this information
pertains.












  3 Responses to “Category : C++ Source Code
Archive   : TI1783.ZIP
Filename : TI1783.ASC

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/