Wednesday, April 11, 2007

Transparent WPF control over unmanaged window handle

All those, how even once tried to put opaque or transparent WPF control over Windows Forms control or, even unmanaged window found themselves within "Airspace problem". So, what to do? How can we use transparent WPF UI over our legacy windows? This how.

See the image above. As you can see, there is tarsparent XAML page (corners, plus and text) over legacy game window. How to do it? We'll put our transparent WPF window over Windows Forms window. First of all, we have to find the handle of our WPF application. We have only one for whole application, so let's get it

 

protected override void OnSourceInitialized(EventArgs e)
        {
            HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
 
            if (hwndSource != null)
            {
                handle = hwndSource.Handle;
                WindowFinder.Start(title, handle);
                WindowFinder.OnWindowFound += new WindowFinder.OnWindowFoundHandler(WindowFinder_OnWindowFound);                
            }
        }

So far, so good. Now we have to find the handle of our legacy window. In order to do it, we have to put ourselves into user32.dll and dig FindWindow method from there.


 



[DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

Now let's enter endless loop for finding our handle


 



IntPtr hWnd = FindWindow(null, (string)e.Argument);
            while (!w.CancellationPending && hWnd == m_ptr)
            {
                System.Threading.Thread.Sleep(100);
                hWnd = FindWindow(null, (string)e.Argument);
            }

Once got it by name, we have to measure it and then resize our WPF window to fit the new size and position. To do it, we'll use another method GetWindowRect


 



[DllImport("user32.dll")]
        static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
 

RECT r = new RECT();
            while (!w.CancellationPending && ((r.Left == r.Right) & (r.Bottom == r.Top)))
            {
                System.Threading.Thread.Sleep(100);
                GetWindowRect(m_ptr, out r);
            }
 
 
            m_rect = new Rect(r.Left,r.Top,r.Right-r.Left,r.Bottom-r.Top);

Got it and it's different then 0. Now let's resize the WPF window and put it over the legacy one and run from the beginning to track any change. Sure, the better way is to build global hook to legacy window messages and adjust properties of our WPF window. Do it. I do not really need it.


 



void WindowFinder_OnWindowFound(IntPtr handle, Rect position)
        {
            this.Left = position.Left;
            this.Top = position.Top;
            this.Width = position.Width;
            this.Height = position.Height;
            WindowFinder.Start(title, handle);
        }

We finished. Now we'll always have WPF window over legacy window, or control, or anything, that have name or handle. Even Windows Media Player window



A couple of things in the code attached not covered by article:



  • Half and MultipleNumber converters - those converters will help up to bind corners of WPF window into black Polylines, that you can see in the image. Those are really simple



    class HalfNumberConverter:IValueConverter
        {
            #region IValueConverter Members
     
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                double half = 0;
                if (double.TryParse(value.ToString(), out half))
                    half /= 2;
                double offset = 0;
                if(double.TryParse(parameter.ToString(), out offset))
                    half += offset;
     
                return half;
            }

    class MultipleNumberConverter:IValueConverter
        {
            #region IValueConverter Members
     
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                double val = 0;
                if (double.TryParse(value.ToString(), out val))
                {
                    char op = parameter.ToString()[0];
                    if (op == '*')
                        val *= double.Parse(parameter.ToString().Remove(0, 1));
                    else if (op == '/')
                        val /= double.Parse(parameter.ToString().Remove(0, 1));
                    if (op == '+')
                        val += double.Parse(parameter.ToString().Remove(0, 1));
                    else if (op == '-')
                        val -= double.Parse(parameter.ToString().Remove(0, 1));
                    else
                        val += double.Parse(parameter.ToString());
                }
                return val;
            }

     

  • StrokeTextBlock - this is hi-contrast textblock, that visible on any background. I wrote about how to build it earlier. Very useful control and any, especially this application

  • In case that you do not know - in order to move borderless transparent window you have to overwrite any OnMouse.... event and DragMove() the control there. The example of usage is OnMouseLeftButtonDown one.



    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                base.OnMouseLeftButtonDown(e);
                DragMove();
     
            }

  • Do you remember, that there is no hittest within transparent window, so you can use underlying window through our WPF form

  • To do you work, use BackgroundWorker - this save you actions related to thread context stuff.

I think, that most of things were covered by this, so see the source in attachment and when using it, don't forget to remember me.


Source code for this article

No comments: