printer.cpp
字号:
//-----------------------------------------------------------------------------------//
// Windows Graphics Programming: Win32 GDI and DirectDraw //
// ISBN 0-13-086985-6 //
// //
// Written by Yuan, Feng www.fengyuan.com //
// Copyright (c) 2000 by Hewlett-Packard Company www.hp.com //
// Published by Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com //
// //
// FileName : printer.cpp //
// Description: Printer demo program, Chapter 17 //
// Version : 1.00.000, May 31, 2000 //
//-----------------------------------------------------------------------------------//
#define STRICT
#define _WIN32_WINNT 0x0500
#define NOCRYPT
#pragma pack(push, 4)
#include <windows.h>
#pragma pack(pop)
#include <assert.h>
#include <tchar.h>
#include <math.h>
#include <stdio.h>
#include "..\..\include\win.h"
#include "..\..\include\dialog.h"
#include "..\..\include\Toolbar.h"
#include "..\..\include\Status.h"
#include "..\..\include\FrameWnd.h"
#include "..\..\include\GDIObject.h"
#include "..\..\include\pen.h"
#include "..\..\include\filedialog.h"
#include "..\..\include\outputsetup.h"
#include "..\..\include\fonttext.h"
#include "..\..\include\pagecanvas.h"
#include "..\..\include\areafill.h"
#include "resource.h"
//////////////////////////////////////////////////////////////
int nCall_AbortProc;
BOOL CALLBACK SimpleAbortProc(HDC hDC, int iError)
{
nCall_AbortProc ++;
return TRUE;
}
void SimplePrint(int nPages)
{
TCHAR temp[MAX_PATH];
DWORD size = MAX_PATH;
GetDefaultPrinter(temp, & size); // default printer name
HDC hDC = CreateDC(NULL, temp, NULL, NULL); // DC with default setting
if ( hDC )
{
nCall_AbortProc = 0;
SetAbortProc(hDC, SimpleAbortProc);
DOCINFO docinfo;
docinfo.cbSize = sizeof(docinfo);
docinfo.lpszDocName = _T("SimplePrint");
docinfo.lpszOutput = NULL;
docinfo.lpszDatatype = _T("EMF");
docinfo.fwType = 0;
if ( StartDoc(hDC, & docinfo) > 0 )
{
for (int p=0; p<nPages; p++) // one page at a time
if ( StartPage(hDC) <= 0 )
break;
else
{
int width = GetDeviceCaps(hDC, HORZRES);
int height = GetDeviceCaps(hDC, VERTRES);
int dpix = GetDeviceCaps(hDC, LOGPIXELSX);
int dpiy = GetDeviceCaps(hDC, LOGPIXELSY);
wsprintf(temp, _T("Page %d of %d"), p+1, nPages);
SetTextAlign(hDC, TA_TOP | TA_RIGHT );
TextOut(hDC, width, 0, temp, _tcslen(temp));
Rectangle(hDC, 0, 0, dpix, dpiy);
Rectangle(hDC, width, height, width-dpix, height-dpiy);
if ( EndPage(hDC)<0 )
break;
}
EndDoc(hDC);
}
DeleteDC(hDC);
}
wsprintf(temp, "AbortProc called %d times", nCall_AbortProc);
MessageBox(NULL, temp, "SimlePrint", MB_OK);
}
const TCHAR * R2_Names[] =
{
"R2_BLACK",
"R2_NOTMERGEPEN",
"R2_MASKNOTPEN",
"R2_NOTCOPYPEN",
"R2_MASKPENNOT",
"R2_NOT",
"R2_XORPEN",
"R2_NOTMASKPEN",
"R2_MASKPEN",
"R2_NOTXORPEN",
"R2_NOP",
"R2_MERGENOTPEN",
"R2_COPYPEN",
"R2_MERGEPENNOT",
"R2_MERGEPEN",
"R2_WHITE"
};
void Demo_LineCurve(HDC hDC, const RECT * rcPaint, int width, int height)
{
// ROP2
KLogFont logfont(- 10 * ONEINCH / 72, "Tahoma");
KGDIObject font(hDC, logfont.CreateFont());
for (int c=0; c<20; c++)
{
RECT rect = { c*200+400, 300, c*200+580, 4000 };
HBRUSH hBrush = CreateSolidBrush(PALETTEINDEX(c));
FillRect(hDC, & rect, hBrush);
DeleteObject(hBrush);
}
{
KGDIObject redbrush(hDC, CreateSolidBrush(RGB(0xFF, 0, 0)));
SelectObject(hDC, GetStockObject(NULL_PEN));
SetTextAlign(hDC, TA_TOP | TA_LEFT);
for (int r=R2_BLACK; r<=R2_WHITE; r++)
{
SetROP2(hDC, r);
TextOut(hDC, 4400, r*220+200, R2_Names[r-R2_BLACK], _tcslen(R2_Names[r-R2_BLACK]));
Rectangle(hDC, 300, r*220+200, 4300, r*220+400);
}
SetROP2(hDC, R2_COPYPEN);
}
{
KPen Red (PS_DOT, 0, RGB(0xFF, 0, 0));
KPen Blue(PS_SOLID, ONEINCH/36, RGB(0, 0, 0xFF));
for (int z=0; z<=2000; z+=400)
{
int x = 400, y = 6400;
POINT p[4] = { x, y, x+ 400, y-z, x+800, y-z, x+1200, y }; x+= 1300;
POINT q[4] = { x, y, x+ 400, y-z, x+800, y+z, x+1200, y }; x+= 1400;
POINT r[4] = { x, y, x+1500, y-z, x- 200, y-z, x+1300, y }; x+= 1800;
POINT s[4] = { x, y, x+1500, y-z, x- 200, y+z, x+1300, y }; x+= 1600;
POINT t[4] = { x+600, y, x, y-z, x+1300, y-z, x+600, y };
Red.Select(hDC);
Polyline(hDC, p, 4);
Polyline(hDC, q, 4);
Polyline(hDC, r, 4);
Polyline(hDC, s, 4);
Polyline(hDC, t, 4);
Red.UnSelect();
Blue.Select(hDC);
PolyBezier(hDC, p, 4);
PolyBezier(hDC, q, 4);
PolyBezier(hDC, r, 4);
PolyBezier(hDC, s, 4);
PolyBezier(hDC, t, 4);
Blue.UnSelect();
}
}
}
TCHAR * HS_Names [] =
{ _T("HS_HORIZONTAL"),
_T("HS_VERTICAL"),
_T("HS_FDIAGONAL"),
_T("HS_BDIAGONAL"),
_T("HS_CROSS"),
_T("HS_DIAGCROSS")
};
void Demo_AreaFill(HDC hDC, const RECT * rcPaint, int width, int height)
{
const COLORREF c0 = RGB(0x20, 0x20, 0x20);
const COLORREF c1 = RGB(0xF0, 0xF0, 0x20);
for (int i=0; i<4; i++)
GradientRectangle(hDC, 1000+1100*i, 500, 2000+1100*i, 1500, c0, c1, i*450);
for (i=0; i<4; i++)
SymGradientRectangle(hDC, 1000+1100*i, 1600, 2000+1100*i, 2600, c0, c1, i*450);
for (i=0; i<4; i++)
CornerGradientRectangle(hDC, 1000+1100*i, 2700, 2000+1100*i, 3700, c0, c1, i);
CenterGradientRectangle(hDC, 5600, 1500, 6600, 2500, c0, c1);
CenterGradientRectangle(hDC, 5600, 2600, 6600, 3600, c1, c0);
// Buttons
RoundRectButton(hDC, 0, 4000, 800, 4800, 0, 100, RGB(0x20, 0x20, 0x20), RGB(0xF0, 0xF0, 0x20));
RoundRectButton(hDC, 1000, 4000, 1800, 4800, 400, 100, RGB(0xF0, 0x20, 0x20), RGB(0x20, 0xF0, 0x20));
RoundRectButton(hDC, 2000, 4000, 2800, 4800, 800, 100, RGB(0xFF, 0xFF, 0x20), RGB(0x20, 0x20, 0xF0));
GradientRectangle(hDC, 0, 5000, 2000, 6000, RGB(0xFF, 0x0, 0), RGB(0, 0, 0xFF), 0);
HLSGradientRectangle(hDC, 0, 6200, 2000, 7200, RGB(0xFF, 0x0, 0), RGB(0, 0, 0xFF), 200);
RadialGradientFill(hDC, 3200, 6300, 3200 , 6300 , 1000, RGB(0xFF, 0xFF, 0xFF), RGB(0, 0, 0xFF), 8);
RadialGradientFill(hDC, 5300, 6300, 5300-300, 6300-600, 1000, RGB(0xFF, 0xFF, 0xFF), RGB(0, 0, 0xFF), 16);
RadialGradientFill(hDC, 7400, 6300, 7400-300, 6300+300, 1000, RGB(0xFF, 0xFF, 0xFF), RGB(0, 0, 0xFF), 256);
{
RECT r = { 7000, 500, 8500, 500+1500 };
KGDIObject blue(hDC, CreatePen(PS_SOLID, 1 * ONEINCH/72, RGB(0, 0, 0xFF)));
KGDIObject yellow(hDC, CreateSolidBrush(RGB(0xFF, 0xFF, 0)));
Rectangle(hDC, 7000, 500, 8500, 500+1500);
BrickPatternFill(hDC, 7000, 500, 8500, 500+1500, 150, 150);
BrickPatternFill(hDC, 7000, 2100, 8500, 2100+1500, 100, 100);
}
KLogFont logfont(- 8 * ONEINCH / 72, "Tahoma");
KGDIObject font(hDC, logfont.CreateFont());
{
SelectObject(hDC, GetStockObject(NULL_PEN));
typedef enum { GAP = 1200 };
for (int hs= HS_HORIZONTAL; hs<=HS_DIAGCROSS; hs++)
{
HBRUSH hBrush = CreateHatchBrush(hs, RGB(0, 0, 0xFF));
HGDIOBJ hOld = SelectObject(hDC, hBrush);
SetBkColor(hDC, RGB(0xFF, 0xFF, 0));
Rectangle(hDC, hs*GAP, 8000, hs*GAP+890, 8890);
SetBkColor(hDC, RGB(0xFF, 0xFF, 0xFF));
SetTextAlign(hDC, TA_CENTER);
TextOut(hDC, hs*GAP+880/2, 8980, HS_Names[hs-HS_HORIZONTAL], _tcslen(HS_Names[hs-HS_HORIZONTAL]));
SelectObject(hDC, hOld);
DeleteObject(hBrush);
}
}
{
HINSTANCE hCards = LoadLibrary("cards.dll");
for (int i=0; i<3; i++)
{
HBRUSH hBrush;
int width, height;
switch ( i )
{
case 0:
{
HBITMAP hBitmap = LoadBitmap(hCards, MAKEINTRESOURCE(52));
BITMAP bmp;
GetObject(hBitmap, sizeof(bmp), & bmp);
width = bmp.bmWidth; height = bmp.bmHeight;
hBrush = CreatePatternBrush(hBitmap);
DeleteObject(hBitmap);
}
break;
case 1:
{
HRSRC hResource = FindResource(hCards, MAKEINTRESOURCE(52-14), RT_BITMAP);
HGLOBAL hGlobal = LoadResource(hCards, hResource);
hBrush = CreateDIBPatternBrushPt(LockResource(hGlobal), DIB_RGB_COLORS);
width = ((BITMAPCOREHEADER *) hGlobal)->bcWidth; // old DIB format
height = ((BITMAPCOREHEADER *) hGlobal)->bcHeight;// old DIB format
}
break;
case 2:
{
HRSRC hResource = FindResource(hCards, MAKEINTRESOURCE(52-28), RT_BITMAP);
HGLOBAL hGlobal = LoadResource(hCards, hResource);
hBrush = CreateDIBPatternBrush(hGlobal, DIB_RGB_COLORS);
width = ((BITMAPCOREHEADER *) hGlobal)->bcWidth; // old DIB format
height = ((BITMAPCOREHEADER *) hGlobal)->bcHeight; // old DIB format
}
}
HGDIOBJ hOld = SelectObject(hDC, hBrush);
POINT P = { i*1400+200 + width*10*i/4, 10000 + height*10*i/4 };
LPtoDP(hDC, &P, 1);
SetBrushOrgEx(hDC, P.x, P.y, NULL); // make sure cards aligned with rectangle
Rectangle(hDC, i*1400+200, 10000, i*1400+200+width*30/2+1, 10000+height*30/2+1);
SelectObject(hDC, hOld);
DeleteObject(hBrush);
}
}
}
void Demo_Bitmap(HDC hDC, const RECT * rcPaint, int width, int height, HINSTANCE hInst)
{
HDC hMemDC = CreateCompatibleDC(NULL);
// load difference BITMAP as resource
HPALETTE hPal = CreateHalftonePalette(hDC);
HPALETTE hOld = SelectPalette(hDC, hPal, FALSE);
RealizePalette(hDC);
SetStretchBltMode(hDC, STRETCH_DELETESCANS);
for (int i=0; i<6; i++)
{
int x = 100 + (i%3) * 3200;
int y = 100 + (i/3) * 3200;
HBITMAP hBmp = LoadBitmap(hInst, MAKEINTRESOURCE(i+IDB_BITMAP1));
KGDIObject bmp(hMemDC, hBmp);
if ( hBmp==NULL ) break;
BITMAP info;
GetObject(hBmp, sizeof(BITMAP), & info);
StretchBlt(hDC, x, y,
info.bmWidth * ONEINCH / 120, info.bmHeight * ONEINCH / 120,
hMemDC, 0, 0, info.bmWidth, info.bmHeight, SRCCOPY);
}
SelectPalette(hDC, hOld, FALSE);
RealizePalette(hDC);
DeleteObject(hPal);
DeleteObject(hMemDC);
}
void Demo_Text(HDC hDC, const RECT * rcPaint, int width, int height)
{
KLogFont logfont(- 10 * ONEINCH / 72, "Times New Roman");
KGDIObject font(hDC, logfont.CreateFont());
const TCHAR * mess = "A quick brown fox jumps over a lazy dog. "
"A quick brown fox jumps over a lazy dog. "
"A quick brown fox jumps over a lazy dog. ";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -