gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
C# setwindowpos and not focus
- User
C# setwindowpos and not focus
- Assistant
To use the
SetWindowPosfunction in C# and not focus the window, you can use theSWP_NOACTIVATEflag in combination with other flags. Here's an example:using System; using System.Runtime.InteropServices; class Program { [DllImport("user32.dll", SetLastError = true)] static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); const uint SWP_NOMOVE = 0x0002; const uint SWP_NOSIZE = 0x0001; const uint SWP_NOACTIVATE = 0x0010; static void Main() { // Get the handle of the window you want to set position for IntPtr hWnd = FindWindow(null, "Window Title"); // Set new position and size of the window SetWindowPos(hWnd, HWND_TOPMOST, 100, 100, 400, 300, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE); } }In this example,
SetWindowPossets the position and size of the window specified by thehWndparameter. TheSWP_NOACTIVATEflag prevents the window from being activated/focused when the position is set. You can combine this flag with other flags, such as and , to specify that the window should not be moved or sized.