我有以下代碼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 的常見情況 - 通常,假設您需要這樣做,除非另有說明。
- 1 回答
- 0 關注
- 121 瀏覽
添加回答
舉報
0/150
提交
取消