ComboBox2存的是当前打印机的进纸盒信息,也就是纸张来源信息
要在头文件中定义下面的
private: // User declarations
// Device Name
char szDeviceName[MAX_PATH],//[CCHDEVICENAME],
// Driver Name
szDriverName[MAX_PATH],
// Port Name
szPortName[MAX_PATH];
//其中MAX_PATH在windef.h中有定义:
#define MAX_PATH 260
void __fastcall TzzhPageSetupForm::PrinterComboBoxChange(TObject *Sender)
{
POINT *PaperSize;
int iLoop;
char *pszName;
char szTemp[MAX_PATH];
char *pszPos;
int iLength;
int iNumOfPapers;
WORD *pwPaperTypes;
char *pszPaperNames;
int iNumOfBins;
WORD *pwBinTypes;
char *pszBinNames;
// szDeviceName, szDriverName and szPortName as declared
// as private variables in the Form's class
//(in the Form's headerfile)
THandle hPrnDevMode; // Handle to the memory area
// containing the DEVMODE structure
// Get Printer Info (this way we avoid having to remove any
// eventual "On LPT1:" strings (which can be translated on
// other systems)
// Get Name
Printer()->GetPrinter(szDeviceName, szDriverName,
szPortName, hPrnDevMode);
// Check if either the port name or driver name is empty
if (!strlen(szPortName) || !strlen(szDriverName))
{
// --- szPortName or szDriverName was empty, fill them ---
GetProfileString("Devices", szDeviceName, "",
szTemp, MAX_PATH);
// Find the index of the next comma
pszPos = StrPos(szTemp, ",");
if (pszPos)
{
iLength = strlen(szTemp)-strlen(pszPos);
// Get the driver name
strncpy(szDriverName, szTemp, iLength);
szDriverName[iLength] = '\0'; // Terminate string
// Get the port name
strcpy(szPortName, ++pszPos);
} // ! if
} // ! if
// Notice: In the project header file I've created a const
// names CCHPAPERNAME with the value 64. This should have
// been done by Microsoft, but they didn't do it, so we have
// to do it ourselves ;)
// Note: szDeviceName, szDriverName and szPortName are
// delcared as private variables Form's class
//(in the Form's headerfile) and are already filled with
// the information we need.
// Get the number of papers
// int iNumOfPapers;
iNumOfPapers = DeviceCapabilitiesA(szDeviceName,
szPortName,
DC_PAPERS,
NULL,
NULL);
if(iNumOfPapers<=0)
Abort();
// Allocate memory to contain the list of paper type-numbers
pwPaperTypes = new WORD[iNumOfPapers];
// Get the list of types
DeviceCapabilitiesA(szDeviceName,
szPortName,
DC_PAPERS,
(LPTSTR)pwPaperTypes,
NULL); //DC_PAPERS
// Allocate memory to contain the list of papersize
PaperSize = new POINT[iNumOfPapers];
// Get the list of types
DeviceCapabilitiesA(szDeviceName,
szPortName,
DC_PAPERSIZE,
(LPTSTR)PaperSize,
NULL);
// Allocate memory to contain all paper names
pszPaperNames = new char[iNumOfPapers*64];
// Get list of paper names
DeviceCapabilitiesA(szDeviceName,
szPortName,
DC_PAPERNAMES,
pszPaperNames,
NULL);
// Fill the ListBox with the information
for (iLoop=0; iLoop < iNumOfPapers; iLoop++)
{
pszName = &pszPaperNames[iLoop*64];
ComboBox1->Items->AddObject(AnsiString(pszName)+
"="+IntToStr(pwPaperTypes[iLoop]),(TObject *)&PaperSize[iLoop]);
}
// Notice: In the project header file I've created a const
// names CCHBINNAME with the value 24. This should have been
// done by Microsoft, but they didn't do it, so we have to
// do it ourselves ;)
// Note: szDeviceName, szDriverName and szPortName are
// delcared as private variables Form's class (in the Form's
// headerfile) and are already filled with the information
// we need.
// Get the number of bins
iNumOfBins = DeviceCapabilitiesA(szDeviceName,
szPortName,
DC_BINS,
NULL,
NULL);
// Allocate memory to contain the list of bin type-numbers
pwBinTypes = new WORD[iNumOfBins];
// Get the list of types
DeviceCapabilitiesA(szDeviceName,
szPortName,
DC_BINS,
(LPTSTR)pwBinTypes,
NULL);
// Allocate memory to contain all bin names
pszBinNames = new char[iNumOfBins*24];
// Get list of paper names
DeviceCapabilitiesA(szDeviceName,
szPortName,
DC_BINNAMES,
pszBinNames,
NULL);
// Fill the ListBox with the information
// char *pszName;
for (iLoop=0; iLoop < iNumOfBins; iLoop++)
{
pszName = &pszBinNames[iLoop*24];
ComboBox2->Items->Add(AnsiString(pszName)
+"="+IntToStr(pwBinTypes[iLoop]));
}