我在谈论财产PageSettings.PrinterResolution
。根据{{3}},此属性具有setter
,与属性PrinterResolution.X
相同,即使我无法为其设置新值。该属性仍然包含它的早期值,而不是新分配的值。
PrintDocument pd = new PrintDocument();
//assigning a printer to `pd`, etc...
//premise: pd.DefaultPageSettings.PrinterResolution.X has currently the value 200
现在,我尝试将新的int
值分配给X
:
pd.DefaultPageSettings.PrinterResolution.X = 300;
但在检查后,其价值pd.DefaultPageSettings.PrinterResolution.X
仍然包含200
。我只是想出一个新的PrinterResolution
对象,其中包含X
和Y
的设置值到pd.DefaultPageSettings.PrinterResolution
,可以根据需要更改值。
PrinterResolution changedRes = new PrinterResolution();
changedRes.X = 200;
changedRes.Y = 200;
pd.DefaultPageSettings.PrinterResolution = changedRes;
那为什么我不能设置属性的值?特别是为什么有记录的setter
,即使它不是"可用" 。
答案 0 :(得分:6)
如果您查看PageSettings
的{{3}},就会发现始终会从API重新请求PrinterResolution
(只要您不设置PrinterResolution
1}}手动属性):
public PrinterResolution PrinterResolution {
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Process, ResourceScope.Process)]
get {
if (printerResolution == null) {
IntSecurity.AllPrintingAndUnmanagedCode.Assert();
IntPtr modeHandle = printerSettings.GetHdevmode();
IntPtr modePointer = SafeNativeMethods.GlobalLock(new HandleRef(this, modeHandle));
SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE) UnsafeNativeMethods.PtrToStructure(modePointer, typeof(SafeNativeMethods.DEVMODE));
PrinterResolution result = PrinterResolutionFromMode(mode);
SafeNativeMethods.GlobalUnlock(new HandleRef(this, modeHandle));
SafeNativeMethods.GlobalFree(new HandleRef(this, modeHandle));
return result;
}
else
return printerResolution;
}
set {
printerResolution = value;
}
}
因此,只要您未手动设置PageSettings.PrinterResolution
,每次访问PrinterResolution
getter时,都会从API加载新的PageSettings.PrinterResolution
实例。此新实例再次包含原始X
值。您之前设置X
的实例将被丢弃。
至于为什么就像我对此不太了解一样。