0

I have to put a WPF C# application in an old winform hmi.

The wpf application must be contained in a panel. To do this, I create a new wpf application process and use the SetParent function to "cast" the wpf window into the panel. I've also included a function for the "resize" of the page.

The problem is that when I execute the SetParent function, the application positions itself correctly in the panel, but it's transparent... In other words, I can see through it, but when I click randomly in the panel, the buttons work.

The winform application is in .net framework 4.6.1 and the wpf application is in .net framekoek 4.72.

That's a part of my C# code in winform hmi :

        private Process pDocked;
        private IntPtr hWndOriginalParent;
        private IntPtr hWndParent;
        private IntPtr hWndDocked;

        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

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


        private void tabPageCalibration_Enter(object sender, EventArgs e)
        {
            if (hWndDocked != IntPtr.Zero) //don't do anything if there's already a window docked.
                return;

            tabPageCalibration.Controls.Clear();
            hWndParent = IntPtr.Zero;

            ProcessStartInfo _processStartInfo = new ProcessStartInfo
            {
                FileName = @"C:XXX.exe"
            };

            pDocked = Process.Start(_processStartInfo);

            while (hWndDocked == IntPtr.Zero)
            {
                pDocked.WaitForInputIdle(); //wait for the window to be ready for input;
                pDocked.Refresh();              //update process info
                if (pDocked.HasExited)
                {
                    return; //abort if the process finished before we got a handle.
                }
                hWndDocked = pDocked.MainWindowHandle;  //cache the window handle
            }

            //Windows API call to change the parent of the target window.
            //It returns the hWnd of the window's parent prior to this call.
            SetParent(hWndDocked, tabPageCalibration.Handle);

            //Wire up the event to keep the window sized to match the control
            tabPageCalibration.SizeChanged += new EventHandler(Pnl_Camera_Resize);

            //Perform an initial call to set the size.
            Pnl_Camera_Resize(new Object(), new EventArgs());
        }
            private void Pnl_Camera_Resize(object sender, EventArgs e)
        {
            //Change the docked windows size to match its parent's size. 
            MoveWindow(hWndDocked, 0, 0, tabPageCalibration.Width, tabPageCalibration.Height, true);
        }

I've tried several things to find and fix this problem. First, I tried another application (excel) and it works. So I deduced that it was a problem specific to my wpf application. So I did some research and added these parameters to my wpf window:

  • Opacity="1.0" AllowsTransparency="False"

Even with these parameters, it still doesn't work.

I've also checked with another wpf application which is programmed in the same version of .net framework as the winform program (4.6.1) but that doesn't work either.

I don't really know what could be the cause of my problem, which is why I need your help. Could you please tell me what might be causing my transparent display problem ?

If you need any further information to help me, I'll be happy to provide it.

thank you in advance for your help and sorry for my poor English...

Lenns
  • 1
  • 2
  • You talk about a panel. But you parent the other window to something that looks to be a TabPage according to the name. Is that the correct handle you use as parent or are there more controls in the mix here besides the parent and that "foreign" window? – Ralf Jul 04 '23 at 09:54
  • _"The winform application is in .net framework 4.6.1 and the wpf application is in .net framekoek 4.72."_ - If the horse is dead, stop riding it. – Fildor Jul 04 '23 at 09:55
  • Did you have a look here? https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/wpf-and-windows-forms-interoperation?view=netframeworkdesktop-4.8#hosting-wpf-controls-in-windows-forms – Klaus Gütter Jul 04 '23 at 10:32
  • @Ralf tabPageCalibration is a panel. In previous tests, I declared it as a tabpage and haven't yet changed its name. The tabPageCalibration panel is now inside a tabPage, which is itself inside a tabControl. – Lenns Jul 04 '23 at 11:17
  • @Fildor-standswithMods I have no choice but to use this old framework because many of my .dlls only work with it... – Lenns Jul 04 '23 at 11:17
  • @KlausGütter Yes, I've seen that, but I don't want to host just one page or element but rather the entire application so that it remains "independent" of my winform hmi solution. – Lenns Jul 04 '23 at 11:17
  • [Is it legal to have a cross-process parent/child or owner/owned window relationship?](https://devblogs.microsoft.com/oldnewthing/20130412-00/?p=4683) – Jimi Jul 04 '23 at 11:51
  • _"because many of my .dlls only work with it"_ - that's no excuse. You are in the realm of security relevant out-of-dateness. – Fildor Jul 04 '23 at 12:00

0 Answers0