-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
164 lines (147 loc) · 5.62 KB
/
Program.cs
File metadata and controls
164 lines (147 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace EthminerGUI
{
static class Program
{
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("gdi32.dll")]
static extern int AddFontResource(string Arg1);
public static IntPtr HWndConsole { get; set; }
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
#if !DEBUG && !WITHOUTWT
if (args.Length > 0 && args[0] == "start")
{
#region TCP监听hWnd指针
var ip = IPAddress.Loopback;
var endPoint = new IPEndPoint(ip, 8888);
var listener = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(endPoint);
listener.Listen(1);
while (true)
{
var handler = listener.Accept();
var buffer = new byte[1024];
var data = new byte[16];
var pos = 0;
while (true)
{
int bytesRec = handler.Receive(buffer);
Buffer.BlockCopy(buffer, 0, data, pos, bytesRec);
pos += bytesRec;
if (pos == 16) break;
}
handler.Close();
if (Encoding.ASCII.GetString(data, 0, 12) == "ethminer-gui")
{
HWndConsole = (IntPtr)BitConverter.ToInt32(data, 12);
break;
}
}
listener.Close();
#endregion
}
else
{
var hWnd = GetConsoleWindow();
ShowWindow(hWnd, 0);
}
if (HWndConsole == IntPtr.Zero)
{
#region 注册字体
var fontPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Microsoft", "Windows", "Fonts");
var cascadiaPath = Path.Combine(fontPath, "Cascadia.ttf");
var CascadiaMonoPath = Path.Combine(fontPath, "CascadiaMono.ttf");
if (!File.Exists(cascadiaPath))
{
Directory.CreateDirectory(fontPath);
File.Copy(Path.Combine("wt", "Cascadia.ttf"), cascadiaPath);
AddFontResource(cascadiaPath);
}
if (!File.Exists(CascadiaMonoPath))
{
Directory.CreateDirectory(fontPath);
File.Copy(Path.Combine("wt", "CascadiaMono.ttf"), CascadiaMonoPath);
AddFontResource(CascadiaMonoPath);
}
var key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts");
if (key.GetValue("Cascadia Code Regular (TrueType)") == null)
{
key.SetValue("Cascadia Code Regular (TrueType)", cascadiaPath);
}
if (key.GetValue("Cascadia Mono Regular (TrueType)") == null)
{
key.SetValue("Cascadia Mono Regular (TrueType)", CascadiaMonoPath);
}
#endregion
#region 用wt启动自己(套娃
var process = new Process();
process.StartInfo.FileName = @"wt\WindowsTerminal.exe";
process.StartInfo.Arguments = "-f -d . ethminer-gui.exe start";
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
process.Start();
int readCount = 0;
string title = null;
while (true)
{
Thread.Sleep(10);
process.Refresh();
if (process.MainWindowHandle != HWndConsole && readCount < 2)
{
readCount++;
HWndConsole = process.MainWindowHandle;
title = process.MainWindowTitle;
}
if (readCount == 2)
{
if (process.MainWindowTitle != title)
{
ShowWindow(HWndConsole, 9);
break;
}
}
}
#endregion
#region TCP发送hWnd指针
var ip = IPAddress.Loopback;
var endPoint = new IPEndPoint(ip, 8888);
Socket sender = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
sender.Connect(endPoint);
sender.Send(Encoding.ASCII.GetBytes("ethminer-gui"));
sender.Send(BitConverter.GetBytes((int)HWndConsole));
sender.Close();
#endregion
}
else
{
#else
HWndConsole = GetConsoleWindow();
#endif
// Winform主入口
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new App());
#if !DEBUG && !WITHOUTWT
}
#endif
}
}
}