资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,.,*,C#WINDOWS API,ae,easy,expansion,Application Programming Interface,.,目录,CWAPI,1,2,3,4,Windows api Introduction,Windows api,Reference,Windows api code,Windows api Hook,.,1,Windows api,Introduction,WINDOWS API,中包含很多函数,约有,1500,多个,.,根据其类别主要分为:,窗口及窗口过程、声音及音频控制、位图及位图处理、,INI,及注册表操作、窗口效果及管理类、字体相关、错误处理、通用对话框、压缩及加密控制、文件及目录操作、剪切板使用、绘图及图形相关、,游戏手柄及键盘相关、堆栰操作、图标光标相关、菜单操作、,HOOK,及消息处理、鼠标控制类、进程及线程管理、文本及字符串操作、时间和定时器管理、端口及网络相关、存储及显示打印设备、多媒体及,MCI,相关、内存管理、操作系统,SHELL,、,WINDOWS,系统信息和其他等。,WINDOWS,中有,3,个非常重要的底层,DLL,文件,:,KERNEL32.DLL,、,USER32.DLL,和,GDI32.DLL,。其中,KERNEL32.DLL-,主要包含用于管理内存、进程和线程的函数,;,USER32.DLL-,中包含的则是用于执行用户界面任务的函数,比如把用户的鼠标点击操作传递给窗口等等,.,GDI32.DLL-,全称是,GRAPHICAL DEVICE INTERFACE(,图形设备接口,),,包含用于画图和显示文本的函数,比如要显示一个程序窗口,就调用了其中的函数来画这个窗口,.,.,Windows API,的功能分类,文件系统:对文件的基本操作包括文件的创建,打开,读写,关闭,删除,文件属性的更改,目录操作,以及磁盘分卷的操作,镜像文件加密文件系统等。,内存,管理:主要是内存的分配,共享,释放等内容,包括虚拟内存管理,分页机制,堆管理等。,进程 线程 模块,:,包括进程主程序(,exe,),模块,动态链接库,线程的创建,遍历,同步等操作,进程与权限等。,设备,I/O,,驱动程序控制:加载与卸载驱动程序,控制驱动程序,与驱动程序通信等。,调试与错误处理:包括如何开发调试器,程序运行错误处理,日志记录,,widows,可执行文件的结构等。,Windows,系统消息:包括注册表的操作,打开,读取,植入,创建和删除键值对,还包括系统基本消息的获取和设置,如:系统日志,系统版本,计算机名等。,进程间通信:包括使用共享文件进行进程间的通信的方法,使用消息进行进程间通信:使用 管道,共享内存等方法。,定时器与消息机制:消息机制是,windows,系统中很重要的一种机制,几乎所有的,windows,应用程序都在于消息打交道。而,windows,的消息机制又是依赖于定时器,所以了解消息机制对学习,windows,开发是至关重要的。,.,2,Windows api,Reference,引用此名称空间,实现引用非托管函数,using System.Runtime.InteropServices,使用,DllImportAttribute,特性来引入,api,函数,注意声明的是空方法,即方法体为空。,DllImport(user32.dll)public static extern ReturnType FunctionName(type arg1,type arg2,.);,可以使用字段进一步说明特性,用逗号隔开,如:,DllImport(user32.dll,EntryPoint=FindWindow,CallingConvention=CallingConvention.StdCall,CharSet=CharSet.Auto,ExactSpelling=false),private static extern IntPtr FindWindow(string ClassName,string WindowNamw);,.,Dlllmport,举例属性,1,2,3,4,EntryPoint,:,指示要调用的,DLL,入口点的名称或序号。如果你的方法名不想与,api,函数同名的话,要显示指定此参数。,CallingConvention,:,指示向非托管实现传递方法参数时所用的,CallingConvention,值。,CallingConvention.StdCall:,被调用方清理堆栈。它是从托管代码调用非托管函数的默认约定。,CharSet,:,用于标识函数调用时所采用的是,Unicode,还是,ANSI,版本。,ExactSpelling=false,:,告诉编译器,去决定使用,Unicode,还是,ANSI,.,chartset,CharSet,控制调用函数的名称版本及指示如何向方法封送,String,参数,.,。,此字段被设置为,CharSet,值之一。如果,CharSet,字段设置为,Unicode,,则所有字符串参数在传递到非托管实现之前都转换成,Unicode,字符。这还导致向,DLL EntryPoint,的名称中追加字母“,W”,。如果此字段设置为,Ansi,,则字符串将转换成,ANSI,字符串,同时向,DLL EntryPoint,的名称中追加字母“,A”,。,大多数,Win32 API,使用这种追加“,W”,或“,A”,的约定。如果,CharSet,设置为,Auto,,则这种转换就是与平台有关的(在,Windows NT,上为,Unicode,,在,Windows 98,上为,Ansi,)。,CharSet,的默认值为,Ansi,。,CharSet,字段也用于确定将从指定的,DLL,导入哪个版本的函数。,CharSet.Ansi,和,CharSet.Unicode,的名称匹配规则大不相同。对于,Ansi,来说,如果将,EntryPoint,设置为“,MyMethod”,且它存在的话,则返回“,MyMethod”,。如果,DLL,中没有“,MyMethod”,,但存在“,MyMethodA”,,则返回“,MyMethodA”,。,对于,Unicode,来说则正好相反。如果将,EntryPoint,设置为“,MyMethod”,且它存在的话,则返回“,MyMethodW”,。如果,DLL,中不存在“,MyMethodW”,,但存在“,MyMethod”,,则返回“,MyMethod”,。如果使用的是,Auto,,则匹配规则与平台有关(在,Windows NT,上为,Unicode,,在,Windows 98,上为,Ansi,)。如果,ExactSpelling,设置为,true,,则只有当,DLL,中存在“,MyMethod”,时才返回“,MyMethod”,。,.,Windows,数据类型,BOOL,布尔型变量,INT_PTR,指向,INT,类型数据的指针类型,LPWSTR,Unicode,字符串常量,BYTE,字节类型,8,位,INT32,32,位符号整型,SHORT,无符号短整形,CHAR,8,比特字节,INT64,64,位符号整型,SIZE_T,内存大小,CONST,常量,LONG,32,位无符号,long,UCHAR,无符号,CHAR,DWORD,无符号整型数据,32,LONGLONG,64,位符号整型,UINT,无符号,INT,DWORD32,无符号整型数据,32,LONG32,32,位符号长整型,ULONG,无符号,long,DWORD64,无符号整型数据,64,LONG64,64,位符号长整型,VOID,无类型,void,FLOAT,浮点数据类型,LPARAM,消息的,L,参数,HANDLE,对象的句柄,WPARAM,消息的,W,参数,HICON,图标的句柄,LPCSTR,Ansi,字符串常量,HINSTANCE,程序实例句柄,LPCWSTR,Unicode,字符串常量,HKEY,注册表键句柄,LPDWORD,指向,DWORD,类型数据指针,HMODULE,模块的句柄,WCHAR,16,位,Unicode,字符,HWND,窗口句柄,WINAPI,函数调用方式,INT,32,整形数据,WORD,16,无符号整形,.,键码值,数据类型,.,3,IS WINDOWS API Code,http:,/,.,/,/,锁定系统,/,DllImport(user32.dll),private static extern bool LockWorkStation();,/,/,锁定键盘鼠标输入 但不锁定,ctr+alt+delete,/,DllImport(user32.dll),private static extern bool BlockInput(bool fBlockIt);,private void skinButton29_Click(object sender,EventArgs e),bool result=BlockInput(true);,if(result=false),throw new Win32Exception(Marshal.GetLastWin32Error();,/,根据鼠标位置信息 得到鼠标下 窗体的句柄值,DllImport(user32.dll),private static extern IntPtr WindowFromPoint(Point p);,DllImport(user32.dll),private static extern int GetClassName(IntPtr hWnd,StringBuilder lpClassName,int nMaxCount);,private void skinButton21_Click(object sender,EventArgs e),Point p;,GetCursorPos(out p);,int hwnd=WindowFromPoint(p);,IntPtr handle=(IntPtr)hwnd;,StringBuilder sb=new StringBuilder(256);,GetClassName(handle,sb,sb.Capacity);,MessageBox.Show(sb.ToString();,/,设置鼠标坐标 基于屏幕,DllImport(user32.dll),private static extern bool SetCursorPos(int x,int y);,private void skinButton3_Click(object sender,EventArgs e),SetCursorPos(10,10);,.,DllImport(user32.dll),private static extern IntPtr GetActiveWindow();/,获得当前活动窗体,DllImport(user32.dll),public static extern bool SetForegroundWindow(IntPtr hWnd);/,设置窗体获得焦点,DllImport(user32.dll),public static extern IntPtr GetForegroundWindow();/,得到当前获得焦点的窗口句柄,DllImport(user32.dll),导入模拟键盘的方法,第一个值为虚拟键值,第二个参数为扫描不设置,为,0,,第三个数为按键状态选项,keydown,为,0,,如果为,keyup,则设置成,,KEYEVENT_KEYUP,第四个参数一般为,0,public static extern void keybd_event(byte bVk,byte bScan,int dwFlags,int dwExtraInfo);,private void skinButton10_Click(object sender,EventArgs e),IntPtr Revit=GetActiveWindow();,SetForegroundWindow(Revit);,keybd_event(0 x1B,0,0,0);,keybd_event(0 x1B,0,2,0);,keybd_event(0 x1B,0,0,0);,keybd_event(0 x1B,0,2,0);,DllImport(user32.dll),private static extern IntPtr GetDesktopWindow();/,获得桌面句柄,.,DllImport(user32.dll),public static extern IntPtr FindWindow(string ClassName,string WindowNamw);,DllImport(user32.dll),public static extern int GetWindowText(int hWnd,StringBuilder lptext,int nCount);,DllImport(user32.dll),public static extern int EnumWindows(CALLBACK call,int lParam);,public delegate bool CALLBACK(int hwnd,int lparam);,DllImport(user32.dll),public static extern bool EnumChildWindows(IntPtr window,EnumWindowProc callback,IntPtr lparam);,public delegate bool EnumWindowProc(IntPtr hWnd,IntPtr parameter);,DllImport(user32.dll),public static extern int SendMessage(IntPtr hWnd,int Msg,IntPtr wParam,int lParam);,DllImport(user32.dll),public static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter,string lpszClass,string lpszWindow,);,.,class propertyButton,DllImport(user32.dll),public static extern IntPtr FindWindow(string ClassName,string WindowNamw);,DllImport(user32.dll),public static extern int GetWindowText(int hWnd,StringBuilder lptext,int nCount);,DllImport(user32.dll),public static extern int EnumWindows(CALLBACK call,int lParam);,public delegate bool CALLBACK(int hwnd,int lparam);,DllImport(user32.dll),public static extern bool EnumChildWindows(IntPtr window,EnumWindowProc callback,IntPtr lparam);,public delegate bool EnumWindowProc(IntPtr hWnd,IntPtr parameter);,DllImport(user32.dll),public static extern int SendMessage(IntPtr hWnd,int Msg,IntPtr wParam,int lParam);,DllImport(user32.dll),public static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter,string lpszClass,string lpszWindow);,public const int WM_CLICK=0 x00f5;,public static IntPtr Revit=IntPtr.Zero;,public static IntPtr Edit=IntPtr.Zero;,.,public static int SetEdit(),CALLBACK cb=BackCallHwnd;,EnumWindows(cb,0);,if(Revit!=IntPtr.Zero),EnumWindowProc childProc=FindChildEditHwnd;,EnumChildWindows(Revit,childProc,IntPtr.Zero);,if(Edit!=IntPtr.Zero),SendMessage(Edit,WM_CLICK,IntPtr.Zero,0);,return Edit.ToInt32();,public static bool FindChildEditHwnd(IntPtr hwndParent,IntPtr lParam),IntPtr hwnd=FindWindowEx(hwndParent,IntPtr.Zero,Button,编辑类型,);,if(hwnd!=IntPtr.Zero),Edit=hwnd;,return false;,return true;,.,public static bool BackCallHwnd(int hwnd,int lparam),StringBuilder sb=new StringBuilder(256);,GetWindowText(hwnd,sb,sb.Capacity);,if(sb.ToString().Contains(Autodesk Revit 2016),Revit=FindWindow(null,sb.ToString();,return false;,return true;,完结,.,Just windows,Is eBIM Team,.,
展开阅读全文