Skip to content
This repository has been archived by the owner on Jun 23, 2021. It is now read-only.

Commit

Permalink
支持键盘映射
Browse files Browse the repository at this point in the history
鼠标中键关闭页签
  • Loading branch information
xunki committed Jun 28, 2018
1 parent 6d68adf commit 0d4f193
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 39 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ MSTSC 还有一种说法,Microsoft Telnet Screen Control ,即“微软远程
* 右击页面头会有菜单选项,比如可执行 `关闭` 操作,右击连接方块可进行 `编辑``删除` 等操作
* 切换主题,偶尔换换口味吧
* 编辑时可以点击右上角的 `复制` 则保留该连接信息,点保存时换创建一个新的连接
* 鼠标中键关闭页签 (远程桌面)
* 键盘映射,当窗口处于活动状态远程桌面将获得键盘事件,当窗口不处于活动状态则本机获得键盘事件,建议点击远程窗口和本机任务栏来进行键盘映射的切换

以上功能均是出于我个人使用习惯,可能对您并不实用,所以文档也含糊一下啦,抱歉!

Expand Down
28 changes: 15 additions & 13 deletions RdpTest/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 89 additions & 25 deletions RdpTest/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ public MainForm()
private void MainForm_Load(object sender, EventArgs e)
{
//异步加载远程桌面配置
BeginInvoke(new Action(LoadHostConfig));
BeginInvoke(new Action(LoadHosts));
}

private void LoadHostConfig()
/// <summary>
/// 加载所以远程桌面配置
/// </summary>
private void LoadHosts()
{
panelBody.Visible = false;
panelBody.Controls.Clear(); //清空原有
Expand Down Expand Up @@ -73,7 +76,10 @@ private void LoadHostConfig()
panelBody.Visible = true;
}

void ConnectRemoteHost(object sender, EventArgs e)
/// <summary>
/// 连接远程桌面
/// </summary>
private void ConnectRemoteHost(object sender, EventArgs e)
{
var host = (RemoteHost)((MetroTile)sender).Tag;

Expand All @@ -100,8 +106,12 @@ void ConnectRemoteHost(object sender, EventArgs e)
rdpClient.UserName = host.User;
rdpClient.AdvancedSettings2.ClearTextPassword = host.Pwd;

//进运行远程程序模式
if (!string.IsNullOrEmpty(host.RemoteProgram))
if (string.IsNullOrEmpty(host.RemoteProgram)) //普通远程桌面模式
{
//映射键盘
rdpClient.SecuredSettings3.KeyboardHookMode = 1;
}
else //运行远程程序模式
{
rdpClient.RemoteProgram2.RemoteProgramMode = true;
rdpClient.Width = Screen.PrimaryScreen.Bounds.Width;
Expand All @@ -122,13 +132,6 @@ void ConnectRemoteHost(object sender, EventArgs e)
};
}

//rdpClient.RemoteProgram2.RemoteProgramMode = true;
//rdpClient.OnLoginComplete += (o, args) =>
//{
// rdpClient.RemoteProgram2.ServerStartProgram("cmd", "", "%SYSTEMROOT%", false, "", false);
// tabMain.TabPages.Remove(page);
//};

