3

Background: I have a window that starts out with a width of 240px, some visual elements can only be displayed a particular way at this width, however resizing is allowed and must be allowed.

If someone decides to resize, then i will need some sort of "Snap" to get back to the original width. It would be a pain/next to impossible to require the users to get the exact px right in order to restore full visual effects.

Question: So, how can i make it so if they get to a certain distance (lets say, within 4px) away from 240px in either direction, it will snap to that width, and resize to a different size only when the resize cursor has strayed over 4px away from the width..

Failed Ideas: ive played with several different things that set the width of the window to 240 on the sizeChanged event if its within the 4px, but since that is on the size event, it will do the calculations after the window has already resized. which means that you get a really choppy effect when it is being resized to/away from the default width.

caesay
  • 16,932
  • 15
  • 95
  • 160
  • If you have only a margin of 4px to resize to and fro i would recomment not to resize the window at all and make it Fixed. User wont gain any visible space in just 4 pixels – Shekhar_Pro Feb 05 '11 at 07:16
  • ... and any way you want it to snap to you 240px then why allow user to resize the window at all.. use scroll bars oof you have some extra information to show – Shekhar_Pro Feb 05 '11 at 07:18
  • you didnt understand the question at all, @Shekhar_Pro... I said, that within 4px is the width that id like to snap to 240.. The window can be any size, from a width of 10 to a width of 1000. – caesay Feb 05 '11 at 07:24
  • So in other words, if they are resizing, and they come within 4px of 240, then snap to that. – caesay Feb 05 '11 at 07:24
  • This is pretty easy to do in WinForms by overriding the `WndProc` method and listening to Windows messages corresponding to resize events. That eliminates the choppiness of handling the events raised by the form. No idea if that works for WPF, though. I don't have nearly enough experience in that environment. – Cody Gray - on strike Feb 05 '11 at 07:32
  • @Cody Gray: I figured it would be something with wndproc in WF. but thats not an option for me. – caesay Feb 05 '11 at 07:40
  • @Cody: can you post an answer that would do it in WinForms? I think there may be a way to override WndProc in WPF aswel. – caesay Feb 05 '11 at 07:42
  • @Tommy: No, I think your first instinct was correct. Windows in WPF don't have an `hWnd`. I just don't know enough about WPF to know what's possible. [A similar question](http://stackoverflow.com/questions/624367/) indicates that the way to override `WndProc` in WPF is to use an `HWndHost`, but that may not even work as expected for this. That feeling is bolstered even more by the arguments made [here](http://stackoverflow.com/questions/1406244/wpf-layout-on-a-window/1412125#1412125). (There's a reason I haven't switched to WPF yet...) – Cody Gray - on strike Feb 05 '11 at 07:45
  • If we're going with Win32, we may try to subclass window that hosts WPF. As I understand, this will give us ability to handle messages before they pass to WPF's window procedure. – Marat Khasanov Feb 05 '11 at 09:18

1 Answers1

1

Here is a message that explains how to gain fine-grained control over the resizing by handling the WM_SIZING message in your own window procedure:

The moderator's sample code is in VB but there are plenty of C#/WinForms examples you can use to get started.

Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95