Click here to Skip to main content
15,881,248 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;

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

        [StructLayout(LayoutKind.Sequential)]
        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()
        {
            InitializeComponent();

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

        }

        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)
                popup.Close();
            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;
                        break;
                    }
                }
                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.Show();
                    //popup.Topmost = true;
                    //popup.Activate();
                }
               }
}
        

        
    }


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

            
            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"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="MainWin" Title="PopupWindow" WindowStyle="None"  BorderBrush="Red" BorderThickness="10" AllowsTransparency="True" Topmost="True" ShowInTaskbar="False">
    <Window.Background>
        <SolidColorBrush Opacity="0.5"/>
    </Window.Background>
    <Canvas x:Name="myCanvas" Background="Transparent">
    </Canvas>
Posted
Comments
[no name] 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