/* 因为分辨率比例问题,缩放效果并不怎么样
rdpClient.Width = Screen.PrimaryScreen.Bounds.Width;
rdpClient.Height = Screen.PrimaryScreen.Bounds.Height;
Expand All @@ -146,7 +149,17 @@ void ConnectRemoteHost(object sender, EventArgs e)
rdpClient.Connect();
}

private AxMsRdpClient9NotSafeForScripting GetHost(int pageIndex)
{
var page = tabMain.TabPages[pageIndex];
return (AxMsRdpClient9NotSafeForScripting)page.Controls[0];
}


#region 菜单按钮
/// <summary>
/// 切换主题
/// </summary>
private void btnChangeStyle_Click(object sender, EventArgs e)
{
var nextStyle = ((int)metroStyleManager.Style + 1) % 14;
Expand All @@ -159,35 +172,44 @@ private void btnChangeStyle_Click(object sender, EventArgs e)
Refresh();
}

/// <summary>
/// 刷新
/// </summary>
private void btnRefresh_Click(object sender, EventArgs e)
{
LoadHostConfig();
LoadHosts();
}

/// <summary>
/// 添加主机
/// </summary>
private void btnAddRemoteHost_Click(object sender, EventArgs e)
{
var hostForm = new RemoteHostForm { StyleManager = metroStyleManager };
if (hostForm.ShowDialog() == DialogResult.OK)
LoadHostConfig();
LoadHosts();
}

/// <summary>
/// 关于
/// </summary>
private void btnAbout_Click(object sender, EventArgs e)
{
Process.Start("https://github.com/wang9563/RemoteDesktopManage");
}
#endregion

#region 页签控制
#region 页签控制 [拖动/关闭]
private void tabMain_MouseDown(object sender, MouseEventArgs e)
{
var index = InTabPageHead(e.Location, false);
if (index > 0)
{
tabMain.SelectedIndex = index;

if (e.Button == MouseButtons.Right)
tabMain.ContextMenuStrip = menuTabPage; //弹出菜单
else
if (e.Button == MouseButtons.Right) //右击,弹出菜单
tabMain.ContextMenuStrip = menuTabPage;
else if (e.Button == MouseButtons.Left) //左击,进行拖动判定
{
_tabMoving = true;
_tabBeforeMoveX = e.X;
Expand Down Expand Up @@ -254,8 +276,18 @@ private int InTabPageHead(Point location, bool zoomRange)

private void tabMain_MouseUp(object sender, MouseEventArgs e)
{
_tabMoving = false;
Cursor.Current = Cursors.Default;
if (e.Button == MouseButtons.Left) //完成拖动
{
_tabMoving = false;
Cursor.Current = Cursors.Default;
}
else if (e.Button == MouseButtons.Middle) //关闭页签
{
var index = InTabPageHead(e.Location, true);
if (index <= 0) return;

CloseHost(index);
}
}

private void MainForm_MouseLeave(object sender, EventArgs e)
Expand All @@ -265,15 +297,20 @@ private void MainForm_MouseLeave(object sender, EventArgs e)

private void tmiCloseHost_Click(object sender, EventArgs e)
{
var next = tabMain.SelectedIndex - 1;
var host = (AxMsRdpClient9NotSafeForScripting)tabMain.SelectedTab.Controls[0];
CloseHost(tabMain.SelectedIndex);
}

private void CloseHost(int pageIndex)
{
var page = tabMain.TabPages[pageIndex];
var host = GetHost(pageIndex);

if (host.Connected == 1)
host.Disconnect();
host.Dispose();
tabMain.SelectedTab.Dispose();
tabMain.TabPages.Remove(page);

tabMain.SelectedIndex = next;
tabMain.SelectedIndex = pageIndex - (pageIndex == tabMain.TabPages.Count ? 1 : 0);
}
#endregion

Expand All @@ -296,7 +333,7 @@ private void tmiEdit_Click(object sender, EventArgs e)
var hostForm = new RemoteHostForm { StyleManager = metroStyleManager, RemoteHost = host };

if (hostForm.ShowDialog() == DialogResult.OK)
LoadHostConfig();
LoadHosts();
}
private void tmiDeleteHost_Click(object sender, EventArgs e)
{
Expand Down Expand Up @@ -324,5 +361,32 @@ private void tmiDeleteAll_Click(object sender, EventArgs e)

#endregion

#region 界面切换与唤醒 [切换映射键盘]
private int _keyboardHookModeChanged;
private void MainForm_Activated(object sender, EventArgs e)
{
if (_keyboardHookModeChanged == 1) return;
_keyboardHookModeChanged = 1;
if (tabMain.SelectedIndex <= 0) return;

var host = GetHost(tabMain.SelectedIndex);
if (host.RemoteProgram2.RemoteProgramMode) return;

host.SecuredSettings3.KeyboardHookMode = 1; //在远程服务器上应用组合键。
}
private void MainForm_Deactivate(object sender, EventArgs e)
{
if (_keyboardHookModeChanged == 2) return;
_keyboardHookModeChanged = 2;
if (tabMain.SelectedIndex <= 0) return;

var host = GetHost(tabMain.SelectedIndex);
if (host.RemoteProgram2.RemoteProgramMode) return;

host.SecuredSettings3.KeyboardHookMode = 2; //仅当客户端以全屏模式运行时才将组合键应用于远程服务器。这是默认值。
}
#endregion


}
}
4 changes: 3 additions & 1 deletion RdpTest/RemoteHostForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ private void LoadRemoteHost()
private void lbCopy_Click(object sender, EventArgs e)
{
RemoteHost = null;
lbCopy.Visible = false;
lbCopy.Visible = false;
lbStatus.Text = "[新增]";
chIsParent.Enabled = true;
}

private void cbIsParent_CheckedChanged(object sender, System.EventArgs e)
Expand Down

0 comments on commit 0d4f193

Please sign in to comment.