将WPF应用程序链接到Windows窗体应用程序的代码

问题描述:

他们是通过Windows窗体应用程序中的按钮将WPF应用程序链接到Windows窗体应用程序的任何代码吗?

is their any code to link a WPF application to Windows form application by a button in Windows form app?


嗨欧山,


Hi  Aushan,

>>是通过Windows窗体应用程序中的按钮将WPF应用程序链接到Windows窗体应用程序的任何代码吗?

以下代码供您参考.您可以尝试调整代码以适合您的需求.

The following code for your reference. You can try adjust the code to suit your needs.

  public partial class MainForm : Form
    {
        Process _process;
        bool isFirst = false;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
           
        }


        private void WaitForInputIdle()
        {
            while (_process.MainWindowHandle == IntPtr.Zero)
            {
                _process.Refresh();
                _process.MainWindowHandle.ToInt32();
            }
        }

      
        private void EmbedProcess(Process process)
        {
            if (process != null && !(process.MainWindowHandle == IntPtr.Zero))
            {
                try
                {
                    // Put it into this form
                    AppContainer.SetParent(process.MainWindowHandle, panel1.Handle);
                    // Remove border and whatnot               
                    AppContainer.SetWindowLong(new HandleRef(this, process.MainWindowHandle));
                    // Move the window to overlay it on this window
                    AppContainer.MoveWindow(_process, panel1);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (isFirst)
                AppContainer.MoveWindow(_process, this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process[] processs = Process.GetProcessesByName("TestTempWPF.exe");
            foreach (var item in processs)
            {
                item.Kill();
            }

            string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestTempWPF.exe");

            if (File.Exists(fileName))
            {
                ProcessStartInfo startInfo = new ProcessStartInfo(fileName);
                startInfo.Arguments = "UploadFile," + this.Handle;
                startInfo.UseShellExecute = true;
                startInfo.CreateNoWindow = true;
                startInfo.WorkingDirectory = "";
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;

                _process = Process.Start(startInfo);
                _process.EnableRaisingEvents = true;
                WaitForInputIdle();
                EmbedProcess(_process);
            }
            else
                MessageBox.Show("error");
            isFirst = true;
        }
    }


 class AppContainer
    {
 
        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
             CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
        private static extern long GetWindowLong(IntPtr hwnd, int nIndex);
        [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
        private static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
        internal static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
        [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
        private static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr GetParent(IntPtr hwnd);
        [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        private const int SWP_NOOWNERZORDER = 0x200;
        private const int SWP_NOREDRAW = 0x8;
        private const int SWP_NOZORDER = 0x4;
        private const int SWP_SHOWWINDOW = 0x0040;
        private const int WS_EX_MDICHILD = 0x40;
        private const int SWP_FRAMECHANGED = 0x20;
        private const int SWP_NOACTIVATE = 0x10;
        private const int SWP_ASYNCWINDOWPOS = 0x4000;
        private const int SWP_NOMOVE = 0x2;
        private const int SWP_NOSIZE = 0x1;
        private const int GWL_STYLE = (-16);
        private const int WS_VISIBLE = 0x10000000;
        private const int WM_CLOSE = 0x10;
        private const int WS_CHILD = 0x40000000;

        private const int SW_HIDE = 0; 
        private const int SW_SHOWNORMAL = 1; 
        private const int SW_NORMAL = 1; 
        private const int SW_SHOWMINIMIZED = 2; 
        private const int SW_SHOWMAXIMIZED = 3;
        private const int SW_MAXIMIZE = 3; 
        private const int SW_SHOWNOACTIVATE = 4; 
        private const int SW_SHOW = 5; 
        private const int SW_MINIMIZE = 6; 
        private const int SW_SHOWMINNOACTIVE = 7; 
        private const int SW_SHOWNA = 8; 
        private const int SW_RESTORE = 9; 
        private const int SW_SHOWDEFAULT = 10;
        private const int SW_MAX = 10;
        private const int HWND_TOP = 0x0;
        private const int WM_COMMAND = 0x0112;
        private const int WM_QT_PAINT = 0xC2DC;
        private const int WM_PAINT = 0x0001;
        private const int WM_SIZE = 0x0001;

        [SecuritySafeCritical]
        internal static void MoveWindow(Process app, Control control)
        {
            if (app != null)
            {
                MoveWindow(app.MainWindowHandle, 0, 0, control.Width, control.Height, true);
            }
        }

        [SecuritySafeCritical]
        internal static void SetWindowLong(HandleRef handleRef)
        {
            SetWindowLong(handleRef, GWL_STYLE, WS_VISIBLE);
        }

        [SecuritySafeCritical]
        internal static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
        {
            if (IntPtr.Size == 4)
            {
                return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
            }
            return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
        }

    }




下面的文章介绍如何将WPF控件嵌入WinForms应用程序中.您可以参考.


The following article introduce how to embed WPF controls in a WinForms application. You can refer it.

混合WPF和WinForms:
https://www.simple-talk.com/dotnet/net-framework /mixing-wpf-and-winforms/

Mixing WPF and WinForms:
https://www.simple-talk.com/dotnet/net-framework/mixing-wpf-and-winforms/


如果您对Windows窗体有任何疑问,可以访问 Windows窗体获得适当帮助的常规论坛.


If you have any question about Windows Forms, you can visit the Windows Forms General forum for getting suitable help.



最好的问候,


Best Regards,

吕汉楠