【整理】c# 调用windows API(user32.dll)

User32.dll提供了很多可供调用的接口,大致如下(转自http://blog.csdn.net/zhang399401/article/details/6978803

using System;   
using System.Collections.Generic;   
using System.Linq;   
using System.Text;   
using System.Runtime.InteropServices;   
  
namespace WindowsAPI   
{   
    class CSharp_Win32Api   
    {  
        #region User32.dll 函数   
        /// <summary>   
        /// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在GDI函数中使用该句柄来在设备上下文环境中绘图。hWnd:设备上下文环境被检索的窗口的句柄   
        /// </summary>   
        [DllImport("user32.dll", CharSet = CharSet.Auto)]   
        public static extern IntPtr GetDC(IntPtr hWnd);   
        /// <summary>   
        /// 函数释放设备上下文环境(DC)供其他应用程序使用。
   
        /// </summary>   
        public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);   
        /// <summary>   
        /// 该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。   
        /// </summary>   
        static public extern IntPtr GetDesktopWindow();   
        /// <summary>   
        /// 该函数设置指定窗口的显示状态。   
        /// </summary>   
        static public extern bool ShowWindow(IntPtr hWnd, short State);   
        /// <summary>   
        /// 通过发送重绘消息 WM_PAINT 给目标窗体来更新目标窗体客户区的无效区域。   
        /// </summary>   
        static public extern bool UpdateWindow(IntPtr hWnd);   
        /// <summary>   
        /// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。   
        /// </summary>   
        static public extern bool SetForegroundWindow(IntPtr hWnd);   
        /// <summary>   
        /// 该函数改变一个子窗口,弹出式窗口式顶层窗口的尺寸,位置和Z序。   
        /// </summary>   
        static public extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, uint flags);   
        /// <summary>   
        /// 打开剪切板   
        /// </summary>   
        static public extern bool OpenClipboard(IntPtr hWndNewOwner);   
        /// <summary>   
        /// 关闭剪切板   
        /// </summary>   
        static public extern bool CloseClipboard();   
        /// <summary>   
        /// 打开清空</summary>   
        static public extern bool EmptyClipboard();   
        /// <summary>   
        /// 将存放有数据的内存块放入剪切板的资源管理中   
        /// </summary>   
        static public extern IntPtr SetClipboardData(uint Format, IntPtr hData);   
        /// <summary>   
        /// 在一个矩形中装载指定菜单条目的屏幕坐标信息    
        /// </summary>   
        static public extern bool GetMenuItemRect(IntPtr hWnd, IntPtr hMenu, uint Item, ref RECT rc);   
  
        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]   
        /// <summary>   
        /// 该函数获得一个指定子窗口的父窗口句柄。   
        /// </summary>   
        public static extern IntPtr GetParent(IntPtr hWnd);   
        /// <summary>   
        /// 该函数将指定的消息发送到一个或多个窗口。此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回。    
        /// </summary>   
        /// <param name="hWnd">其窗口程序将接收消息的窗口的句柄</param>   
        /// <param name="msg">指定被发送的消息</param>   
        /// <param name="wParam">指定附加的消息指定信息</param>   
        /// <param name="lParam">指定附加的消息指定信息</param>   
        /// <returns></returns>   
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);   
        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);           
        public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref RECT lParam);   
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref POINT lParam);          
        public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TBBUTTON lParam);           
        public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TBBUTTONINFO lParam);         
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref REBARBANDINFO lParam);         
        public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TVITEM lParam);          
        public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref LVITEM lParam);       
        public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref HDITEM lParam);      
        public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref HD_HITTESTINFO hti);     
        /// <summary>   
        /// 该函数将一个消息放入(寄送)到与指定窗口创建的线程相联系消息队列里   
        /// </summary>   
        public static extern IntPtr PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);   
        public static extern IntPtr SetWindowsHookEx(int hookid, HookProc pfnhook, IntPtr hinst, int threadid);   
  
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]   
        public static extern bool UnhookWindowsHookEx(IntPtr hhook);   
  
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]   
        public static extern IntPtr CallNextHookEx(IntPtr hhook, int code, IntPtr wparam, IntPtr lparam);   
        /// <summary>   
        /// 该函数对指定的窗口设置键盘焦点。   
        /// </summary>   
        public static extern IntPtr SetFocus(IntPtr hWnd);   
        /// <summary>   
        /// 该函数在指定的矩形里写入格式化文本,根据指定的方法对文本格式化(扩展的制表符,字符对齐、折行等)。   
        /// </summary>   
        public extern static int DrawText(IntPtr hdc, string lpString, int nCount, ref RECT lpRect, int uFormat);   
        /// <summary>   
        /// 该函数改变指定子窗口的父窗口。   
        /// </summary>   
        public extern static IntPtr SetParent(IntPtr hChild, IntPtr hParent);   
        /// <summary>   
        /// 获取对话框中子窗口控件的句柄   
        /// </summary>   
        public extern static IntPtr GetDlgItem(IntPtr hDlg, int nControlID);   
        /// <summary>   
        /// 该函数获取窗口客户区的坐标。   
        /// </summary>   
        public extern static int GetClientRect(IntPtr hWnd, ref RECT rc);   
        /// <summary>   
        /// 该函数向指定的窗体添加一个矩形,然后窗口客户区域的这一部分将被重新绘制。   
        /// </summary>   
        public extern static int InvalidateRect(IntPtr hWnd, IntPtr rect, int bErase);   
        /// <summary>   
        /// 该函数产生对其他线程的控制,如果一个线程没有其他消息在其消息队列里。   
        /// </summary>   
        public static extern bool WaitMessage();   
        /// <summary>   
        /// 该函数为一个消息检查线程消息队列,并将该消息(如果存在)放于指定的结构。   
        /// </summary>   
        public static extern bool PeekMessage(ref MSG msg, int hWnd, uint wFilterMin, uint wFilterMax, uint wFlag);   
        /// <summary>   
        /// 该函数从调用线程的消息队列里取得一个消息并将其放于指定的结构。此函数可取得与指定窗口联系的消息和由PostThreadMesssge寄送的线程消息。此函数接收一定范围的消息值。   
        /// </summary>   
        public static extern bool GetMessage(ref MSG msg, int hWnd, uint wFilterMin, uint wFilterMax);   
        /// <summary>   
        /// 该函数将虚拟键消息转换为字符消息。   
        /// </summary>   
        public static extern bool TranslateMessage(ref MSG msg);   
        /// <summary>   
        /// 该函数调度一个消息给窗口程序。   
        /// </summary>   
        public static extern bool DispatchMessage(ref MSG msg);   
        /// <summary>   
        /// 该函数从一个与应用事例相关的可执行文件(EXE文件)中载入指定的光标资源.   
        /// </summary>   
        public static extern IntPtr LoadCursor(IntPtr hInstance, uint cursor);   
        /// <summary>   
        /// 该函数确定光标的形状。   
        /// </summary>   
        public static extern IntPtr SetCursor(IntPtr hCursor);   
        /// <summary>   
        /// 确定当前焦点位于哪个控件上。   
        /// </summary>   
        public static extern IntPtr GetFocus();   
        /// <summary>   
        /// 该函数从当前线程中的窗口释放鼠标捕获,并恢复通常的鼠标输入处理。捕获鼠标的窗口接收所有的鼠标输入(无论光标的位置在哪里),除非点击鼠标键时,光标热点在另一个线程的窗口中。   
        /// </summary>   
        public static extern bool ReleaseCapture();   
        /// <summary>   
        /// 准备指定的窗口来重绘并将绘画相关的信息放到一个PAINTSTRUCT结构中。   
        /// </summary>   
        public static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);   
        /// <summary>   
        /// 标记指定窗口的绘画过程结束,每次调用BeginPaint函数之后被请求   
        /// </summary>   
        public static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);   
        /// <summary>   
        /// 半透明窗体   
        /// </summary>   
        public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);   
        /// <summary>   
        /// 该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。   
        /// </summary>   
        public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);   
        /// <summary>   
        /// 该函数将指定点的用户坐标转换成屏幕坐标。   
        /// </summary>   
        public static extern bool ClientToScreen(IntPtr hWnd, ref POINT pt);   
        /// <summary>   
        /// 当在指定时间内鼠标指针离开或盘旋在一个窗口上时,此函数寄送消息。   
        /// </summary>   
        public static extern bool TrackMouseEvent(ref TRACKMOUSEEVENTS tme);   
        /// <summary>   
        ///    
        /// </summary>   
        public static extern bool SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);   
        /// <summary>   
        /// 该函数检取指定虚拟键的状态。   
        /// </summary>   
        public static extern ushort GetKeyState(int virtKey);   
        /// <summary>   
        /// 该函数改变指定窗口的位置和尺寸。对于顶层窗口,位置和尺寸是相对于屏幕的左上角的:对于子窗口,位置和尺寸是相对于父窗口客户区的左上角坐标的。   
        /// </summary>   
        public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);   
        /// <summary>   
        /// 该函数获得指定窗口所属的类的类名。   
        /// </summary>   
        public static extern int GetClassName(IntPtr hWnd, out STRINGBUFFER ClassName, int nMaxCount);   
        /// <summary>   
        /// 该函数改变指定窗口的属性   
        /// </summary>   
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);   
        /// <summary>   
        /// 该函数检索指定窗口客户区域或整个屏幕的显示设备上下文环境的句柄,在随后的GDI函数中可以使用该句柄在设备上下文环境中绘图。   
        /// </summary>   
        public static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hRegion, uint flags);   
        /// <summary>   
        /// 获取整个窗口(包括边框、滚动条、标题栏、菜单等)的设备场景 返回值 Long。   
        /// </summary>   
        public static extern IntPtr GetWindowDC(IntPtr hWnd);   
        /// <summary>   
        /// 该函数用指定的画刷填充矩形,此函数包括矩形的左上边界,但不包括矩形的右下边界。   
        /// </summary>   
        public static extern int FillRect(IntPtr hDC, ref RECT rect, IntPtr hBrush);   
        /// <summary>   
        /// 该函数返回指定窗口的显示状态以及被恢复的、最大化的和最小化的窗口位置。   
        /// </summary>   
        public static extern int GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT wp);   
        /// <summary>   
        /// 该函数改变指定窗口的标题栏的文本内容   
        /// </summary>   
        public static extern int SetWindowText(IntPtr hWnd, string text);   
        /// <summary>   
        /// 该函数将指定窗口的标题条文本(如果存在)拷贝到一个缓存区内。如果指定的窗口是一个控制,则拷贝控制的文本。   
        /// </summary>   
        public static extern int GetWindowText(IntPtr hWnd, out STRINGBUFFER text, int maxCount);   
        /// <summary>   
        /// 用于得到被定义的系统数据或者系统配置信息.   
        /// </summary>   
        static public extern int GetSystemMetrics(int nIndex);   
        /// <summary>   
        /// 该函数设置滚动条参数,包括滚动位置的最大值和最小值,页面大小,滚动按钮的位置。   
        /// </summary>   
        static public extern int SetScrollInfo(IntPtr hwnd, int bar, ref SCROLLINFO si, int fRedraw);   
        /// <summary>   
        /// 该函数显示或隐藏所指定的滚动条。   
        /// </summary>   
        public static extern int ShowScrollBar(IntPtr hWnd, int bar, int show);   
        /// <summary>   
        /// 该函数可以激活一个或两个滚动条箭头或是使其失效。   
        /// </summary>   
        public static extern int EnableScrollBar(IntPtr hWnd, uint flags, uint arrows);   
        /// <summary>   
        /// 该函数将指定的窗口设置到Z序的顶部。   
        /// </summary>   
        public static extern int BringWindowToTop(IntPtr hWnd);   
        /// <summary>   
        /// 该函数滚动指定窗体客户区域的目录。   
        /// </summary>   
        static public extern int ScrollWindowEx(IntPtr hWnd, int dx, int dy,ref RECT rcScroll, ref RECT rcClip, IntPtr UpdateRegion, ref RECT rcInvalidated, uint flags);   
        /// <summary>   
        /// 该函数确定给定的窗口句柄是否识别一个已存在的窗口。   
        /// </summary>   
        public static extern int IsWindow(IntPtr hWnd);   
        /// <summary>   
        /// 该函数将256个虚拟键的状态拷贝到指定的缓冲区中。   
        /// </summary>   
        public static extern int GetKeyboardState(byte[] pbKeyState);   
        /// <summary>   
        /// 该函数将指定的虚拟键码和键盘状态翻译为相应的字符或字符串。该函数使用由给定的键盘布局句柄标识的物理键盘布局和输入语言来翻译代码。   
        /// </summary>   
        public static extern int ToAscii(int uVirtKey,int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey,int fuState);   
          

在窗口列表中寻找与指定条件相符的第一个子窗口 。

该函数获得一个窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配。这个函数查找子窗口,从排在给定的子窗口后面的下一个子窗口开始。在查找时不区分大小写。

      [DllImport("user32.dll ", SetLastError = true)]
       internal static extern IntPtr FindWindowEx(IntPtr parentHandle, int childAfter, string className, string windowTitle);

参数;

(1)hwndParent:要查找的子窗口所在的窗口句柄(如果设置了hwndParent,则表示从这个hwndParent指向的父窗口中搜索子窗口)。

如果hwndParent为 0 ,则函数以桌面窗口为父窗口,查找桌面窗口的所有子窗口。

Windows NT5.0 and later:如果hwndParent是HWND_MESSAGE,函数仅查找所有消息窗口。

(2)hwndChildAfter :子窗口句柄。查找从在Z序中的下一个子窗口开始。子窗口必须为hwndParent窗口的直接子窗口而非后代窗口。如果HwndChildAfter为NULL,查找从hwndParent的第一个子窗口开始。如果hwndParent 和 hwndChildAfter同时为NULL,则函数查找所有的顶层窗口及消息窗口。

(3)lpszClass:指向一个指定了类名的空结束字符串,或一个标识类名字符串的成员的指针。如果该参数为一个成员,则它必须为前次调用theGlobaIAddAtom函数产生的全局成员。该成员为16位,必须位于lpClassName的低16位,高位必须为0。

