View on GitHub

WinAPI

Selected subset of Win (user32, shell32) API Function Calls for Window, Taskbar, etc manipulation, conveniently organized and wrapped in a C# DLL

WinAPI License: MIT DOI

Selected Win API Function Calls for Window, Taskbar, Screens, etc manipulation, conveniently organized and wrapped in a C# DLL

Find Window

A simple wrap - FindWindow:

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr FindWindow(string strClassName, string strWindowName);
        
        public IntPtr FindWindow(string WindowName)
        { return FindWindow(null, WindowName); }

A more demanding wrap - GetWindowSize:

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
        
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
        public RECT GetWindowRectangle(string WindowName) {
            RECT pRect;
            GetWindowRect(FindWindow(null, WindowName), out pRect);
            return pRect;
        }
        public Size GetWindowSize(string WindowName) {
            RECT pRect;
            Size cSize = new Size();
            // get coordinates relative to window
            GetWindowRect(FindWindow(null, WindowName), out pRect);
            cSize.Width = pRect.Right - pRect.Left;
            cSize.Height = pRect.Bottom - pRect.Top;
            return cSize;
        }

Show Window

Derived functions that use private const values:

        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        
        public bool RestoreWindow(string WindowName)
        { return ShowWindow(FindWindow(null, WindowName), SW_RESTORE); }
        
        public bool ShowWindow(string WindowName)
        { return ShowWindow(FindWindow(null, WindowName), SW_SHOW); }
        
        public bool MaximizeWindow(string WindowName)
        { return ShowWindow(FindWindow(null, WindowName), SW_SHOWMAXIMIZED); }
        
        public bool MinimizeWindow(string WindowName)
        { return ShowWindow(FindWindow(null, WindowName), SW_MINIMIZE); }

Move Window

Move window by name (string WindowName):

[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

public bool MoveWindowByName(string WindowName, int X, int Y, int Width, int Height)
{  return MoveWindow(FindWindow(null, WindowName), X, Y, Width, Height, true);  }

Taskbar

Show/hide taskbar (AppBarStates is a subset of a more verbose AppBarMessages enum):

        public enum AppBarStates {
            AutoHide = 0x01,
            AlwaysOnTop = 0x02
        }
        public void SetTaskbarState(AppBarStates option) {
            APPBARDATA msgData = new APPBARDATA();
            msgData.cbSize = (UInt32)Marshal.SizeOf(msgData);
            msgData.hWnd = FindWindow("System_TrayWnd", null);
            msgData.lParam = (Int32)(option);
            SHAppBarMessage((UInt32)AppBarMessages.SetState, ref msgData);
        }

References

WinUser MoveWindow function
WinUser ShowWindow function
System Windows Forms > Screen > AllScreens