Click here to Skip to main content
15,610,088 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I wanted to create a transparent overlay window on top active window.
The overlay window consists of canvas and dynamic buttons.

I am able to draw overlay window but the position is not correct with respect to active window. I am passing same measurement of active window, but still not able to get exactly on top of active window.

The overlay window size is bigger than active top window and mispositioned.
Please help..
Stuck from long time.

What I have tried:

For getting Active window code-

public partial class MainWindow : Window
        static IntPtr handle = IntPtr.Zero;

        static extern IntPtr GetForegroundWindow();
        private static IntPtr GetActiveWindow()
            IntPtr handle = IntPtr.Zero;
            return GetForegroundWindow();
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

        public struct RECT
            public int Left;        // x position of upper-left corner  
            public int Top;         // y position of upper-left corner  
            public int Right;       // x position of lower-right corner  
            public int Bottom;      // y position of lower-right corner  
        //   [DllImport("user32.dll")]
        //  static extern bool GetWindowRect(IntPtr hWnd, out Rectangle lpRect);

        [DllImport("user32.dll", EntryPoint = "GetWindowTextW", CharSet = CharSet.Unicode)]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxLength);
        ListBox lst = new ListBox();
        public MainWindow()

            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(DispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
            var d = ActiveWindowTitle();
            var t = GetForegroundWindow();


        public static IntPtr GetTopWindow(string wClass, string wCaption)
            if (string.IsNullOrEmpty(string.Concat(wClass, wCaption)))
                return (IntPtr)0;
            return (IntPtr)0;

        private static RECT ActiveRect()
            const int nChar = 256;
            RECT ss = new RECT();
            RECT dummy = new RECT();
            IntPtr handle = IntPtr.Zero;
            handle = GetForegroundWindow();

            if (GetWindowRect(handle, out ss)) return ss;
            else return dummy;
        private static List<ActiveWindow> activeWindowList = new List<ActiveWindow>();
        public static PopupWindow popup;
        private void DispatcherTimer_Tick(object sender, EventArgs e)
            if(popup !=null)
            var rec = ActiveRect();
            var title = ActiveWindowTitle();
            int height = rec.Bottom - rec.Top;
            int width = rec.Right - rec.Left;
            int top = rec.Top;

            Console.WriteLine("Active Window Title Overlay: " + title);
            Console.WriteLine("Active Window width Overlay: " + width);
            Console.WriteLine("Active Window height Overlay: " + height);
            Console.WriteLine("Active Window top Overlay: " + top);
            Console.WriteLine("Active Window left Overlay: " + rec.Left);
            Console.WriteLine("Active Window right Overlay: " + rec.Right);
            if (!String.IsNullOrEmpty(title))
                activeWindowList.Add(new ActiveWindow { WindowTitle = title, Top = rec.Top, Left = rec.Left, Width = width, Height = height });
            if(App.Current.MainWindow.Title != null)
                var current = App.Current.MainWindow.Title;
                int count = 0;
                int cur = 1;
                count = activeWindowList.Count - 1;
                for (int i = count; i >= 0; i--)
                    if (!current.Equals(activeWindowList[i].WindowTitle) && !String.IsNullOrEmpty(activeWindowList[i].WindowTitle) && !activeWindowList[i].WindowTitle.Equals("Bixby", StringComparison.OrdinalIgnoreCase))
                        cur = i;
                var activeWindow = activeWindowList[cur];
                if (!current.Equals(activeWindow.WindowTitle) && !String.IsNullOrEmpty(activeWindow.WindowTitle))

                    Console.WriteLine("Overlay Window Title Overlay: " + activeWindow.WindowTitle);
                    Console.WriteLine("Overlay Window width Overlay: " + activeWindow.Width);
                    Console.WriteLine("Overlay Window height Overlay: " + activeWindow.Height);
                    Console.WriteLine("Overlay Window left Overlay: " + activeWindow.Left);
                    Console.WriteLine("Overlay Window top Overlay: " + activeWindow.Top);

                    popup = new PopupWindow(Convert.ToInt32(activeWindow.Width), Convert.ToInt32(activeWindow.Height), 
                        Convert.ToInt32(activeWindow.Left), Convert.ToInt32(activeWindow.Top));
                    //popup.Height = height;
                    //popup.Width = width;
                    //popup.Left = rec.Left;
                    //popup.BorderThickness = new Thickness(10);
                    //popup.Top = rec.Top;
                    //popup.Topmost = true;


For overlay window PopupWindow code .cs class-
public partial class PopupWindow : Window
        public PopupWindow(double width, double height, double left, double top)

            myCanvas.Height = height;
            this.Width = width;
            this.Height = height;
            this.Top = top;
            this.Left = left;
            myCanvas.Width = width;


For overlay window PopupWindow code .xaml -

<Window x:Class="TransparentWindow.PopupWindow"
x:Name="MainWin" Title="PopupWindow" WindowStyle="None"  BorderBrush="Red" BorderThickness="10" AllowsTransparency="True" Topmost="True" ShowInTaskbar="False">
        <SolidColorBrush Opacity="0.5"/>
    <Canvas x:Name="myCanvas" Background="Transparent">
Gerry Schmitz 20-Sep-22 12:23pm    
WPF windows are not "related"; there is no concept of "overlaying windows". You can "overlay" controls by hosting them in a Grid. That's it.
Shradha Deep 21-Sep-22 1:02am    
I just created a transparent window which appears on every active top window, I am getting width and height from handle and I am trying to draw an transparent window on that active top window which is not working properly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900