(4)lpszWindow:指向一个指定了窗口名(窗口标题)的空结束字符串。如果该参数为 NULL,则为所有窗口全匹配。

返回值

Long,找到的窗口的句柄。如未找到相符窗口,则返回零。会设置GetLastError

如果函数成功,返回值为具有指定类名和窗口名的窗口句柄。如果函数失败,返回值为NULL。

若想获得更多错误信息,请调用GetLastError函数。

声明

1.VB 声明

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

2.C# 声明

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

#endregion   
  
    }   
}
  

 

另外

User32.dll的函数基本使用C/C++写的,那么其参数和返回值的类型就有可能需要和C#对应下

对应如下:

BOOL=System.Int32 
BOOLEAN=System.Int32 
BYTE=System.UInt16 
CHAR=System.Int16 
COLORREF=System.UInt32
DWORD=System.UInt32 
DWORD32=System.UInt32 
DWORD64=System.UInt64 
FLOAT=System.Float 
HACCEL=System.IntPtr
HANDLE=System.IntPtr 
HBITMAP=System.IntPtr 
HBRUSH=System.IntPtr 
HCONV=System.IntPtr 
HCONVLIST=System.IntPtr
HCURSOR=System.IntPtr 
HDC=System.IntPtr 
HDDEDATA=System.IntPtr 
HDESK=System.IntPtr 
HDROP=System.IntPtr
HDWP=System.IntPtr 
HENHMETAFILE=System.IntPtr 
HFILE=System.IntPtr 
HFONT=System.IntPtr 
HGDIOBJ=System.IntPtr
HGLOBAL=System.IntPtr 
HHOOK=System.IntPtr 
HICON=System.IntPtr 
HIMAGELIST=System.IntPtr 
HIMC=System.IntPtr
HINSTANCE=System.IntPtr 
HKEY=System.IntPtr 
HLOCAL=System.IntPtr 
HMENU=System.IntPtr 
HMETAFILE=System.IntPtr
HMODULE=System.IntPtr 
HMONITOR=System.IntPtr 
HPALETTE=System.IntPtr 
HPEN=System.IntPtr 
HRGN=System.IntPtr
HRSRC=System.IntPtr 
HSZ=System.IntPtr 
HWINSTA=System.IntPtr 
HWND=System.IntPtr 
INT=System.Int32
INT32=System.Int32 
INT64=System.Int64 
LONG=System.Int32 
LONG32=System.Int32 
LONG64=System.Int64
LONGLONG=System.Int64 
LPARAM=System.IntPtr 
LPBOOL=System.Int16[] 
LPBYTE=System.UInt16[] 
LPCOLORREF=System.UInt32[]
LPCSTR=System.String 
LPCTSTR=System.String 
LPCVOID=System.UInt32 
LPCWSTR=System.String 
LPDWORD=System.UInt32[]
LPHANDLE=System.UInt32 
LPINT=System.Int32[] 
LPLONG=System.Int32[] 
LPSTR=System.String 
LPTSTR=System.String
LPVOID=System.UInt32 
LPWORD=System.Int32[] 
LPWSTR=System.String 
LRESULT=System.IntPtr 
PBOOL=System.Int16[]
PBOOLEAN=System.Int16[] 
PBYTE=System.UInt16[] 
PCHAR=System.Char[] 
PCSTR=System.String 
PCTSTR=System.String
PCWCH=System.UInt32 
PCWSTR=System.UInt32 
PDWORD=System.Int32[] 
PFLOAT=System.Float[] 
PHANDLE=System.UInt32
PHKEY=System.UInt32 
PINT=System.Int32[] 
PLCID=System.UInt32 
PLONG=System.Int32[] 
PLUID=System.UInt32
PSHORT=System.Int16[] 
PSTR=System.String 
PTBYTE=System.Char[] 
PTCHAR=System.Char[] 
PTSTR=System.String
PUCHAR=System.Char[] 
PUINT=System.UInt32[] 
PULONG=System.UInt32[] 
PUSHORT=System.UInt16[] 
PVOID=System.UInt32
PWCHAR=System.Char[] 
PWORD=System.Int16[] 
PWSTR=System.String 
REGSAM=System.UInt32 
SC_HANDLE=System.IntPtr
SC_LOCK=System.IntPtr 
SHORT=System.Int16 
SIZE_T=System.UInt32 
SSIZE_=System.UInt32 
TBYTE=System.Char
TCHAR=System.Char 
UCHAR=System.Byte 
UINT=System.UInt32 
UINT32=System.UInt32 
UINT64=System.UInt64
ULONG=System.UInt32 
ULONG32=System.UInt32 
ULONG64=System.UInt64 
ULONGLONG=System.UInt64 
USHORT=System.UInt16
WORD=System.UInt16 
WPARAM=System.IntPtr

 

 

