为了账号安全,请及时绑定邮箱和手机立即绑定

在这种情况下我应该在 CreateDC 之后使用 DeleteDC

在这种情况下我应该在 CreateDC 之后使用 DeleteDC

C#
精慕HU 2023-09-24 10:39:20
我有以下代码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 方法,但这是否一定在我的代码中?我希望这段代码不会出现某种内存问题。
查看完整描述

1 回答

?
呼如林

TA贡献1798条经验 获得超3个赞

是的,当不再需要时,始终调用DeleteDC()您创建的任何设备上下文,并调用您通过或获取的任何设备上下文。CreateDC()ReleaseDC()GetDC()GetWindowDC()

我想不出您不负责销毁您创建的 DC 的常见情况 - 通常,假设您需要这样做,除非另有说明。


查看完整回答
反对 回复 2023-09-24
  • 1 回答
  • 0 关注
  • 50 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信