Graphics.FromHwnd(IntPtr.Zero) 返回 null,为什么?
我目前正在调查我的应用程序中的第 3 方组件 (DevExpress) 的问题。 我的问题与这篇DevExpress 知识库文章非常相似。 我得到了同样的异常,但更多的堆栈跟踪却更少。 因此,我使用 .NET Reflector 来找出在这种情况下什么可能为 null,唯一可能为 null 的对象是 Graphics。 这是使用 Graphics.FromHwnd(IntPtr.Zero) 创建的。 因为我对 GDI 没有广泛的了解,我想知道是否有人可以告诉我可能返回 null 的情况...
我尝试在 Windows 超出 GDI 句柄的情况下重现它,但是然后我遇到了“无法处理”的情况 - 至少有一次异常,这在我正在调查的问题中不是这种情况
, 马丁
I'm currently investigating a problem with a 3rd party component (DevExpress) in my application.
My issue is quite similar to this one DevExpress KB article. I get the same exception with more less the same stacktrace.
So I used .NET Reflector to find out, what may be null in this scenario, and the only object which is a candiate to be null is Graphics.
This is created with Graphics.FromHwnd(IntPtr.Zero). Because I don't have a broad knowledge about GDI, I would like to know if somebody can tell me possible scenarios when this may return null...
I tried to reproduce it in a scenario where windows is out of GDI handle's, but then I'm getting a "out of handles" - exception at least once, which is not the case in the issue I'm investigating
tia,
Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您使用
IntPtr
结构并将其初始化为零。 然后将此句柄传递给 Graphics 类。 除非您有一个带有handle == 0
的窗口,否则null
值正是我所期望的。First of all, you are using the
IntPtr
structure and initializing it to zero. Then you are passing this handle to the Graphics class. Unless you have a window with ahandle == 0
, anull
value is exactly what I would expect.据我所知,HWND 0(实际上是 IntPtr.Zero)是当前用户会话桌面的句柄。 它可用于测量系统的各种指标,并且它应该是 Graphics.FromHwnd 的有效值。
然而,GDI+ 充满了错误,并且 .NET 与它的集成非常不稳定,很可能您的代码或 DevExpress 除了使用 GDI+ 之外没有任何问题。
From what I recall, an HWND of 0 (effectively IntPtr.Zero) is a handle to the current user session's desktop. It can be used to measure various metrics of the system and it should be a valid value for Graphics.FromHwnd.
However, GDI+ is so full of bugs and the .NET integration with it is so unstable it's very possible there's nothing wrong with your code or DevExpress other than it just uses GDI+.