@@ -20,6 +20,8 @@ internal enum MagWindowStatus : int {
2020
2121 // 用于管理全屏窗口,该窗口在一个新线程中启动,通过事件与主线程通信
2222 internal class MagWindow {
23+ private static NLog . Logger Logger { get ; } = NLog . LogManager . GetCurrentClassLogger ( ) ;
24+
2325 public event Action Closed ;
2426
2527 public IntPtr SrcWindow { get ; private set ; } = IntPtr . Zero ;
@@ -72,11 +74,13 @@ public MagWindow(Window parent) {
7274 public void Create (
7375 string scaleModel ,
7476 int captureMode ,
77+ int bufferPrecision ,
7578 bool showFPS ,
7679 bool hookCursorAtRuntime ,
7780 bool noDisturb = false
7881 ) {
7982 if ( Status != MagWindowStatus . Idle ) {
83+ Logger . Info ( "已存在全屏窗口,取消进入全屏" ) ;
8084 return ;
8185 }
8286
@@ -85,19 +89,22 @@ public void Create(
8589 || ! NativeMethods . IsWindowVisible ( hwndSrc )
8690 || NativeMethods . GetWindowShowCmd ( hwndSrc ) != NativeMethods . SW_NORMAL
8791 ) {
88- // 不合法的源窗口
92+ Logger . Info ( "源窗口不合法" ) ;
8993 return ;
9094 }
9195
9296 Status = MagWindowStatus . Starting ;
9397 // 使用 WinRT Capturer API 需要在 MTA 中
9498 // C# 窗体必须使用 STA,因此将全屏窗口创建在新的线程里
9599 magThread = new Thread ( ( ) => {
100+ Logger . Info ( "正在新线程中创建全屏窗口" ) ;
101+
96102 NativeMethods . RunMagWindow (
97103 ( int status , IntPtr errorMsg ) => StatusEvent ( status , Marshal . PtrToStringUni ( errorMsg ) ) ,
98104 hwndSrc , // 源窗口句柄
99105 scaleModel , // 缩放模式
100106 captureMode , // 抓取模式
107+ bufferPrecision , // 缓冲区精度
101108 showFPS , // 显示 FPS
102109 noDisturb // 用于调试
103110 ) ;
@@ -123,9 +130,11 @@ public void Destory() {
123130 }
124131
125132 private void HookCursorAtRuntime ( IntPtr hwndSrc ) {
133+ Logger . Info ( "正在进行运行时注入" ) ;
134+
126135 int pid = NativeMethods . GetWindowProcessId ( hwndSrc ) ;
127136 if ( pid == 0 || pid == Process . GetCurrentProcess ( ) . Id ) {
128- // 不能 hook 本进程
137+ Logger . Info ( "不能注入本进程,已取消" ) ;
129138 return ;
130139 }
131140
@@ -146,19 +155,21 @@ private void HookCursorAtRuntime(IntPtr hwndSrc) {
146155 // 使用 EasyHook 注入
147156 try {
148157 RemoteHooking . Inject (
149- pid , // 要注入的进程的 ID
150- injectionLibrary , // 32 位 DLL
151- injectionLibrary , // 64 位 DLL
152- // 下面为传递给注入 DLL 的参数
153- channelName ,
154- hwndSrc
158+ pid , // 要注入的进程的 ID
159+ injectionLibrary , // 32 位 DLL
160+ injectionLibrary , // 64 位 DLL
161+ // 下面为传递给注入 DLL 的参数
162+ channelName ,
163+ hwndSrc
155164 ) ;
165+ Logger . Info ( $ "已注入CursorHook\n \t 进程ID:{ pid } \n \t 源窗口句柄:{ hwndSrc } ") ;
156166 } catch ( Exception e ) {
157- Console . WriteLine ( "CursorHook 注入失败:" + e . Message ) ;
167+ Logger . Error ( e , "CursorHook注入失败" ) ;
158168 }
159169 }
160170
161171 public void HookCursorAtStartUp ( string exePath ) {
172+ Logger . Info ( "正在进行启动时注入" ) ;
162173
163174 string channelName = null ;
164175#if DEBUG
@@ -185,8 +196,10 @@ public void HookCursorAtStartUp(string exePath) {
185196 // 下面为传递给注入 DLL 的参数
186197 channelName
187198 ) ;
199+
200+ Logger . Info ( $ "已启动进程并注入\n \t 可执行文件:{ exePath } ") ;
188201 } catch ( Exception e ) {
189- Console . WriteLine ( "CursorHook 注入失败:" + e . Message ) ;
202+ Logger . Error ( e , "CursorHook注入失败" ) ;
190203 }
191204 }
192205 }
0 commit comments