Skip to content

Commit f87c27d

Browse files
committed
fix: Add high-DPI support
1 parent 5367f59 commit f87c27d

File tree

11 files changed

+238
-43
lines changed

11 files changed

+238
-43
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# PortProxy for Windows
22

3-
[PortProxy for Windows](https://github.com/FuweiChin/portproxy-for-windows) is a port-redirect tool. It provides WinForms GUI, and uses Windows built-in `netsh` as underlying implmentation, to manage port redirections easily and efficiently.
3+
[PortProxy for Windows](https://github.com/fuweichin/portproxy-for-windows) is a port-redirect tool. It provides WinForms GUI, and uses Windows built-in `netsh` as underlying implementation, to manage port redirections easily and efficiently.
44

5-
<!--See also our [PortProxy for Linux](https://github.com/FuweiChin/portproxy-for-linux) and [PortProxy for macOS](https://github.com/FuweiChin/portproxy-for-macos)-->
5+
![](C:\Users\fuwei\source\repos\portproxy-for-windows\screenshot.png)
66

77
## Features
8-
+ Add/delete/change/list port redirection rules, for TCP only, from v4tov4, v6tov4, v6tov6 or v4tov6
8+
+ Add/delete/change/list port redirection rules (TCP only)
99
+ Export existing rules as XML and import new rules from XML

portproxy/AboutForm.Designer.cs

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

portproxy/AboutForm.resx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120120
<data name="richTextBox1.Text" xml:space="preserve">
121-
<value>PortProxy for Windows is a port-redirect tool designed for beginners among developers/devops. It provides WinForms GUI, and uses Windows built-in netsh as underlying implmentation, to manage port redirections easily and efficiently.
122-
123-
See also our PortProxy for Linux and PortProxy for macOS.</value>
121+
<value>PortProxy for Windows is a port-redirect tool designed for beginners among developers/devops. It provides WinForms GUI, and uses Windows built-in netsh as underlying implmentation, to manage port redirections easily and efficiently.</value>
124122
</data>
125123
</root>

portproxy/MainForm.Designer.cs

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

portproxy/MainForm.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Drawing;
34
using System.Text.RegularExpressions;
5+
using System.Threading.Tasks;
46
using System.Windows.Forms;
57

68
namespace portproxy
@@ -15,6 +17,31 @@ public MainForm()
1517
{
1618
InitializeComponent();
1719
}
20+
protected override void WndProc(ref Message m)
21+
{
22+
if (m.Msg == NativeMethods.WM_SHOWME)
23+
{
24+
ShowAndActivate();
25+
}
26+
base.WndProc(ref m);
27+
}
28+
private void ShowAndActivate()
29+
{
30+
if (!this.Visible)
31+
{
32+
this.Show();
33+
}
34+
if (this.WindowState == FormWindowState.Minimized)
35+
{
36+
this.WindowState = FormWindowState.Normal;
37+
}
38+
this.Activate();
39+
this.TopMost = true;
40+
Action<Task> cancelTopMost = (Task t) => {
41+
this.TopMost = false;
42+
};
43+
Task.Delay(1000).ContinueWith(cancelTopMost);
44+
}
1845

1946
private void MainForm_Shown(object sender, EventArgs e)
2047
{
@@ -269,7 +296,7 @@ private bool IsValidPort(string text)
269296
int port = Convert.ToInt32(text);
270297
return port >= 0 && port <= 65535;
271298
}
272-
catch(FormatException e)
299+
catch (FormatException)
273300
{
274301
return false;
275302
}
@@ -347,5 +374,33 @@ private void cmbDirection_SelectedIndexChanged(object sender, EventArgs e)
347374
{
348375
errorProvider1.Clear();
349376
}
377+
378+
private void ScaleListViewColumns(ListView listView, double dpr, double oldDpr)
379+
{
380+
foreach (ColumnHeader column in listView.Columns)
381+
{
382+
column.Width = (int)Math.Round(column.Width / oldDpr * dpr);
383+
}
384+
}
385+
386+
protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
387+
{
388+
base.ScaleControl(factor, specified);
389+
double oldDpr = 1;
390+
double dpr = DeviceDpi / 96.0d;
391+
ScaleListViewColumns(listRules, dpr, oldDpr);
392+
}
393+
394+
private void MainForm_DpiChanged(object sender, DpiChangedEventArgs e)
395+
{
396+
double oldDpr = e.DeviceDpiOld / 96.0d;
397+
double dpr = e.DeviceDpiNew / 96.0d;
398+
ScaleListViewColumns(listRules, dpr, oldDpr);
399+
}
400+
401+
private void MainForm_Load(object sender, EventArgs e)
402+
{
403+
404+
}
350405
}
351406
}

portproxy/NativeMethods.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.InteropServices;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace portproxy
9+
{
10+
internal class NativeMethods
11+
{
12+
/**
13+
* Notify the main window and activate it
14+
* Copied from https://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-wpf-application#answer-522874
15+
*/
16+
[DllImport("user32.dll")]
17+
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
18+
19+
[DllImport("user32.dll")]
20+
public static extern int RegisterWindowMessage(string message);
21+
22+
public const int HWND_BROADCAST = 0xffff;
23+
public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME");
24+
}
25+
}

0 commit comments

Comments
 (0)