locked
Getting a HBRUSH from a System.Drawing.Brush object... RRS feed

  • Question

  •  ... or the equivalent. I need to have a managed brush be created and passed to an unmanaged function that accepts a HBRUSH. Any ideas?

    Thanks in advance.
    Saturday, September 27, 2008 6:24 PM

Answers

  • You should just use p/invoke and call CreateSolidBrush or whatever and create the brush.  The brush class has a field with the handle for the hbrush.  You could use the following code to get the value.

                Brush brush = Brushes.SaddleBrown;
                IntPtr hbrush = IntPtr.Zero;
                FieldInfo field = typeof(Brush).GetField("nativeBrush",BindingFlags.NonPublic|BindingFlags.Instance);
                hbrush = (IntPtr)field.GetValue(brush);

    http://msdn.microsoft.com/en-us/library/ms532387(VS.85).aspx

    Saturday, September 27, 2008 7:00 PM