Win32 CreatePatternBrush

MSDN displays the following for CreatePatternBrush:

You can remove the template brush without affecting the associated bitmap by using the DeleteObject function. Therefore, you can use this bitmap to create any number of brush patterns.

My question is the opposite. If HBRUSH is durable, can I remove HBITMAP immediately after creating the brush? IE: Does HBRUSH keep its own copy of HBITMAP?

In this case, I would like HBRUSH to have an object scope, while HBITMAP to have a method scope (a method that creates HBRUSH).

+4
source share
4 answers

HBRUSH and HBITMAP are completely independent. Handles can be removed completely independent of each other, and after they are created, no changes in any object will affect others.

+5
source

The brush has its own copy of the bitmap. This is easy to see by removing the bitmap after creating the brush and then using the brush (works great)

Using GetObject to populate the LOGBRUSH structure will return the original BITMAP descriptor to the lbhatch element, but, unfortunately, is not a copy descriptor. And the use of GetObject in the returned bitmap descriptor fails if the bitmap is deleted.

Does anyone know how to get the original sizes of the bitmap images from the brush in this case? I want to create a copy of the template brush, even if the original bitmap is deleted. I can get a copy of the original bitmap just by brushing, but I don’t know its size. I tried using SetbrushorgEx (hdc, -1, -1), hoping that -1 would be reduced in absolute value when the brush is selected in the device context and will get values ​​when I get GetBrushOrgEx. Does not work.

+4
source

I think the bitmap should survive the brush: the brush simply refers to the existing bitmap, and does not copy it.

You can always try and see what happened.

+1
source

I doubt the CreatePatternBrush () API is copying the bitmap you give it since HBITMAP:

  • a GDI handle, the maximum number of which is limited, and
  • potentially quite large.

Win32 and GDI tend to be conservative about making internal copies of your data, if only because when most of their APIs were created (CreatePatternBrush () dates before Windows 95, and many functions are even older), the memory and GDI descriptors were much more limited quantity than now. (For example, Windows 95 should have worked well on a system with only 4 MB of RAM.)

+1
source

Source: https://habr.com/ru/post/1276642/


All Articles