Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know when a WindowsFormsHost is resizing in WPF?

Tags:

c#

.net

wpf

xaml

I've got a windows forms map in a WindowsFormsHost, and I need it to resize with the window.

I'm just not sure what event to listen to, to do this. I need the map to only resize once the mouse is up, otherwise it lags out, and tries drawing itself a million times when you resize a window really slowly.

like image 804
Louis Sayers Avatar asked Oct 14 '22 17:10

Louis Sayers


1 Answers

Waiting on a timer is a very, very bad idea, quite simply, it's a heuristic and you are guessing when the resize operation is done.

A better idea would be to derive a class from WindowsFormsHost and override the WndProc method, handling the WM_SIZE message. This is the message that is sent to a window when the size operation is complete (as opposed to WM_SIZING, which is sent during the process).

You can also handle the WM_SIZING message, and not call the base implementation of WndProc when you get this message, to prevent the message from being processed and having the map redraw itself all those times.

The documentation for the WndProc method on the Control class shows how to override WndProc method:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc(VS.71).aspx

Even though it is for a different class, it's the same exact principal.

Additionally, you will need the values for the WM_SIZING and WM_SIZE constants, which you can find here:

http://www.pinvoke.net/default.aspx/Enums/WindowsMessages.html

Note that you don't need everything from the link above, just the declarations for those two values:

/// <summary>
/// The WM_SIZING message is sent to a window that
/// the user is resizing.  By processing this message,
/// an application can monitor the size and position
/// of the drag rectangle and, if needed, change its
/// size or position. 
/// </summary>
const int WM_SIZING = 0x0214;

/// <summary>
/// The WM_SIZE message is sent to a window after its
/// size has changed.
/// </summary>
const int WM_SIZE = 0x0005;
like image 138
casperOne Avatar answered Oct 20 '22 17:10

casperOne