一
一天到晚游泳的瑜
Unregistered / Unconfirmed
GUEST, unregistred user!
这是以前一个富翁给我的打印窗体的程序,用了挺长一段时间了,一直感觉还不错,但
最近有人反应说就是打印不出来什么东西,我不知道原因出在什么地方,还请各位大虾
多指教。我在激光打印机和喷墨打印机上都试过。先谢了。
var
dc: HDC;
isDcPalDevice: BOOL;
MemDc: HDC;
MemBitmap: hBitmap;
OldMemBitmap: hBitmap;
hDibHeader: THandle;
pDibHeader: pointer;
hBits: THandle;
pBits: pointer;
ScaleX:do
uble;
ScaleY:do
uble;
ppal: PLOGPALETTE;
pal: hPalette;
Oldpal: hPalette;
i: integer;
begin
//PrinterSetupDialog1.Execute;
dc := GetDc(0);
MemDc := CreateCompatibleDc(dc);
MemBitmap := CreateCompatibleBitmap(Dc, form1.width, form1.height);
OldMemBitmap := SelectObject(MemDc, MemBitmap);
isDcPalDevice := false;
if GetDeviceCaps(dc, RASTERCAPS) and RC_PALETTE = RC_PALETTE then
begin
GetMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
FillChar(pPal^, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)), #0);
pPal^.palVersion := $300;
pPal^.palNumEntries := GetSystemPaletteEntries(dc, 0, 256, pPal^.palPalEntry);
if pPal^.PalNumEntries <> 0 then
begin
pal := CreatePalette(pPal^);
oldPal := SelectPalette(MemDc, Pal, false);
isDcPalDevice := true
end
else
FreeMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
end;
BitBlt(MemDc, 0, 0, form1.width, form1.height, Dc, form1.left, form1.top, SrcCopy);
if isDcPalDevice = true then
begin
SelectPalette(MemDc, OldPal, false);
DeleteObject(Pal);
end;
SelectObject(MemDc, OldMemBitmap);
DeleteDc(MemDc);
hDibHeader := GlobalAlloc(GHND, sizeof(TBITMAPINFO) + (sizeof(TRGBQUAD) * 256));
pDibHeader := GlobalLock(hDibHeader);
FillChar(pDibHeader^, sizeof(TBITMAPINFO) + (sizeof(TRGBQUAD) * 256), #0);
PBITMAPINFOHEADER(pDibHeader)^.biSize := sizeof(TBITMAPINFOHEADER);
PBITMAPINFOHEADER(pDibHeader)^.biPlanes := 1;
PBITMAPINFOHEADER(pDibHeader)^.biBitCount := 8;
PBITMAPINFOHEADER(pDibHeader)^.biWidth := form1.width;
PBITMAPINFOHEADER(pDibHeader)^.biHeight := form1.height;
PBITMAPINFOHEADER(pDibHeader)^.biCompression := BI_RGB;
{find out how much memory for the bits}
GetDIBits(dc, MemBitmap, 0, form1.height, nil, TBitmapInfo(pDibHeader^),
DIB_RGB_COLORS);
hBits := GlobalAlloc(GHND, PBitmapInfoHeader(pDibHeader)^.BiSizeImage);
pBits := GlobalLock(hBits);
GetDIBits(dc, MemBitmap, 0, form1.height, pBits, PBitmapInfo (pDibHeader)^, DIB_RGB_COLORS);
if isDcPalDevice = true then
begin
for i := 0 to (pPal^.PalNumEntries - 1)do
begin
PBitmapInfo(pDibHeader)^.bmiColors.rgbRed := pPal^.palPalEntry.peRed;
PBitmapInfo(pDibHeader)^.bmiColors.rgbGreen := pPal^.palPalEntry.peGreen;
PBitmapInfo(pDibHeader)^.bmiColors.rgbBlue := pPal^.palPalEntry.peBlue;
end;
FreeMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
end;
ReleaseDc(0, dc);
DeleteObject(MemBitmap);
Printer.begin
Doc;
if Printer.PageWidth < Printer.PageHeight then
begin
ScaleX := Printer.PageWidth;
ScaleY := form1.Height * (Printer.PageWidth / form1.Width);
end
else
begin
ScaleX := form1.Width * (Printer.PageHeight / form1.Height);
ScaleY := Printer.PageHeight;
end;
isDcPalDevice := false;
if GetDeviceCaps(Printer.Canvas.Handle, RASTERCAPS) and
RC_PALETTE = RC_PALETTE then
begin
GetMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
FillChar(pPal^, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)), #0);
pPal^.palVersion := $300;
pPal^.palNumEntries := 256;
for i := 0 to (pPal^.PalNumEntries - 1)do
begin
pPal^.palPalEntry.peRed := PBitmapInfo(pDibHeader)^.bmiColors.rgbRed;
pPal^.palPalEntry.peGreen := PBitmapInfo(pDibHeader)^.bmiColors.rgbGreen;
pPal^.palPalEntry.peBlue := PBitmapInfo(pDibHeader)^.bmiColors.rgbBlue;
end;
pal := CreatePalette(pPal^);
FreeMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
oldPal := SelectPalette(Printer.Canvas.Handle, Pal, false);
isDcPalDevice := true
end;
StretchDiBits(Printer.Canvas.Handle, 0, 0, Round(scaleX), Round(scaleY), 0, 0,
form1.Width, form1.Height, pBits, PBitmapInfo(pDibHeader)^,
DIB_RGB_COLORS, SRCCOPY);
if isDcPalDevice = true then
begin
SelectPalette(Printer.Canvas.Handle, oldPal, false);
DeleteObject(Pal);
end;
GlobalUnlock(hBits);
GlobalFree(hBits);
GlobalUnlock(hDibHeader);
GlobalFree(hDibHeader);
Printer.EndDoc;
最近有人反应说就是打印不出来什么东西,我不知道原因出在什么地方,还请各位大虾
多指教。我在激光打印机和喷墨打印机上都试过。先谢了。
var
dc: HDC;
isDcPalDevice: BOOL;
MemDc: HDC;
MemBitmap: hBitmap;
OldMemBitmap: hBitmap;
hDibHeader: THandle;
pDibHeader: pointer;
hBits: THandle;
pBits: pointer;
ScaleX:do
uble;
ScaleY:do
uble;
ppal: PLOGPALETTE;
pal: hPalette;
Oldpal: hPalette;
i: integer;
begin
//PrinterSetupDialog1.Execute;
dc := GetDc(0);
MemDc := CreateCompatibleDc(dc);
MemBitmap := CreateCompatibleBitmap(Dc, form1.width, form1.height);
OldMemBitmap := SelectObject(MemDc, MemBitmap);
isDcPalDevice := false;
if GetDeviceCaps(dc, RASTERCAPS) and RC_PALETTE = RC_PALETTE then
begin
GetMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
FillChar(pPal^, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)), #0);
pPal^.palVersion := $300;
pPal^.palNumEntries := GetSystemPaletteEntries(dc, 0, 256, pPal^.palPalEntry);
if pPal^.PalNumEntries <> 0 then
begin
pal := CreatePalette(pPal^);
oldPal := SelectPalette(MemDc, Pal, false);
isDcPalDevice := true
end
else
FreeMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
end;
BitBlt(MemDc, 0, 0, form1.width, form1.height, Dc, form1.left, form1.top, SrcCopy);
if isDcPalDevice = true then
begin
SelectPalette(MemDc, OldPal, false);
DeleteObject(Pal);
end;
SelectObject(MemDc, OldMemBitmap);
DeleteDc(MemDc);
hDibHeader := GlobalAlloc(GHND, sizeof(TBITMAPINFO) + (sizeof(TRGBQUAD) * 256));
pDibHeader := GlobalLock(hDibHeader);
FillChar(pDibHeader^, sizeof(TBITMAPINFO) + (sizeof(TRGBQUAD) * 256), #0);
PBITMAPINFOHEADER(pDibHeader)^.biSize := sizeof(TBITMAPINFOHEADER);
PBITMAPINFOHEADER(pDibHeader)^.biPlanes := 1;
PBITMAPINFOHEADER(pDibHeader)^.biBitCount := 8;
PBITMAPINFOHEADER(pDibHeader)^.biWidth := form1.width;
PBITMAPINFOHEADER(pDibHeader)^.biHeight := form1.height;
PBITMAPINFOHEADER(pDibHeader)^.biCompression := BI_RGB;
{find out how much memory for the bits}
GetDIBits(dc, MemBitmap, 0, form1.height, nil, TBitmapInfo(pDibHeader^),
DIB_RGB_COLORS);
hBits := GlobalAlloc(GHND, PBitmapInfoHeader(pDibHeader)^.BiSizeImage);
pBits := GlobalLock(hBits);
GetDIBits(dc, MemBitmap, 0, form1.height, pBits, PBitmapInfo (pDibHeader)^, DIB_RGB_COLORS);
if isDcPalDevice = true then
begin
for i := 0 to (pPal^.PalNumEntries - 1)do
begin
PBitmapInfo(pDibHeader)^.bmiColors.rgbRed := pPal^.palPalEntry.peRed;
PBitmapInfo(pDibHeader)^.bmiColors.rgbGreen := pPal^.palPalEntry.peGreen;
PBitmapInfo(pDibHeader)^.bmiColors.rgbBlue := pPal^.palPalEntry.peBlue;
end;
FreeMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
end;
ReleaseDc(0, dc);
DeleteObject(MemBitmap);
Printer.begin
Doc;
if Printer.PageWidth < Printer.PageHeight then
begin
ScaleX := Printer.PageWidth;
ScaleY := form1.Height * (Printer.PageWidth / form1.Width);
end
else
begin
ScaleX := form1.Width * (Printer.PageHeight / form1.Height);
ScaleY := Printer.PageHeight;
end;
isDcPalDevice := false;
if GetDeviceCaps(Printer.Canvas.Handle, RASTERCAPS) and
RC_PALETTE = RC_PALETTE then
begin
GetMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
FillChar(pPal^, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)), #0);
pPal^.palVersion := $300;
pPal^.palNumEntries := 256;
for i := 0 to (pPal^.PalNumEntries - 1)do
begin
pPal^.palPalEntry.peRed := PBitmapInfo(pDibHeader)^.bmiColors.rgbRed;
pPal^.palPalEntry.peGreen := PBitmapInfo(pDibHeader)^.bmiColors.rgbGreen;
pPal^.palPalEntry.peBlue := PBitmapInfo(pDibHeader)^.bmiColors.rgbBlue;
end;
pal := CreatePalette(pPal^);
FreeMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
oldPal := SelectPalette(Printer.Canvas.Handle, Pal, false);
isDcPalDevice := true
end;
StretchDiBits(Printer.Canvas.Handle, 0, 0, Round(scaleX), Round(scaleY), 0, 0,
form1.Width, form1.Height, pBits, PBitmapInfo(pDibHeader)^,
DIB_RGB_COLORS, SRCCOPY);
if isDcPalDevice = true then
begin
SelectPalette(Printer.Canvas.Handle, oldPal, false);
DeleteObject(Pal);
end;
GlobalUnlock(hBits);
GlobalFree(hBits);
GlobalUnlock(hDibHeader);
GlobalFree(hDibHeader);
Printer.EndDoc;