Hello, how to catch a window resize event? I mean that there would be certain actions in the window before the WindowState becomes Maximized , regardless of how the WindowState becomes Maximized .

  • 3
  • Is an event really necessary before changing the window? Maybe everything can be done on the event SizeChanged? - zuev93
  • @ zuev93, yes, just before you change the state of the window, you must perform certain actions. - Arthur Edgarov

2 answers 2

  private const int WM_SYSCOMMAND = 0x0112; private const int SC_MINIMIZE = 0xF020; public MainWindow() { InitializeComponent(); } [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { switch (msg) { case WM_SYSCOMMAND: int command = wParam.ToInt32() & 0xfff0; if (command == SC_MINIMIZE) { //before min } else { //before max } break; } return IntPtr.Zero; } private void Window_SourceInitialized(object sender, EventArgs e) { IntPtr windowHandle = (new WindowInteropHelper(this)).Handle; HwndSource src = HwndSource.FromHwnd(windowHandle); src.AddHook(new HwndSourceHook(WndProc)); } 

it is better to execute the code inside SourceInitialized inside this event or any other when the window is already initialized because

  (new WindowInteropHelper(this)).Handle 

will issue 0 until it is initialized

  • did everything as you wrote, but for some reason the program behaves in a strange way with this code. The point is, I have a Border , it serves as the top panel of my program, respectively, while the mouse button is held down, the DragMove() function is DragMove() , but with the addition of this code, the window simply turns to full screen with a simple mouse click on this Border . See the code below. - Arthur Edgarov
  • so correctly, I threw you a little garbage, the fact is that not only window deployment will fall down in the else ... in general, here is a link to winUser.h there you can find all the values ​​that wParam math.uiuc.edu/~gfrancis/ illimath / windows / aszgard_mini / bin / MinGW / ... i.e. in this case, you need to declare SC_MAXIMIZE 0xF030 and else to remove it altogether, the window is expanded because you explicitly call WindowState = WindowState.Maximized - Dmitriy
  • I instead of private const int SC_MINIMIZE = 0xF020; made private const int SC_MAXIMIZE = 0xF030; , and wrote everything in if (command == SC_MAXIMIZE) {} , but it does not work for some reason ... Do not tell me what could be the problem? - Arthur Edgarov
  • And what does not work, the code that should work according to this condition? Point after set int command = wParam.ToInt32 () & 0xfff0; and see what value in the command variable is stored and whether it is SC_MAXIMIZE, at least before I wrote the comments above, I created a test WPF project, everything worked) - Dmitriy
  • This is what happened when assigning the value of the variable command: command variable , variable SC_MAXIMIZE . - Arthur Edgarov
  private const int WM_SYSCOMMAND = 0x0112; private const int SC_MINIMIZE = 0xF020; private void music_SourceInitialized(object sender, EventArgs e) { IntPtr windowHandle = (new WindowInteropHelper(this)).Handle; HwndSource src = HwndSource.FromHwnd(windowHandle); src.AddHook(new HwndSourceHook(WndProc)); } [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { switch (msg) { case WM_SYSCOMMAND: int command = wParam.ToInt32() & 0xfff0; if (command == SC_MINIMIZE) { } else { border_main.CornerRadius = new CornerRadius(0); border_bottom.CornerRadius = new CornerRadius(0); ResizeMode = ResizeMode.NoResize; WindowState = WindowState.Maximized; } break; } return IntPtr.Zero; }