none
WPF中嵌入WinForm做的远程桌面的焦点问题 RRS feed

  • 问题

  • 我原来做了一个基于WinForm的远程桌面,现在想把它嵌入到WPF中。

    现在碰到一个问题,我可以打开远程桌面,且可以操作,但是,当切到另外一个程序后在回到这个远程桌面时,就失去焦点,无法操作了,必须按一下Tab键才可以使用。请问如何操作可以使得鼠标进入远程桌面时就自动获得焦点?

    PS:在第一失去焦点,按了Tab后,再怎么切入切出都可以获得焦点,why?

    远程桌面的cs文件(Form)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using AxMSTSCLib; using System.Threading; namespace RemoteDesktop { public partial class RDP : Form { public AutoResetEvent windowsEvent = new AutoResetEvent(false); private AxMSTSCLib.AxMsRdpClient2 rdpClient; public RDP(string ip,string usrname, string passwd) { InitializeComponent(); this.Text = ip + " by " + usrname; this.Hide(); this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; try { rdpClient = new AxMSTSCLib.AxMsRdpClient2(); rdpClient.Dock = DockStyle.Fill; rdpClient.Width = Screen.PrimaryScreen.Bounds.Width; rdpClient.Height = Screen.PrimaryScreen.Bounds.Height; rdpClient.BeginInit(); this.Controls.Add(rdpClient); rdpClient.EndInit(); rdpClient.OnDisconnected += new AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEventHandler(rdpClient_OnDisconnected); rdpClient.OnConfirmClose += new AxMSTSCLib.IMsTscAxEvents_OnConfirmCloseEventHandler(rdpClient_OnConfirmClose); rdpClient.Server = ip; rdpClient.UserName = usrname; rdpClient.AdvancedSettings2.RDPPort = 3389; rdpClient.AdvancedSettings2.ClearTextPassword = passwd; rdpClient.ColorDepth = 16; rdpClient.FullScreen = false ; rdpClient.Connect(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } void rdpClient_OnConfirmClose(object sender, IMsTscAxEvents_OnConfirmCloseEvent e) { this.Close(); } void rdpClient_OnDisconnected(object sender, AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEvent e) { MessageBox.Show("发生错误需要关闭,错误代码:" + e.discReason); this.Close(); } } }

    wpf的框

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using RemoteDesktop;
    
    namespace remote_desktop_wfp
    {
        /// <summary>
        /// Window2.xaml 的交互逻辑
        /// </summary>
        public partial class Window2 : Window
        {
            public Window2()
            {
                InitializeComponent();
                ShowRemoteDesktop(new RemoteDesktopArgs("172.16.60.228", "123456", "Administrator"));
            }
    
            public class RemoteDesktopArgs
            {
                public string ip { get; set; }
                public string password { get; set; }
                public string user { get; set; }
                public RemoteDesktopArgs(string i, string p, string u) { ip = i; password = p; user = u; }
            }
    
            void ShowRemoteDesktop(RemoteDesktopArgs rdpargs)
            {
    
                System.Windows.Forms.Integration.WindowsFormsHost host =
                new System.Windows.Forms.Integration.WindowsFormsHost();
    
                RDP rdp = new RDP(rdpargs.ip, rdpargs.user, rdpargs.password);
                rdp.TopLevel = false;
    
                host.Child = rdp;
                host.MouseEnter += new MouseEventHandler(host_MouseEnter);
                host.MouseMove += new MouseEventHandler(host_MouseMove);
                host.Focusable = false;
    
                grid.Children.Add(host);
    
            }
    
            void host_MouseMove(object sender, MouseEventArgs e)
            {
                RDP rdp = (sender as System.Windows.Forms.Integration.WindowsFormsHost).Child as RDP;
                rdp.Focus();
            }
    
            void host_MouseEnter(object sender, MouseEventArgs e)
            {
                RDP rdp = (sender as System.Windows.Forms.Integration.WindowsFormsHost).Child as RDP;
                rdp.Focus();
    
            }
        }
    }
    

    2012年8月29日 6:23