MFC中如何获得默认打印机名称
只需要获得默认的打印机名称,而不需要弹出打印对话框
[解决办法]
声明
[DllImport( "winspool.drv " )]
public static extern int GetDefaultPrinter( System.IntPtr ptr , ref int size );
调用
int size = 0;
int n = GetDefaultPrinter( IntPtr.Zero , ref size );
System.IntPtr ptr = Marshal.AllocHGlobal( size );
n= GetDefaultPrinter( ptr , ref size );
String strprint = Marshal.PtrToStringAnsi( ptr , size );
我是从以前贴子复制的
这是c#的,mfc 直接调用api会更简单,
[解决办法]
下面这段代码能列出当前的系统上的打印机
#include <winspool.h>
DWORD dwSize,dwPrinters;
::EnumPrinters(PRINTER_ENUM_LOCAL,NULL,5,NULL,0,&dwSize,&dwPrinters);
BYTE *pBuffer=new BYTE[dwSize];
::EnumPrinters(PRINTER_ENUM_LOCAL,NULL,5,NULL,pBuffer,&dwSize,&dwPrinters);
if(dwPrinters!=0)
{
PRINTER_INFO_5 *pPrnInfo=(PRINTER_INFO_5 *)pBuffer;
for(int i=0;i <dwPrinters,i++)
{
cout < <pPrnInfo-> pPrinterName < <endl;
pPrnInfo++;//指针后移
}
}
delete []pBuffer;