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

Commit 0d4f193

Browse files
committed
支持键盘映射
鼠标中键关闭页签
1 parent 6d68adf commit 0d4f193

File tree

4 files changed

+109
-39
lines changed

4 files changed

+109
-39
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ MSTSC 还有一种说法,Microsoft Telnet Screen Control ,即“微软远程
4848
* 右击页面头会有菜单选项,比如可执行 `关闭` 操作,右击连接方块可进行 `编辑``删除` 等操作
4949
* 切换主题,偶尔换换口味吧
5050
* 编辑时可以点击右上角的 `复制` 则保留该连接信息,点保存时换创建一个新的连接
51+
* 鼠标中键关闭页签 (远程桌面)
52+
* 键盘映射,当窗口处于活动状态远程桌面将获得键盘事件,当窗口不处于活动状态则本机获得键盘事件,建议点击远程窗口和本机任务栏来进行键盘映射的切换
5153

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

RdpTest/MainForm.Designer.cs

Lines changed: 15 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RdpTest/MainForm.cs

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ public MainForm()
3131
private void MainForm_Load(object sender, EventArgs e)
3232
{
3333
//异步加载远程桌面配置
34-
BeginInvoke(new Action(LoadHostConfig));
34+
BeginInvoke(new Action(LoadHosts));
3535
}
3636

37-
private void LoadHostConfig()
37+
/// <summary>
38+
/// 加载所以远程桌面配置
39+
/// </summary>
40+
private void LoadHosts()
3841
{
3942
panelBody.Visible = false;
4043
panelBody.Controls.Clear(); //清空原有
@@ -73,7 +76,10 @@ private void LoadHostConfig()
7376
panelBody.Visible = true;
7477
}
7578

76-
void ConnectRemoteHost(object sender, EventArgs e)
79+
/// <summary>
80+
/// 连接远程桌面
81+
/// </summary>
82+
private void ConnectRemoteHost(object sender, EventArgs e)
7783
{
7884
var host = (RemoteHost)((MetroTile)sender).Tag;
7985

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

103-
//进运行远程程序模式
104-
if (!string.IsNullOrEmpty(host.RemoteProgram))
109+
if (string.IsNullOrEmpty(host.RemoteProgram)) //普通远程桌面模式
110+
{
111+
//映射键盘
112+
rdpClient.SecuredSettings3.KeyboardHookMode = 1;
113+
}
114+
else //运行远程程序模式
105115
{
106116
rdpClient.RemoteProgram2.RemoteProgramMode = true;
107117
rdpClient.Width = Screen.PrimaryScreen.Bounds.Width;
@@ -122,13 +132,6 @@ void ConnectRemoteHost(object sender, EventArgs e)
122132
};
123133
}
124134

