Move window when external application's window moves in C#

Move window when external application's window moves in C#

To move a window in response to another window moving in C#, you can use the SetWindowPos function from the Windows API. Here are the general steps:

  • Get the window handles of both the external application's window and your own window using the FindWindow or FindWindowEx functions from the Windows API.

  • Define a delegate for the SetWindowPos function using the delegate keyword in C#.

private delegate bool SetWindowPosDelegate(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags); 
  • Load the SetWindowPos function using the LoadLibrary and GetProcAddress functions from the Windows API.
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags); private static SetWindowPosDelegate SetWindowPosFunction = GetSetWindowPosFunction(); private static SetWindowPosDelegate GetSetWindowPosFunction() { IntPtr user32 = LoadLibrary("user32.dll"); IntPtr address = GetProcAddress(user32, "SetWindowPos"); return (SetWindowPosDelegate)Marshal.GetDelegateForFunctionPointer(address, typeof(SetWindowPosDelegate)); } 
  • Define a method that will handle the window moving event of the external application's window.
private void ExternalWindowMoved(IntPtr hWnd) { // Get the position and size of the external window GetWindowRect(hWnd, out RECT rect); int x = rect.Left; int y = rect.Top; int cx = rect.Right - rect.Left; int cy = rect.Bottom - rect.Top; // Calculate the new position and size of your own window int newX = x + 50; // Example: Move 50 pixels to the right int newY = y + 50; // Example: Move 50 pixels down int newCX = cx; int newCY = cy; // Move your own window using SetWindowPos SetWindowPosFunction(this.Handle, IntPtr.Zero, newX, newY, newCX, newCY, SWP_NOZORDER); } 
  • Register the window moving event of the external application's window using the SetWinEventHook function from the Windows API.
private const uint EVENT_OBJECT_LOCATIONCHANGE = 0x800B; [DllImport("user32.dll")] private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags); private delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); private static WinEventDelegate ExternalWindowMovedDelegate = new WinEventDelegate((hWinEventHook, eventType, hwnd, idObject, idChild, dwEventThread, dwmsEventTime) => { ExternalWindowMoved(hwnd); }); private static IntPtr HookHandle = SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE, IntPtr.Zero, ExternalWindowMovedDelegate, 0, 0, 0); 

By following these steps, you should be able to move your own window in response to another window moving in C#. Note that this approach relies on the Windows API, and may not work on all platforms or in all scenarios.

Examples

  1. "C# detect external application window movement"

    • Description: This query suggests a need to detect when a window of an external application moves and trigger an action in C#.

    • Code:

      // Detect external window movement using Windows API private const int WM_MOVE = 0x0003; protected override void WndProc(ref Message m) { if (m.Msg == WM_MOVE) { // External window moved, handle the event HandleExternalWindowMove(); } base.WndProc(ref m); } private void HandleExternalWindowMove() { // Code to handle external window movement } 
  2. "C# move own window in response to external window movement"

    • Description: This query indicates a desire to move the current application's window when a window of an external application moves.

    • Code:

      // Move own window when external window moves private const int WM_MOVE = 0x0003; protected override void WndProc(ref Message m) { if (m.Msg == WM_MOVE) { // External window moved, move own window accordingly MoveOwnWindow(); } base.WndProc(ref m); } private void MoveOwnWindow() { // Code to move own window } 
  3. "C# detect external window position changes"

    • Description: This query suggests looking for a way to detect changes in the position of an external application's window.

    • Code:

      // Detect external window position changes private const int WM_WINDOWPOSCHANGED = 0x0047; protected override void WndProc(ref Message m) { if (m.Msg == WM_WINDOWPOSCHANGED) { // External window position changed, handle the event HandleExternalWindowPositionChange(); } base.WndProc(ref m); } private void HandleExternalWindowPositionChange() { // Code to handle external window position change } 
  4. "C# move window in response to external window resize"

    • Description: This query indicates a desire to move the current application's window when a window of an external application is resized.

    • Code:

      // Move own window when external window is resized private const int WM_SIZE = 0x0005; protected override void WndProc(ref Message m) { if (m.Msg == WM_SIZE) { // External window resized, move own window accordingly MoveOwnWindow(); } base.WndProc(ref m); } private void MoveOwnWindow() { // Code to move own window } 
  5. "C# monitor external window position changes continuously"

    • Description: This query suggests a continuous monitoring approach to detect changes in the position of an external application's window.

    • Code:

      // Monitor external window position changes continuously private const int WM_WINDOWPOSCHANGING = 0x0046; protected override void WndProc(ref Message m) { if (m.Msg == WM_WINDOWPOSCHANGING) { // External window position changing, continuously monitor MonitorExternalWindowPosition(); } base.WndProc(ref m); } private void MonitorExternalWindowPosition() { // Code to continuously monitor external window position changes } 
  6. "C# move window based on external process events"

    • Description: This query suggests using external process events to trigger the movement of the current application's window.

    • Code:

      // Move window based on external process events private void MonitorExternalProcessEvents() { // Code to monitor external process events // If relevant event occurs, move own window MoveOwnWindow(); } private void MoveOwnWindow() { // Code to move own window } 
  7. "C# synchronize window movement with external application"

    • Description: This query hints at synchronizing the movement of the current application's window with the movement of an external application's window.

    • Code:

      // Synchronize window movement with external application private void SynchronizeWindowMovement() { // Code to synchronize window movement with external application } private void HandleExternalWindowMove() { // Code to handle external window movement // Trigger synchronization if needed SynchronizeWindowMovement(); } 
  8. "C# detect external window focus changes"

    • Description: This query suggests looking for a way to detect changes in focus for an external application's window.

    • Code:

      // Detect external window focus changes private const int WM_ACTIVATE = 0x0006; protected override void WndProc(ref Message m) { if (m.Msg == WM_ACTIVATE) { // External window focus changed, handle the event HandleExternalWindowFocusChange(); } base.WndProc(ref m); } private void HandleExternalWindowFocusChange() { // Code to handle external window focus change } 
  9. "C# move window based on external window title changes"

    • Description: This query implies a desire to move the current application's window based on changes in the title of an external application's window.

    • Code:

      // Move window based on external window title changes private void MonitorExternalWindowTitleChanges() { // Code to monitor external window title changes // If title changes, move own window MoveOwnWindow(); } private void MoveOwnWindow() { // Code to move own window } 
  10. "C# detect external window state changes"

    • Description: This query indicates a need to detect changes in the state (minimized, maximized, or restored) of an external application's window.

    • Code:

      // Detect external window state changes private const int WM_SYSCOMMAND = 0x0112; private const int SC_MINIMIZE = 0xF020; private const int SC_RESTORE = 0xF120; protected override void WndProc(ref Message m) { if (m.Msg == WM_SYSCOMMAND) { int wParam = m.WParam.ToInt32(); if (wParam == SC_MINIMIZE || wParam == SC_RESTORE) { // External window state changed, handle the event HandleExternalWindowStateChange(); } } base.WndProc(ref m); } private void HandleExternalWindowStateChange() { // Code to handle external window state change } 

More Tags

event-bubbling android-sharedpreferences rest boolean fpga windowbuilder emoticons bootbox rdlc java.util.date

More C# Questions

More Mortgage and Real Estate Calculators

More Various Measurements Units Calculators

More Biology Calculators

More Investment Calculators