if (pd.hDevMode) GlobalFree(pd.hDevMode);
if (pd.hDevNames) GlobalFree(pd.hDevNames);
}
}
//---------------------------------------------------------------------------
UINT APIENTRY PrintHookProc(HWND hdlg, UINT uiMsg, WPARAM wParam,
LPARAM lParam)
{
if (uiMsg == WM_INITDIALOG)
{
// manipulate some controls in the hook procedure
// for example, disable the "Properties" button
HWND HPropButton = GetDlgItem(hdlg, psh2);
EnableWindow(HPropButton, false);
}
return 0L;
}
改变纸张大小:
//---------------------------------------------------------------------------
void __fastcall TForm1::PrintButtonClick(TObject *Sender)
{
// get the default printer name, driver, and port
char *def_string = "no printer";
char buffer[MAX_PATH];
GetProfileString("windows", "device", def_string,
buffer, MAX_PATH);
// parse the buffer to extract individual properties
int first_comma = Abuffer.Pos(",");
Device = Abuffer.SubString(1, first_comma - 1);
Abuffer = Abuffer.SubString(first_comma + 1,
Abuffer.Length() - first_comma);
int second_comma = Abuffer.Pos(",");
Driver = Abuffer.SubString(1, second_comma - 1);
Port = Abuffer.SubString(second_comma + 1,
Abuffer.Length() - second_comma);
// open the printer
HANDLE HPrinter;
if (OpenPrinter(Device.c_str(), &HPrinter, NULL))
{
// call DocumentProerties() with a zero fMode parameter
// to get the number of bytes required
long int num_bytes;
num_bytes = DocumentProperties(NULL, HPrinter, Device.c_str(),
NULL, NULL, 0);
// allocate the memory
unsigned char *buffer = new unsigned char[num_bytes];
// call DocumentProperties() again, this time to get the
// device mode information
DocumentProperties(NULL, HPrinter, Device.c_str(),
(PDEVMODE)buffer, NULL, DM_OUT_BUFFER);
PDEVMODE pdm = (PDEVMODE)buffer;
// modify the appropriate fields
pdm->dmPaperSize = DMPAPER_10X14; // for example
// tell Windows which fields we changed
pdm->dmFields = pdm->dmFields | DM_PAPERSIZE;
// call DocumentProperties() yet again, this time to get
// the final DEVMODE to pass into the CreateDC() function
DocumentProperties(NULL, HPrinter, Device.c_str(),
pdm, pdm, DM_OUT_BUFFER | DM_IN_BUFFER);