使用DEVMOD修改打印机对应设置无法成功设置;
使用GPT或其他人工智能检索整理回答都不会采纳
/// <summary>
/// 设置打印机
/// </summary>
public void SettingPrint(PrinterSet printerSet)
{
//dm = (DEVMODE)Marshal.PtrToStructure(devModeIntptr, typeof(DEVMODE));
int ResultCode, n = 0;
// 空默认不使用
IntPtr temp = new IntPtr();
// 获取lpDevModeDmW的大小
DevModeSize = FunctionClass.DocumentProperties(0, printerSlef, printName, IntPtr.Zero, ref temp, 0);
if (DevModeSize == 0)
{
// TODO 抛出错误
return;
}
IntPtr hDevMode = GCFunction.GlobalAlloc(DevModeCode.GHND, DevModeSize);
devModeIntptr = GCFunction.GlobalLock(hDevMode);
//devModeIntptr = Marshal.AllocHGlobal(DevModeSize);
// 再次访问获取实际数据
ResultCode = FunctionClass.DocumentProperties(0, printerSlef, printName, devModeIntptr, ref temp, DevModeCode.DM_OUT_BUFFER);
if (ResultCode < 0)
{
// TODO 抛出错误
return;
}
GCFunction.Mem2DevCopy(out dm, devModeIntptr, DevModeSize);
// 填写自定义配置
dm.ExDevMode[DevModeCode.ExDevTop + DevModeCode.ExCut2inch] = printerSet.Cut2inch;
// 将打包好的发送回打印机并且设置
//Marshal.StructureToPtr(dm, devModeIntptr, true);
GCFunction.Mem2DevCopy(devModeIntptr,ref dm,DevModeSize);
GetDevMode();
pd.PrinterSettings.SetHdevmode(devModeIntptr);
}
每次Mem2DevCopy或者StructureToPtr之后,再次获取驱动设置,打印机的值未能生效