125-
//rdpClient.RemoteProgram2.RemoteProgramMode = true;
126-
//rdpClient.OnLoginComplete += (o, args) =>
127-
//{
128-
// rdpClient.RemoteProgram2.ServerStartProgram("cmd", "", "%SYSTEMROOT%", false, "", false);
129-
// tabMain.TabPages.Remove(page);
130-
//};
131-
132135
/* 因为分辨率比例问题,缩放效果并不怎么样
133136
rdpClient.Width = Screen.PrimaryScreen.Bounds.Width;
134137
rdpClient.Height = Screen.PrimaryScreen.Bounds.Height;
@@ -146,7 +149,17 @@ void ConnectRemoteHost(object sender, EventArgs e)
146149
rdpClient.Connect();
147150
}
148151

152+
private AxMsRdpClient9NotSafeForScripting GetHost(int pageIndex)
153+
{
154+
var page = tabMain.TabPages[pageIndex];
155+
return (AxMsRdpClient9NotSafeForScripting)page.Controls[0];
156+
}
157+
158+
149159
#region 菜单按钮
160+
/// <summary>
161+
/// 切换主题
162+
/// </summary>
150163
private void btnChangeStyle_Click(object sender, EventArgs e)
151164
{
152165
var nextStyle = ((int)metroStyleManager.Style + 1) % 14;
@@ -159,35 +172,44 @@ private void btnChangeStyle_Click(object sender, EventArgs e)
159172
Refresh();
160173
}
161174

175+
/// <summary>
176+
/// 刷新
177+
/// </summary>
162178
private void btnRefresh_Click(object sender, EventArgs e)
163179
{
164-
LoadHostConfig();
180+
LoadHosts();
165181
}
166182

183+
/// <summary>
184+
/// 添加主机
185+
/// </summary>
167186
private void btnAddRemoteHost_Click(object sender, EventArgs e)
168187
{
169188
var hostForm = new RemoteHostForm { StyleManager = metroStyleManager };
170189
if (hostForm.ShowDialog() == DialogResult.OK)
171-
LoadHostConfig();
190+
LoadHosts();
172191
}
173192

193+
/// <summary>
194+
/// 关于
195+
/// </summary>
174196
private void btnAbout_Click(object sender, EventArgs e)
175197
{
176198
Process.Start("https://github.com/wang9563/RemoteDesktopManage");
177199
}
178200
#endregion
179201

180-
#region 页签控制
202+
#region 页签控制 [拖动/关闭]
181203
private void tabMain_MouseDown(object sender, MouseEventArgs e)
182204
{
183205
var index = InTabPageHead(e.Location, false);
184206
if (index > 0)
185207
{
186208
tabMain.SelectedIndex = index;
187209

188-
if (e.Button == MouseButtons.Right)
189-
tabMain.ContextMenuStrip = menuTabPage; //弹出菜单
190-
else
210+
if (e.Button == MouseButtons.Right) //右击,弹出菜单
211+
tabMain.ContextMenuStrip = menuTabPage;
212+
else if (e.Button == MouseButtons.Left) //左击,进行拖动判定
191213
{
192214
_tabMoving = true;
193215
_tabBeforeMoveX = e.X;
@@ -254,8 +276,18 @@ private int InTabPageHead(Point location, bool zoomRange)
254276

255277
private void tabMain_MouseUp(object sender, MouseEventArgs e)
256278
{
257-
_tabMoving = false;
258-
Cursor.Current = Cursors.Default;
279+
if (e.Button == MouseButtons.Left) //完成拖动
280+
{
281+
_tabMoving = false;
282+
Cursor.Current = Cursors.Default;
283+
}
284+
else if (e.Button == MouseButtons.Middle) //关闭页签
285+
{
286+
var index = InTabPageHead(e.Location, true);
287+
if (index <= 0) return;
288+
289+
CloseHost(index);
290+
}
259291
}
260292

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

266298
private void tmiCloseHost_Click(object sender, EventArgs e)
267299
{
268-
var next = tabMain.SelectedIndex - 1;
269-
var host = (AxMsRdpClient9NotSafeForScripting)tabMain.SelectedTab.Controls[0];
300+
CloseHost(tabMain.SelectedIndex);
301+
}
302+
303+
private void CloseHost(int pageIndex)
304+
{
305+
var page = tabMain.TabPages[pageIndex];
306+
var host = GetHost(pageIndex);
270307

271308
if (host.Connected == 1)
272309
host.Disconnect();
273310
host.Dispose();
274-
tabMain.SelectedTab.Dispose();
311+
tabMain.TabPages.Remove(page);
275312

276-
tabMain.SelectedIndex = next;
313+
tabMain.SelectedIndex = pageIndex - (pageIndex == tabMain.TabPages.Count ? 1 : 0);
277314
}
278315
#endregion
279316

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

298335
if (hostForm.ShowDialog() == DialogResult.OK)
299-
LoadHostConfig();
336+
LoadHosts();
300337
}
301338
private void tmiDeleteHost_Click(object sender, EventArgs e)
302339
{
@@ -324,5 +361,32 @@ private void tmiDeleteAll_Click(object sender, EventArgs e)
324361

325362
#endregion
326363

364+
#region 界面切换与唤醒 [切换映射键盘]
365+
private int _keyboardHookModeChanged;
366+
private void MainForm_Activated(object sender, EventArgs e)
367+
{
368+
if (_keyboardHookModeChanged == 1) return;
369+
_keyboardHookModeChanged = 1;
370+
if (tabMain.SelectedIndex <= 0) return;
371+
372+
var host = GetHost(tabMain.SelectedIndex);
373+
if (host.RemoteProgram2.RemoteProgramMode) return;
374+
375+
host.SecuredSettings3.KeyboardHookMode = 1; //在远程服务器上应用组合键。
376+
}
377+
private void MainForm_Deactivate(object sender, EventArgs e)
378+
{
379+
if (_keyboardHookModeChanged == 2) return;
380+
_keyboardHookModeChanged = 2;
381+
if (tabMain.SelectedIndex <= 0) return;
382+
383+
var host = GetHost(tabMain.SelectedIndex);
384+
if (host.RemoteProgram2.RemoteProgramMode) return;
385+
386+
host.SecuredSettings3.KeyboardHookMode = 2; //仅当客户端以全屏模式运行时才将组合键应用于远程服务器。这是默认值。
387+
}
388+
#endregion
389+
390+
327391
}
328392
}

RdpTest/RemoteHostForm.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ private void LoadRemoteHost()
6767
private void lbCopy_Click(object sender, EventArgs e)
6868
{
6969
RemoteHost = null;
70-
lbCopy.Visible = false;
70+
lbCopy.Visible = false;
71+
lbStatus.Text = "[新增]";
72+
chIsParent.Enabled = true;
7173
}
7274

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

0 commit comments

Comments
 (0)