具体代码,例子如下:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace QQLib
{
    public static class WINAPI
    {
        internal const UInt16 KEYEVENTF_EXTENDEDKEY = 0x0001;
        internal const UInt16 KEYEVENTF_KEYUP = 0x0002;

        internal const UInt16 MOUSEEVENTF_ABSOLUTE = 0x8000;
        internal const UInt16 MOUSEEVENTF_MOVE = 0x0001;
        internal const UInt16 MOUSEEVENTF_LEFTDOWN = 0x0002;
        internal const UInt16 MOUSEEVENTF_LEFTUP = 0x0004;
        internal const UInt16 MOUSEEVENTF_RIGHTDOWN = 0x0008;
        internal const UInt16 MOUSEEVENTF_RIGHTUP = 0x0010;
        internal const UInt16 MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        internal const UInt16 MOUSEEVENTF_MIDDLEUP = 0x0040;
        internal const UInt16 MOUSEEVENTF_WHEEL = 0x0800;

        internal const UInt16 WM_KEYDOWN = 0x0100;
        internal const UInt16 WM_KEYUP = 0x0101;
        internal const UInt16 WM_SYSKEYDOWN = 0x0104;
        internal const UInt16 WM_SYSKEYUP = 0x0105;
        internal const UInt16 WM_CHAR = 0x0102;
        internal const UInt16 WM_IME_CHAR = 0x0286;

        internal const UInt16 WM_SETTEXT = 0x000C;

        internal const Int16 SW_MAXIMIZE = 3;
        internal const Int16 SW_SHOW = 5;
        internal const Int16 SW_MINIMIZE = 6;
        internal const Int16 SW_RESTORE = 9;

        internal const UInt16 MAPVK_VK_TO_VSC = (0);
        internal const UInt16 MAPVK_VSC_TO_VK = (1);
        internal const UInt16 MAPVK_VK_TO_CHAR = (2);

        internal const int FILE_MAP_READ = 0x0004;
        internal const int FILE_MAP_WRITE = 0x0002;
        internal const int INVALID_HANDLE_VALUE = -1;
        internal const int PAGE_READWRITE = 0x04;

        internal static readonly IntPtr HGDI_ERROR = (IntPtr)(unchecked((int)0xFFFFFFFF));

        internal const int WM_HOTKEY = 0x312;

        internal const int URLMON_OPTION_USERAGENT = 0x10000001;

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            [MarshalAs(UnmanagedType.I4)]
            public int x;
            [MarshalAs(UnmanagedType.I4)]
            public int y;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            [MarshalAs(UnmanagedType.I4)]
            public int left;
            [MarshalAs(UnmanagedType.I4)]
            public int top;
            [MarshalAs(UnmanagedType.I4)]
            public int right;
            [MarshalAs(UnmanagedType.I4)]
            public int bottom;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct WINDOWPLACEMENT
        {
            [MarshalAs(UnmanagedType.U2)]
            public UInt16 length;
            [MarshalAs(UnmanagedType.U2)]
            public UInt16 flags;
            [MarshalAs(UnmanagedType.U2)]
            public UInt16 showCmd;
            [MarshalAs(UnmanagedType.Struct)]
            public POINT ptMinPosition;
            [MarshalAs(UnmanagedType.Struct)]
            public POINT ptMaxPosition;
            [MarshalAs(UnmanagedType.Struct)]
            public RECT rcNormalPosition;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct CURSORINFO
        {
            [MarshalAs(UnmanagedType.U4)]
            public UInt32 cbSize;
            [MarshalAs(UnmanagedType.U4)]
            public UInt32 flags;
            public IntPtr hCursor;
            [MarshalAs(UnmanagedType.Struct)]
            public POINT ptScreenPos;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "FindWindow", SetLastError = true)]
        internal static extern IntPtr FindWindowAPI
            (
            [In]
            [MarshalAs(UnmanagedType.LPTStr)]
            string className,
            [In]
            [MarshalAs(UnmanagedType.LPTStr)]
            string windowName
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "keybd_event", SetLastError = true)]
        internal static extern void KeyBoardAPI
            (
            [In]
            [MarshalAs(UnmanagedType.U1)]
            byte bVk,
            [In]
            [MarshalAs(UnmanagedType.U1)]
            byte bScan,
            [In]
            [MarshalAs(UnmanagedType.U4)]
            UInt32 dwFlags,
            [In]
            [MarshalAs(UnmanagedType.U4)]
            UInt32 dwExtraInfo
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "MapVirtualKey", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.U2)]
        internal static extern UInt16 MapVirtualKey
            (
            [In]
            [MarshalAs(UnmanagedType.U2)]
            UInt16 uCode,
            [In]
            [MarshalAs(UnmanagedType.U2)]
            UInt16 uMapType
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "SendMessage", SetLastError = true)]
        internal static extern IntPtr SendMessage
            (
            [In]
            IntPtr hwnd,
            [In]
            [MarshalAs(UnmanagedType.U2)]
            UInt16 msg,
            [In]
            [MarshalAs(UnmanagedType.U2)]
            UInt16 wParam,
            [In]
            [MarshalAs(UnmanagedType.I4)]
            Int32 lParam
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "PostMessage", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.U2)]
        internal static extern Int16 PostMessage
            (
            [In]
            IntPtr hwnd,
            [In]
            [MarshalAs(UnmanagedType.U2)]
            UInt16 msg,
            [In]
            [MarshalAs(UnmanagedType.U2)]
            UInt16 wParam,
            [In]
            [MarshalAs(UnmanagedType.I4)]
            Int32 lParam
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "GetFocus", SetLastError = true)]
        internal static extern IntPtr GetFocus();

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "GetWindowThreadProcessId", SetLastError = true)]//
        [return: MarshalAs(UnmanagedType.U4)]
        internal static extern UInt32 GetWindowThreadProcessId
            (
            [In]
            IntPtr hwnd,
            [In]
            [Out]
            [MarshalAs(UnmanagedType.U4)]
            ref UInt32 lpdwProcessId
            );

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "GetCurrentThreadId", SetLastError = true)]
        internal static extern UInt32 GetCurrentThreadId();

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "AttachThreadInput", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 AttachThreadInput
            (
            [In]
            [MarshalAs(UnmanagedType.U4)]
            UInt32 idAttach,
            [In]
            [MarshalAs(UnmanagedType.U4)]
            UInt32 idAttachTo,
            [In]
            [MarshalAs(UnmanagedType.I2)]
            Int16 fAttach
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "GetCursorPos", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 GetCursorPos
            (
            [Out]
            [MarshalAs(UnmanagedType.Struct)]  
            out POINT lpPoint
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "WindowFromPoint", SetLastError = true)]
        internal static extern IntPtr WindowFromPoint
            (
            [In]
            [MarshalAs(UnmanagedType.Struct)]
            POINT point
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "GetWindowDC", SetLastError = true)]
        internal static extern IntPtr GetWindowDC
            (
            [In]
            IntPtr hwnd
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "ReleaseDC", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 ReleaseDC
            (
            [In]
            IntPtr hwnd,
            [In]
            IntPtr hdc
            );

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "TextOut", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 TextOut
            (
            [In]
            IntPtr hdc,
            [In]
            [MarshalAs(UnmanagedType.I2)]
            Int16 x,
            [In]
            [MarshalAs(UnmanagedType.I2)]
            Int16 y,
            [In]
            [MarshalAs(UnmanagedType.LPTStr)]
            string lpString,
            [In]
            [MarshalAs(UnmanagedType.I2)]
            Int16 c
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "GetCaretPos", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 GetCaretPos
            (
            [Out]
            [MarshalAs(UnmanagedType.Struct)]
            out POINT lpPoint
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "GetWindowPlacement", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 GetWindowPlacement
            (
            [In]
            IntPtr hwnd,
            [Out]
            [MarshalAs(UnmanagedType.Struct)]
            out WINDOWPLACEMENT lpwndpl
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "SetForegroundWindow", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 SetForegroundWindow
            (
            [In]
            IntPtr hwnd
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "ShowWindow", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 ShowWindow
            (
            [In]
            IntPtr hwnd,
            [In]
            [MarshalAs(UnmanagedType.I2)]
            Int16 nCmdShow
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "GetWindowRect", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 GetWindowRect
            (
            [In]
            IntPtr hwnd,
            [Out]
            [MarshalAs(UnmanagedType.Struct)]
            out RECT lpRect
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "mouse_event", SetLastError = true)]
        internal static extern void MouseAPI
            (
            [In]
            [MarshalAs(UnmanagedType.U4)]
            UInt32 dwFlag,
            [In]
            [MarshalAs(UnmanagedType.U4)]
            UInt32 dx,
            [In]
            [MarshalAs(UnmanagedType.U4)]
            UInt32 dy,
            [In]
            [MarshalAs(UnmanagedType.U4)]
            UInt32 dwData,
            [In]
            [MarshalAs(UnmanagedType.U4)]
            uint dwExtraInfo
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "SetCursorPos", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 SetCursorPos
            (
            [In]
            [MarshalAs(UnmanagedType.I2)]
            Int16 x,
            [In]
            [MarshalAs(UnmanagedType.I2)]
            Int16 y
            );

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "GetPixel", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.U4)]
        internal static extern UInt32 GetPixel
            (
            [In]
            IntPtr hdc,
            [In]
            [MarshalAs(UnmanagedType.I2)]
            Int16 x,
            [In]
            [MarshalAs(UnmanagedType.I2)]
            Int16 y
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "GetDC", SetLastError = true)]
        internal static extern IntPtr GetDC
            (
            [In]
            IntPtr hwnd
            );

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "BitBlt", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 BitBlt
            (
            [In]
            IntPtr hdcDest,
            [In]
            [MarshalAs(UnmanagedType.I4)]
            int xDest,
            [In]
            [MarshalAs(UnmanagedType.I4)]
            int yDest,
            [In]
            [MarshalAs(UnmanagedType.I4)]
            int wDest,
            [In]
            [MarshalAs(UnmanagedType.I4)]
            int hDest,
            [In]
            IntPtr hdcSource,
            [In]
            [MarshalAs(UnmanagedType.I4)]
            int xSrc,
            [In]
            [MarshalAs(UnmanagedType.I4)]
            int ySrc,
            [In]
            [MarshalAs(UnmanagedType.U4)]
            UInt32 rop
            );

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "DeleteDC", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 DeleteDC
            (
            [In]
            IntPtr hDc
            );

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "DeleteObject", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 DeleteObject
            (
            [In]
            IntPtr hObject
            );

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "CreateCompatibleBitmap", SetLastError = true)]
        internal static extern IntPtr CreateCompatibleBitmap
            (
            [In]
            IntPtr hdc,
            [In]
            [MarshalAs(UnmanagedType.I4)]
            Int32 nWidth,
            [In]
            [MarshalAs(UnmanagedType.I4)]
            Int32 nHeight
            );

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "CreateCompatibleDC", SetLastError = true)]
        internal static extern IntPtr CreateCompatibleDC
            (
            [In]
            IntPtr hdc
            );

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "SelectObject", SetLastError = true)]
        internal static extern IntPtr SelectObject
            (
            [In]
            IntPtr hdc,
            [In]
            IntPtr hgidobj
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "GetDesktopWindow", SetLastError = true)]
        internal static extern IntPtr GetDesktopWindow();

        [DllImport("Kernel32.dll", EntryPoint = "CreateFileMapping")]
        internal static extern IntPtr CreateFileMapping(IntPtr hFile, //HANDLE hFile,
         UInt32 lpAttributes,//LPSECURITY_ATTRIBUTES lpAttributes,  //0
         UInt32 flProtect,//DWORD flProtect
         UInt32 dwMaximumSizeHigh,//DWORD dwMaximumSizeHigh,
         UInt32 dwMaximumSizeLow,//DWORD dwMaximumSizeLow,
         string lpName//LPCTSTR lpName
         );

        [DllImport("Kernel32.dll", EntryPoint = "OpenFileMapping")]
        internal static extern IntPtr OpenFileMapping(
         UInt32 dwDesiredAccess,//DWORD dwDesiredAccess,
         int bInheritHandle,//BOOL bInheritHandle,
         string lpName//LPCTSTR lpName
         );

        [DllImport("Kernel32.dll", EntryPoint = "MapViewOfFile")]
        internal static extern IntPtr MapViewOfFile(
         IntPtr hFileMappingObject,//HANDLE hFileMappingObject,
         UInt32 dwDesiredAccess,//DWORD dwDesiredAccess
         UInt32 dwFileOffsetHight,//DWORD dwFileOffsetHigh,
         UInt32 dwFileOffsetLow,//DWORD dwFileOffsetLow,
         UInt32 dwNumberOfBytesToMap//SIZE_T dwNumberOfBytesToMap
         );

        [DllImport("Kernel32.dll", EntryPoint = "UnmapViewOfFile")]
        internal static extern int UnmapViewOfFile(IntPtr lpBaseAddress);

        [DllImport("Kernel32.dll", EntryPoint = "CloseHandle")]
        internal static extern int CloseHandle(IntPtr hObject);

        [DllImport("user32.dll")]
        internal static extern bool ClientToScreen(IntPtr hWnd, ref Point p);

        [DllImport("user32.dll")]
        internal static extern bool GetClientRect(IntPtr hWnd, ref Rectangle rect);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "DrawIcon", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 DrawIcon
            (
            [In]
            IntPtr hdc,
            [In]
            [MarshalAs(UnmanagedType.I2)]
            Int16 x,
            [In]
            [MarshalAs(UnmanagedType.I2)]
            Int16 y,
            [In]
            IntPtr hIcon
            );

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "GetCursorInfo", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.I2)]
        internal static extern Int16 GetCursorInfo
            (
            [Out]
            [MarshalAs(UnmanagedType.Struct)]  
            out CURSORINFO pci
            );

        [DllImport("Kernel32")]
        internal static extern IntPtr LoadLibrary(string dllfile);
        [DllImport("Kernel32")]
        internal static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);


        [DllImport("user32.dll ")]
        internal static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        internal static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll ", SetLastError = true)]
        internal static extern IntPtr FindWindowEx(IntPtr parentHandle, int childAfter, string className, string windowTitle);

        [DllImportAttribute("user32.dll", EntryPoint = "RegisterHotKey")]
        internal static extern bool RegisterHotKey
            (
                IntPtr hWnd,        //要注册热键的窗口句柄     
                int id,             //热键编号 定义热键ID(不能与其它ID重复)
                int fsModifiers,    //特殊键如:Ctrl,Alt,Shift,Window   //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效   
                int vk              //一般键如:A B C F1,F2 等   定义热键的内容
            );
        [DllImportAttribute("user32.dll", EntryPoint = "UnregisterHotKey")]
        internal static extern bool UnregisterHotKey
            (
                IntPtr hWnd,        //要取消热键的窗口的句柄     
                int id              //热键编号上面注册热键的编号      要取消热键的ID
            );
        [Flags()]//附上Flags特性后,用该枚举变量是既可以象整数一样进行按位的“|”或者按位的“&”操作
        internal enum MyKeys
        {
            None = 0,
            Alt = 1,
            Ctrl = 2,
            Shift = 4,
            Win = 8
        }

        [DllImport("wininet")]
        internal extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);

        [DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
        internal static extern int UrlMkSetSessionOption(int dwOption, string pBuffer, int dwBufferLength, int dwReserved);
       
    }
}

posted @ 2015-10-22 10:14  潋滟碧水醉落花  阅读(4310)  评论(0编辑  收藏  举报