Win32 创建图案画笔
MSDN 显示 CreatePatternBrush 的以下内容:
您可以删除图案画笔,无需 影响关联的位图 使用删除对象函数。 因此,您可以使用这个 位图创建任意数量的图案 画笔。
我的问题恰恰相反。 如果 HBRUSH 寿命较长,我可以在创建画笔后立即删除 HBITMAP 吗? IE:HBRUSH 是否存储自己的 HBITMAP 副本?
在这种情况下,我希望 HBRUSH 具有对象范围,而 HBITMAP 具有方法范围(创建 HBRUSH 的方法)。
MSDN displays the following for CreatePatternBrush:
You can delete a pattern brush without
affecting the associated bitmap by
using the DeleteObject function.
Therefore, you can then use this
bitmap to create any number of pattern
brushes.
My question is the opposite. If the HBRUSH is long lived, can I delete the HBITMAP right after I create the brush? IE: does the HBRUSH store its own copy of the HBITMAP?
In this case, I'd like the HBRUSH to have object scope while the HBITMAP would have method scope (the method that creates the HBRUSH).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为位图必须比画笔更长寿:画笔只是引用现有的位图而不是复制它。
你总是可以尝试一下,看看会发生什么。
I think the bitmap must outlive the brush: the brush just references the existing bitmap rather than copying it.
You could always try it and see what happened.
我怀疑 CreatePatternBrush() API 是否会复制您提供的位图,因为 HBITMAP 是:一个
Win32 和 GDI 在创建数据的内部副本方面往往比较保守,因为在创建它们的大多数 API 时(CreatePatternBrush() 可以追溯到 Windows 95,并且许多函数还更旧),内存和 GDI 句柄占用的内存和 GDI 句柄要多得多。供应比现在有限。 (例如,Windows 95 需要在只有 4MB RAM 的系统上良好运行。)
I doubt that the CreatePatternBrush() API copies the bitmap you give it, since an HBITMAP is:
Win32 and GDI tend to be conservative about creating internal copies of your data, if only because when most of their APIs were created (CreatePatternBrush() dates to Windows 95, and many functions are older still), memory and GDI handles were in much more limited supply than they are now. (For example, Windows 95 was required to run well on a system with only 4MB of RAM.)
画笔确实有自己的位图副本。 通过在创建画笔后删除位图然后使用画笔(工作正常)可以很容易地看到这一点。
但是,使用 GetObject 填充 LOGBRUSH 结构将返回成员 lbhatch 中的原始 BITMAP 句柄,而不是副本的句柄。 如果删除位图,则在返回的位图句柄上使用 GetObject 将失败。
在这种情况下,任何人都知道如何从画笔获取原始位图尺寸? 我希望创建图案画笔的副本,即使原始位图已删除。 我只需用画笔绘画即可获得原始位图的副本,但我不知道它的大小。 我尝试使用 SetbrushorgEx (hdc, -1,-1),希望当将画笔选择到设备上下文中时,-1 会以其尺寸为模减小,并在使用 GetBrushOrgEx 检索时获取值。 不起作用。
The brush does have its own copy of the bitmap. This is easily see by deleting the bitmap after creating the brush and then using the brush (works fine)
Using GetObject to fill a LOGBRUSH structure will return the original BITMAP handle in member lbhatch, though, and not the copy's handle, unfortunately. And using GetObject on the returned bitmap handle fails if the bitmap is deleted.
Anyone any idea how to get the original bitmap dimensions from the brush in this case? I wish to create a copy of the pattern brush even though the original bitmap is deleted. I can get a copy of the original bitmap simply by painting with the brush, but I don't know it's size. I tried using SetbrushorgEx (hdc, -1,-1), hoping the -1's would be reduced modulo its dimensions when brush selected into device context and get values when I retrieve with GetBrushOrgEx. Doesn't work.
HBRUSH 和 HBITMAP 是完全独立的。 句柄可以完全独立地删除,并且一旦创建,对任何一个对象的更改都不会影响另一个对象。
The HBRUSH and HBITMAP are entirely independent. The handles can be deleted entirely independent from each other, and, once created, no changes to either object will effect the other.