Wednesday, August 22, 2007

Z-Order hack for WinForms interop controls in WPF

If you ever tried to put something from WPF world over Windows Forms controls (interop), you, probably, found it impossible. After it, while searching internet, you found AirSpace article, that clearly describes the reasons and rules. Digging more will bring you to the blog of Jeremiah Morrill, who fighting with AirSpace issue about a year. But this can not help you (and especially to your angry client). From one hand, it's too hard to tell your client: "Sorry, we do now support it". From the other hand, you do not want to mess with complicated 3rd party controls with fair performance. What to do? Scrap it with GDI+. Here the example of scrapping WebBrowser control with it's internal function DrawToBitmap(). We can incorporate it into BitmapSource solution and have nice graphics instead of static control, when you need it. Just collapse WindowsFormsHost and Show Image control with graphical representation of  your Win32 control

 

BitmapSource GetScreen()

{


Bitmap bm = new Bitmap(wb.ClientRectangle.Width, wb.ClientRectangle.Height);

wb.DrawToBitmap(bm, wb.Bounds);


BitmapSource src = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bm.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,

System.Windows.Media.Imaging.
BitmapSizeOptions.FromEmptyOptions());

src.Freeze();


bm.Dispose();


bm =
null;

return src;




}




Wait a second... Why it takes a while to capture the browser screen? Let's see what Control.DrawToBitmap() method doing.





 

public void DrawToBitmap(Bitmap bitmap, Rectangle targetBounds)

{





int width = Math.Min(this.Width, targetBounds.Width);

int height = Math.Min(this.Height, targetBounds.Height);

Bitmap image = new Bitmap(width, height, bitmap.PixelFormat);

using (Graphics graphics = Graphics.FromImage(image))

{


IntPtr hdc = graphics.GetHdc();

using (Graphics graphics2 = Graphics.FromImage(bitmap))

{


IntPtr handle = graphics2.GetHdc();

BitBlt(
new HandleRef(graphics2, handle), targetBounds.X, targetBounds.Y, width, height, new HandleRef(graphics, hdc), 0, 0, 0xcc0020);

graphics2.ReleaseHdcInternal(handle);


}


graphics.ReleaseHdcInternal(hdc);


}




Well, why to repaint graphics with BitBlit? Really, I can not see any reason of it. Let's just copy handler





 

BitmapSource GetScreenInt()



{



Bitmap bm =new Bitmap(wb.ClientRectangle.Width, wb.ClientRectangle.Height);

Graphics g =Graphics.FromImage(bm);

PrintWindow(wb.Handle, g.GetHdc(), 0);


g.ReleaseHdc();


g.Flush();





BitmapSource src = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bm.GetHbitmap(),IntPtr.Zero,Int32Rect.Empty,

System.Windows.Media.Imaging.
BitmapSizeOptions.FromEmptyOptions());

src.Freeze();


bm.Dispose();


bm =
null;

return src;




}


Now, the operation takes less, then 0.3 seconds. Pretty good, isn't it? The only thing, you'll need is unmanaged signature of PrintWindow. Here is comes





 

[DllImport("user32.dll", SetLastError=true)]

static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);

 






Have a nice day

No comments: