编译是有很多警告,帮忙看看什么问题

litsnake 2016-07-04 01:03:18
搞了一个小程序,编译没有错误,但是有很多警告,烦请大家看看都是啥问题,是c#菜鸟 见笑了,
谢谢!
1>------ 已启动全部重新生成: 项目: WindowsFormsApplication1, 配置: Release Any CPU ------
1>C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(1635,5): warning MSB3267: 未能在当前目标框架中解析主引用“Microsoft.CSharp”,它是一个框架程序集。“.NETFramework,Version=v3.5”。若要解决此问题,请移除引用“Microsoft.CSharp”,或将应用程序的目标重新指向包含“Microsoft.CSharp”的框架版本。
1>D:\Form1.cs(449,25,449,27): warning CS0162: 检测到无法访问的代码
1>D:\Form1.cs(1116,13,1116,19): warning CS0162: 检测到无法访问的代码
1>D:\Form1.cs(1700,13,1700,17): warning CS0162: 检测到无法访问的代码
1>D:\Form1.cs(2147,13,2147,19): warning CS0162: 检测到无法访问的代码
1>D:\Form1.cs(2280,13,2280,19): warning CS0162: 检测到无法访问的代码
1>D:\Form1.cs(2309,13,2309,19): warning CS0162: 检测到无法访问的代码
1> WindowsFormsApplication1 -> D:\bin\Release\Test.exe
1> 正在运行代码分析...
1>d:\Form1.cs(408): warning CA2122: Microsoft.Security : 'Form1.KeyboardHookProc(int, IntPtr, IntPtr)' 调入具有 LinkDemand 的 'Marshal.PtrToStructure(IntPtr, Type)'。通过此调用,'Marshal.PtrToStructure(IntPtr, Type)' 被间接公开给用户代码。检查以下可能公开避开安全保护的方法的调用堆栈:
1>d:\Form1.cs(408): warning CA2122: ->'Form1.KeyboardHookProc(int, IntPtr, IntPtr)'
1>d:\Form1.cs(408): warning CA2122: ->'Form1.KeyboardHookProc(int, IntPtr, IntPtr)'
1>d:\Form1.cs(380): warning CA2122: Microsoft.Security : 'Form1.MouseHookProc(int, IntPtr, IntPtr)' 调入具有 LinkDemand 的 'Marshal.PtrToStructure(IntPtr, Type)'。通过此调用,'Marshal.PtrToStructure(IntPtr, Type)' 被间接公开给用户代码。检查以下可能公开避开安全保护的方法的调用堆栈:
1>d:\Form1.cs(380): warning CA2122: ->'Form1.MouseHookProc(int, IntPtr, IntPtr)'
1>d:\Form1.cs(380): warning CA2122: ->'Form1.MouseHookProc(int, IntPtr, IntPtr)'
1>d:\Form1.cs(1389): warning CA2202: Microsoft.Usage : 可以在方法 'Form1.SaveImageForModi(Image, string, int, int)' 中多次释放对象 'tempImg'。若要避免生成 System.ObjectDisposedException,不应对一个对象多次调用 Dispose。: Lines: 1389
1>MSBUILD : warning CA1060: Microsoft.Design : 由于是 P/Invoke 方法,因此,应在名为 NativeMethods、SafeNativeMethods 或 UnsafeNativeMethods 的类中定义 'INIClass.GetPrivateProfileString(string, string, string, StringBuilder, int, string)'。
1>MSBUILD : warning CA2101: Microsoft.Globalization : 为了降低安全风险,请将参数 'section' 封送为 Unicode,方法是将 DllImport.CharSet 设置为 CharSet.Unicode,或者将该参数显式封送为 UnmanagedType.LPWStr。如果您需要将该字符串封送为 ANSI 或者与系统相关的编码,请显式指定 MarshalAs,并设置 BestFitMapping=false;为了提高安全性,还应设置 ThrowOnUnmappableChar=true。
1>MSBUILD : warning CA2101: Microsoft.Globalization : 为了降低安全风险,请将参数 'key' 封送为 Unicode,方法是将 DllImport.CharSet 设置为 CharSet.Unicode,或者将该参数显式封送为 UnmanagedType.LPWStr。如果您需要将该字符串封送为 ANSI 或者与系统相关的编码,请显式指定 MarshalAs,并设置 BestFitMapping=false;为了提高安全性,还应设置 ThrowOnUnmappableChar=true。
1>MSBUILD : warning CA2101: Microsoft.Globalization : 为了降低安全风险,请将参数 'def' 封送为 Unicode,方法是将 DllImport.CharSet 设置为 CharSet.Unicode,或者将该参数显式封送为 UnmanagedType.LPWStr。如果您需要将该字符串封送为 ANSI 或者与系统相关的编码,请显式指定 MarshalAs,并设置 BestFitMapping=false;为了提高安全性,还应设置 ThrowOnUnmappableChar=true。
1>MSBUILD : warning CA2101: Microsoft.Globalization : 为了降低安全风险,请将参数 'retVal' 封送为 Unicode,方法是将 DllImport.CharSet 设置为 CharSet.Unicode,或者将该参数显式封送为 UnmanagedType.LPWStr。如果您需要将该字符串封送为 ANSI 或者与系统相关的编码,请显式指定 MarshalAs,并设置 BestFitMapping=false;为了提高安全性,还应设置 ThrowOnUnmappableChar=true。
1>MSBUILD : warning CA1060: Microsoft.Design : 由于是 P/Invoke 方法,因此,应在名为 NativeMethods、SafeNativeMethods 或 UnsafeNativeMethods 的类中定义 'INIClass.WritePrivateProfileString(string, string, string, string)'。
1>MSBUILD : warning CA1901: Microsoft.Portability : 如代码中所声明的,P/Invoke 'INIClass.WritePrivateProfileString(string, string, string, string)' 的返回类型在 32 位 平台上的字节宽度将为 8。这是不正确的,因为此 API 的实际本机声明表明该返回类型在 32 位 平台上的字节宽度应为 4。请参考 MSDN Platform SDK 文档来获取帮助,确定应使用哪种数据类型来代替 'long'。
1>MSBUILD : warning CA1901: Microsoft.Portability : 如代码中所声明的,P/Invoke 'INIClass.WritePrivateProfileString(string, string, string, string)' 的返回类型在 64 位 平台上的字节宽度将为 8。这是不正确的,因为此 API 的实际本机声明表明该返回类型在 64 位 平台上的字节宽度应为 4。请参考 MSDN Platform SDK 文档来获取帮助,确定应使用哪种数据类型来代替 'long'。
1>MSBUILD : warning CA2101: Microsoft.Globalization : 为了降低安全风险,请将参数 'section' 封送为 Unicode,方法是将 DllImport.CharSet 设置为 CharSet.Unicode,或者将该参数显式封送为 UnmanagedType.LPWStr。如果您需要将该字符串封送为 ANSI 或者与系统相关的编码,请显式指定 MarshalAs,并设置 BestFitMapping=false;为了提高安全性,还应设置 ThrowOnUnmappableChar=true。
1>MSBUILD : warning CA2101: Microsoft.Globalization : 为了降低安全风险,请将参数 'key' 封送为 Unicode,方法是将 DllImport.CharSet 设置为 CharSet.Unicode,或者将该参数显式封送为 UnmanagedType.LPWStr。如果您需要将该字符串封送为 ANSI 或者与系统相关的编码,请显式指定 MarshalAs,并设置 BestFitMapping=false;为了提高安全性,还应设置 ThrowOnUnmappableChar=true。
1>MSBUILD : warning CA2101: Microsoft.Globalization : 为了降低安全风险,请将参数 'filePath' 封送为 Unicode,方法是将 DllImport.CharSet 设置为 CharSet.Unicode,或者将该参数显式封送为 UnmanagedType.LPWStr。如果您需要将该字符串封送为 ANSI 或者与系统相关的编码,请显式指定 MarshalAs,并设置 BestFitMapping=false;为了提高安全性,还应设置 ThrowOnUnmappableChar=true。
1>MSBUILD : warning CA1901: Microsoft.Portability : 如代码中所声明的,P/Invoke 'NativeMethods.CallNextHookEx(int, int, IntPtr, IntPtr)' 的参数 'idHook' 在 64 位 平台上的字节宽度将为 4。这是不正确的,因为此 API 的实际本机声明表明该参数在 64 位 平台上的字节宽度应为 8。请参考 MSDN Platform SDK 文档来获取帮助,确定应使用哪种数据类型来代替 'int'。
1>MSBUILD : warning CA1901: Microsoft.Portability : 如代码中所声明的,P/Invoke 'NativeMethods.CallNextHookEx(int, int, IntPtr, IntPtr)' 的返回类型在 64 位 平台上的字节宽度将为 4。这是不正确的,因为此 API 的实际本机声明表明该返回类型在 64 位 平台上的字节宽度应为 8。请参考 MSDN Platform SDK 文档来获取帮助,确定应使用哪种数据类型来代替 'int'。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.CallNextHookEx(int, int, IntPtr, IntPtr)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1901: Microsoft.Portability : 如代码中所声明的,P/Invoke 'NativeMethods.EnumWindows(NativeMethods.WNDENUMPROC, int)' 的参数 'lParam' 在 64 位 平台上的字节宽度将为 4。这是不正确的,因为此 API 的实际本机声明表明该参数在 64 位 平台上的字节宽度应为 8。请参考 MSDN Platform SDK 文档来获取帮助,确定应使用哪种数据类型来代替 'int'。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.EnumWindows(NativeMethods.WNDENUMPROC, int)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.FindWindow(string, string)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA2101: Microsoft.Globalization : 为了降低安全风险,请将参数 'lpWindowName' 封送为 Unicode,方法是将 DllImport.CharSet 设置为 CharSet.Unicode,或者将该参数显式封送为 UnmanagedType.LPWStr。如果您需要将该字符串封送为 ANSI 或者与系统相关的编码,请显式指定 MarshalAs,并设置 BestFitMapping=false;为了提高安全性,还应设置 ThrowOnUnmappableChar=true。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.FindWindowEx(IntPtr, IntPtr, string, string)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.GetClassName(IntPtr, StringBuilder, int)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA2101: Microsoft.Globalization : 为了降低安全风险,请将参数 'lpClassName' 封送为 Unicode,方法是将 DllImport.CharSet 设置为 CharSet.Unicode 或者将该参数显式封送为 UnmanagedType.LPWStr。如果您需要将该字符串封送为 ANSI 或与系统相关的编码,请设置 BestFitMapping=false;为了提高安全性,还可以设置 ThrowOnUnmappableChar=true。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.GetLocalTime(ref NativeMethods.SystemTime)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.GetWindowRect(IntPtr, out NativeMethods.Rect)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.GetWindowText(IntPtr, StringBuilder, int)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.keybd_event(byte, byte, int, int)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1901: Microsoft.Portability : 如代码中所声明的,P/Invoke 'NativeMethods.mouse_event(NativeMethods.MouseEventFlag, int, int, uint, int)' 的参数 'extraInfo' 在 64 位 平台上的字节宽度将为 4。这是不正确的,因为此 API 的实际本机声明表明该参数在 64 位 平台上的字节宽度应为 8。请参考 MSDN Platform SDK 文档来获取帮助,确定应使用哪种数据类型来代替 'int'。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.mouse_event(NativeMethods.MouseEventFlag, int, int, uint, int)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.SetCursorPos(int, int)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.SetForegroundWindow(IntPtr)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.SetLocalTime(ref NativeMethods.SystemTime)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.SetWindowPos(IntPtr, int, int, int, int, int, int)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1901: Microsoft.Portability : 如代码中所声明的,P/Invoke 'NativeMethods.SetWindowsHookEx(int, NativeMethods.HookProc, IntPtr, int)' 的返回类型在 64 位 平台上的字节宽度将为 4。这是不正确的,因为此 API 的实际本机声明表明该返回类型在 64 位 平台上的字节宽度应为 8。请参考 MSDN Platform SDK 文档来获取帮助,确定应使用哪种数据类型来代替 'int'。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.SetWindowsHookEx(int, NativeMethods.HookProc, IntPtr, int)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.ShowWindow(IntPtr, int)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.UnhookWindowsHookEx(int)' 的可访问性,使它在自己的程序集外部不再是可见的。
1>MSBUILD : warning CA2111: Microsoft.Security : 将 'NativeMethods.WindowInfo.hWnd' 设置为私有的。
1> 代码分析完成 -- 0 个错误,44 个警告
========== 全部重新生成: 成功 1 个,失败 0 个,跳过 0 个 ==========
...全文
793 16 打赏 收藏 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 2016-07-05
  • 打赏
  • 举报
回复
警告没关系。 不过你可以跟着警告看看是否严重。这个自己主观也能判断一下的
litsnake 2016-07-05
  • 打赏
  • 举报
回复
引用 13 楼 sp1234 的回复:
[quote=引用 4 楼 litsnake 的回复:] 测试啊,编译通过了,因为以前不用c#开发,不太懂这些警告啥意思,怕这些警告在别的环境或者别的人用的时候 出错而用不了,
如果你针对那些提示而设计测试用例,最多写上5个,基本上就心里有数了。 自己动手写测试的人根本不可能“怕”什么东西。[/quote] 谢谢兄弟,明白你的意思
  • 打赏
  • 举报
回复
警告不代表代码有错误,百度看看,见多了就猜到大概什么问题了
  • 打赏
  • 举报
回复
引用 4 楼 litsnake 的回复:
测试啊,编译通过了,因为以前不用c#开发,不太懂这些警告啥意思,怕这些警告在别的环境或者别的人用的时候 出错而用不了,
如果你针对那些提示而设计测试用例,最多写上5个,基本上就心里有数了。 自己动手写测试的人根本不可能“怕”什么东西。
  • 打赏
  • 举报
回复
针对警告,你可以编写测试用例!这就是我的建议。 实际上就算是测试,今天不报bug,未1个月后也不报bug。未必现在有10个测试时不报bug,而有20个测试时也不报bug。所以就是是测试,也是与时俱进的。 总之,不要“含沙射影”地夸大那些警告。而具体对待它们,自然处理就好了。
litsnake 2016-07-04
  • 打赏
  • 举报
回复
引用 9 楼 johnliuyuan 的回复:
你的这些警告虽然不影响程序运行,但是有一些显示你的代码有一些问题,可以对照说明进行检查
是的,代码应该有点问题,就是在找哪里有问题,该怎么改
  • 打赏
  • 举报
回复
引用 5 楼 litsnake 的回复:
引用 3 楼 starfd 的回复:
首先你得搞清楚Error和Warning的区别,Error是指编译无法通过的错误,Warning是指不影响编译,但可能会影响程序正确性的问题,而你的那些提示里面,哪些是不影响的,哪些是可能影响的很容易看出来吧,提示都这么明确了……
提示是很明确,中文我全都看得懂,但是不明白内在含义,warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.SetWindowsHookEx(int, NativeMethods.HookProc, IntPtr, int)' 的可访问性,使它在自己的程序集外部不再是可见的 啥意思,不是很明白,
编译器多管闲事呗。让你别让程序集外部的dll、exe也可以访问到它。你可以从自己的立场出发来判断。我说“编译期多管闲事”也是从动手的角度,从“如果让我写一个测试用例”的角度出发来判断这样做到底有没有什么 bug。我不认为有什么“天规”或者“洁癖”,我们完全接受编译器的提示,但是我们动手测试一下,结果就算是从外部可以访问它而现在也没能发现bug。这就是结论。
john_QQ:2335298917 2016-07-04
  • 打赏
  • 举报
回复
你的这些警告虽然不影响程序运行,但是有一些显示你的代码有一些问题,可以对照说明进行检查
Poopaye 2016-07-04
  • 打赏
  • 举报
回复
引用
1>MSBUILD : warning CA1901: Microsoft.Portability : 如代码中所声明的,P/Invoke 'NativeMethods.CallNextHookEx(int, int, IntPtr, IntPtr)' 的返回类型在 64 位 平台上的字节宽度将为 4。这是不正确的,因为此 API 的实际本机声明表明该返回类型在 64 位 平台上的字节宽度应为 8。请参考 MSDN Platform SDK 文档来获取帮助,确定应使用哪种数据类型来代替 'int'。
最有可能出问题的是这个,现在你编译64位的程序,应该返回long
  • 打赏
  • 举报
回复
引用 4 楼 litsnake 的回复:
[quote=引用 1 楼 sp1234 的回复:] 你不测试吗? 如果进行真正的测试,那么这些警告可以暂时不看。
测试啊,编译通过了,因为以前不用c#开发,不太懂这些警告啥意思,怕这些警告在别的环境或者别的人用的时候 出错而用不了, [/quote] 你可以针对警告而设计测试用例。你还是没有揣摩一点我说的“测试”真正的意思。
  • 打赏
  • 举报
回复
更改 P/Invoke 'NativeMethods.SetWindowsHookEx(int, NativeMethods.HookProc, IntPtr, int)' 的可访问性,使它在自己的程序集外部不再是可见的 意思是设置访问级别为internal或者protected之类的,不要设置为public
litsnake 2016-07-04
  • 打赏
  • 举报
回复
引用 3 楼 starfd 的回复:
首先你得搞清楚Error和Warning的区别,Error是指编译无法通过的错误,Warning是指不影响编译,但可能会影响程序正确性的问题,而你的那些提示里面,哪些是不影响的,哪些是可能影响的很容易看出来吧,提示都这么明确了……
提示是很明确,中文我全都看得懂,但是不明白内在含义,warning CA1401: Microsoft.Interoperability : 更改 P/Invoke 'NativeMethods.SetWindowsHookEx(int, NativeMethods.HookProc, IntPtr, int)' 的可访问性,使它在自己的程序集外部不再是可见的 啥意思,不是很明白,
litsnake 2016-07-04
  • 打赏
  • 举报
回复
引用 1 楼 sp1234 的回复:
你不测试吗? 如果进行真正的测试,那么这些警告可以暂时不看。
测试啊,编译通过了,因为以前不用c#开发,不太懂这些警告啥意思,怕这些警告在别的环境或者别的人用的时候 出错而用不了,
  • 打赏
  • 举报
