关于Graphics 和IntPtr 和DllImport("User32.dll")的一些问题???
先这样:
[DllImport("User32.dll")]
public extern static System.IntPtr GetDC(System.IntPtr hWnd);
[DllImport("User32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
然后下面这段用时间器反复执行:
IntPtr ScreenHandle = GetDC(IntPtr.Zero);
Graphics G = Graphics.FromHdc(ScreenHandle);
G.Draw……
InvalidateRect(System.IntPtr.Zero, System.IntPtr.Zero, false);
G.Dispose();
ReleaseDC(IntPtr.Zero, ScreenHandle);
上面这些是为了实现在一个游戏画面之上绘制一些辅助线,有几个地方不明白:
1.ScreenHandle = GetDC(IntPtr.Zero)得到的是个什么地方的句柄了?然后G = Graphics.FromHdc(ScreenHandle)是决定在刚才那个得到的地方绘图吗?
2.InvalidateRect(System.IntPtr.Zero, System.IntPtr.Zero, false)这句具体是什么意思?反正注释掉后程序就不能清除上次绘图结果,结果会叠加起来
3.ReleaseDC(IntPtr.Zero, ScreenHandle)这句具体是什么意思?是把ScreenHandle得到的句柄释放么?
然后,我准备改造一下,时间器反复执行使游戏有点迟缓,就把InvalidateRect(System.IntPtr.Zero, System.IntPtr.Zero, false)这句注释掉,然后设定一个按键点一次运行一次,又出现了几个问题:
1.这时绘图结果会叠加;所以把InvalidateRect(System.IntPtr.Zero, System.IntPtr.Zero, false)放在了IntPtr ScreenHandle = GetDC(IntPtr.Zero)之前,每次绘图前先清除再画;但不知道InvalidateRect的具体意义属于乱用呢,Graphics 有.clear这样的方法清除之前的结果么?
2.这样绘制出来的图不稳定,游戏画面一动,绘制出来的图就没了;有办法让图单独处在屏幕的最上层,可以挡住下面的图且不受下面的图的影响么?具体语句该怎么写?
分不够再加,谢谢大家