我有以下代码
public static class GammaRamp
{
[DllImport("gdi32.dll")]
private unsafe static extern bool SetDeviceGammaRamp(Int32 hdc, ushort* ramp);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData);
private static Int32 hdc;
//some other things
public static unsafe void SetGammaRamp(int aBrightness, int aTemperature)
{
//...some other things calculating rgbArray but not important for the question
foreach (System.Windows.Forms.Screen screen in System.Windows.Forms.Screen.AllScreens)
{
//Get the hardware device context of the screen
hdc = CreateDC(screen.DeviceName, null, null, IntPtr.Zero).ToInt32();
SetDeviceGammaRamp(hdc, rgbArray);
}
}
}
将 CreateDC 结果转换为 ToInt32 而不是保留它 IntPtr 然后调用 DeleteDC 是一种好习惯。我应该在 CreateDC 之后使用 DeleteDC 方法,但这是否必须在我的代码中?我希望这段代码不会出现某种内存问题。
是的,总是在不再需要时调用DeleteDC()
您创建的任何设备上下文CreateDC()
,并调用ReleaseDC()
您通过GetDC()
或获得的任何设备上下文GetWindowDC()
。
我想不出常见的情况是您不负责销毁您创建的 DC - 通常,假设您需要这样做,除非另有说明。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句