回复
首先你得搞清楚Error和Warning的区别,Error是指编译无法通过的错误,Warning是指不影响编译,但可能会影响程序正确性的问题,而你的那些提示里面,哪些是不影响的,哪些是可能影响的很容易看出来吧,提示都这么明确了……
litsnake 2016-07-04
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public class NativeMethods
    {
        public const int WH_MOUSE_LL = 14;
        /// <summary>
        /// Windows NT/2000/XP: Installs a hook procedure that monitors low-level keyboard  input events.
        /// </summary>
        public const int WH_KEYBOARD_LL = 13;

        /// <summary>
        /// Installs a hook procedure that monitors mouse messages. For more information, see the MouseProc hook procedure. 
        /// </summary>
        public const int WH_MOUSE = 7;
        /// <summary>
        /// Installs a hook procedure that monitors keystroke messages. For more information, see the KeyboardProc hook procedure. 
        /// </summary>
        public const int WH_KEYBOARD = 2;

        /// <summary>
        /// The WM_MOUSEMOVE message is posted to a window when the cursor moves. 
        /// </summary>
        public const int WM_MOUSEMOVE = 0x200;
        /// <summary>
        /// The WM_LBUTTONDOWN message is posted when the user presses the left mouse button 
        /// </summary>
        public const int WM_LBUTTONDOWN = 0x201;
        /// <summary>
        /// The WM_RBUTTONDOWN message is posted when the user presses the right mouse button
        /// </summary>
        public const int WM_RBUTTONDOWN = 0x204;
        /// <summary>
        /// The WM_MBUTTONDOWN message is posted when the user presses the middle mouse button 
        /// </summary>
        public const int WM_MBUTTONDOWN = 0x207;
        /// <summary>
        /// The WM_LBUTTONUP message is posted when the user releases the left mouse button 
        /// </summary>
        public const int WM_LBUTTONUP = 0x202;
        /// <summary>
        /// The WM_RBUTTONUP message is posted when the user releases the right mouse button 
        /// </summary>
        public const int WM_RBUTTONUP = 0x205;
        /// <summary>
        /// The WM_MBUTTONUP message is posted when the user releases the middle mouse button 
        /// </summary>
        public const int WM_MBUTTONUP = 0x208;
        /// <summary>
        /// The WM_LBUTTONDBLCLK message is posted when the user double-clicks the left mouse button 
        /// </summary>
        public const int WM_LBUTTONDBLCLK = 0x203;
        /// <summary>
        /// The WM_RBUTTONDBLCLK message is posted when the user double-clicks the right mouse button 
        /// </summary>
        public const int WM_RBUTTONDBLCLK = 0x206;
        /// <summary>
        /// The WM_RBUTTONDOWN message is posted when the user presses the right mouse button 
        /// </summary>
        public const int WM_MBUTTONDBLCLK = 0x209;
        /// <summary>
        /// The WM_MOUSEWHEEL message is posted when the user presses the mouse wheel. 
        /// </summary>
        public const int WM_MOUSEWHEEL = 0x020A;

        /// <summary>
        /// The WM_KEYDOWN message is posted to the window with the keyboard focus when a nonsystem 
        /// key is pressed. A nonsystem key is a key that is pressed when the ALT key is not pressed.
        /// </summary>
        public const int WM_KEYDOWN = 0x100;
        /// <summary>
        /// The WM_KEYUP message is posted to the window with the keyboard focus when a nonsystem 
        /// key is released. A nonsystem key is a key that is pressed when the ALT key is not pressed, 
        /// or a keyboard key that is pressed when a window has the keyboard focus.
        /// </summary>
        public const int WM_KEYUP = 0x101;
        /// <summary>
        /// The WM_SYSKEYDOWN message is posted to the window with the keyboard focus when the user 
        /// presses the F10 key (which activates the menu bar) or holds down the ALT key and then 
        /// presses another key. It also occurs when no window currently has the keyboard focus; 
        /// in this case, the WM_SYSKEYDOWN message is sent to the active window. The window that 
        /// receives the message can distinguish between these two contexts by checking the context 
        /// code in the lParam parameter. 
        /// </summary>
        public const int WM_SYSKEYDOWN = 0x104;
        /// <summary>
        /// The WM_SYSKEYUP message is posted to the window with the keyboard focus when the user 
        /// releases a key that was pressed while the ALT key was held down. It also occurs when no 
        /// window currently has the keyboard focus; in this case, the WM_SYSKEYUP message is sent 
        /// to the active window. The window that receives the message can distinguish between 
        /// these two contexts by checking the context code in the lParam parameter. 
        /// </summary>
        public const int WM_SYSKEYUP = 0x105;

        public const byte VK_SHIFT = 0x10;
        public const byte VK_CAPITAL = 0x14;
        public const byte VK_NUMLOCK = 0x90;



        [DllImport("user32.dll")]
        public static extern bool SetCursorPos(int X, int Y);

        [DllImport("user32.dll")]
        public static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, int extraInfo);

        [Flags]
        public enum MouseEventFlag : uint
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            XDown = 0x0080,
            XUp = 0x0100,
            Wheel = 0x0800,
            VirtualDesk = 0x4000,
            Absolute = 0x8000
        }

        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);


        [DllImport("User32.dll")]
        public static extern void keybd_event(Byte bVk, Byte bScan, Int32 dwFlags, Int32 dwExtraInfo);

        //安装钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
        //卸载钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);
        //调用下一个钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

        public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        public static extern int GetWindowRect(IntPtr hwnd, out  Rect lpRect);

        

        

        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        

        [DllImport("user32.dll ", EntryPoint = "FindWindowEx")]   //找子窗体 
        public static extern IntPtr FindWindowEx(IntPtr hwndParent,
        IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        //private delegate bool CallBack(IntPtr hWnd, int lParam);

        public delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);

        [DllImport("user32.dll")]
        public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);


        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int GetWindowText(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);

        // 用于设置系统时间
        [DllImport("Kernel32.dll")]
        public static extern bool SetLocalTime(ref SystemTime sysTime);
        // 用于获得系统时间
        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(ref SystemTime sysTime);


        [StructLayout(LayoutKind.Sequential)]

        public class MouseHookStruct
        {
            public POINT pt;
            public int hwnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        public class KeyboardHookStruct
        {
            public int vkCode;
            public int ScanCode;
            public int Flags;
            public int Time;
            public int DwExtraInfo;
        }




        public struct WindowInfo
        {
            public IntPtr hWnd;
            public string szWindowName;
            public string szClassName;

        }

        [StructLayout(LayoutKind.Sequential)]
        public struct SystemTime
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMiliseconds;
        }


        public struct Rect
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;

            


        }

        
    }
}

  • 打赏
  • 举报
回复
你不测试吗? 如果进行真正的测试,那么这些警告可以暂时不看。

111,128

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