C#可以直接调用的Win32API(和VCL做的整理工作非常类似)

Posted 朝闻道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#可以直接调用的Win32API(和VCL做的整理工作非常类似)相关的知识,希望对你有一定的参考价值。

以前整理的Win32 API,可以直接在C#中直接调用,在做WinForm时还是很有帮助的。以前用在一个多窗口界面中,当轮询窗口时,调用API会提高很多效率。 

源码下载 
http://files.cnblogs.com/lordeo/win32api.rar 

 

整理的Win32 API,可以直接在C#中直接调用,在做WinForm时还是很有帮助的。

源码包含三个文件

Win32API.cs,

[csharp] view plain copy
 
  1. using System;  
  2. using System.Drawing;  
  3. using System.Runtime.InteropServices;  
  4. using Lordal.Window.Form.Lib.General;  
  5. using Lordal.Window.Form.Lib.Win32;  
  6.   
  7. namespace Lordeo.Framework  
  8. {  
  9.     /// <summary>  
  10.     /// Windows API Functions  
  11.     /// </summary>  
  12.     public class Win32API  
  13.     {  
  14.         #region .ctor()  
  15.         // No need to construct this object  
  16.         private Win32API()  
  17.         {  
  18.         }  
  19.         #endregion  
  20.          
  21.         #region Constans values  
  22.         public const string TOOLBARCLASSNAME = "ToolbarWindow32";  
  23.         public const string REBARCLASSNAME = "ReBarWindow32";  
  24.         public const string PROGRESSBARCLASSNAME = "msctls_progress32";  
  25.         public const string SCROLLBAR = "SCROLLBAR";  
  26.         #endregion  
  27.  
  28.         #region CallBacks  
  29.         public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);  
  30.         #endregion  
  31.  
  32.         #region Kernel32.dll functions  
  33.         [DllImport("kernel32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]  
  34.             public static extern int GetCurrentThreadId();  
  35.         #endregion  
  36.      
  37.         #region Gdi32.dll functions  
  38.         [DllImport("gdi32.dll")]  
  39.             static public extern bool StretchBlt(IntPtr hDCDest, int XOriginDest, int YOriginDest, int WidthDest, int HeightDest,  
  40.             IntPtr hDCSrc,  int XOriginScr, int YOriginSrc, int WidthScr, int HeightScr, uint Rop);  
  41.         [DllImport("gdi32.dll")]  
  42.             static public extern IntPtr CreateCompatibleDC(IntPtr hDC);  
  43.         [DllImport("gdi32.dll")]  
  44.             static public extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int Width, int Heigth);  
  45.         [DllImport("gdi32.dll")]  
  46.             static public extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);  
  47.         [DllImport("gdi32.dll")]  
  48.             static public extern bool BitBlt(IntPtr hDCDest, int XOriginDest, int YOriginDest, int WidthDest, int HeightDest,  
  49.             IntPtr hDCSrc,  int XOriginScr, int YOriginSrc, uint Rop);  
  50.         [DllImport("gdi32.dll")]  
  51.             static public extern IntPtr DeleteDC(IntPtr hDC);  
  52.         [DllImport("gdi32.dll")]  
  53.             static public extern bool PatBlt(IntPtr hDC, int XLeft, int YLeft, int Width, int Height, uint Rop);  
  54.         [DllImport("gdi32.dll")]  
  55.             static public extern bool DeleteObject(IntPtr hObject);  
  56.         [DllImport("gdi32.dll")]  
  57.             static public extern uint GetPixel(IntPtr hDC, int XPos, int YPos);  
  58.         [DllImport("gdi32.dll")]  
  59.             static public extern int SetMapMode(IntPtr hDC, int fnMapMode);  
  60.         [DllImport("gdi32.dll")]  
  61.             static public extern int GetObjectType(IntPtr handle);  
  62.         [DllImport("gdi32")]  
  63.             public static extern IntPtr CreateDIBSection(IntPtr hdc, ref BITMAPINFO_FLAT bmi,   
  64.             int iUsage, ref int ppvBits, IntPtr hSection, int dwOffset);  
  65.         [DllImport("gdi32")]  
  66.             public static extern int GetDIBits(IntPtr hDC, IntPtr hbm, int StartScan, int ScanLines, int lpBits, BITMAPINFOHEADER bmi, int usage);  
  67.         [DllImport("gdi32")]  
  68.             public static extern int GetDIBits(IntPtr hdc, IntPtr hbm, int StartScan, int ScanLines, int lpBits, ref BITMAPINFO_FLAT bmi, int usage);  
  69.         [DllImport("gdi32")]  
  70.             public static extern IntPtr GetPaletteEntries(IntPtr hpal, int iStartIndex, int nEntries, byte[] lppe);  
  71.         [DllImport("gdi32")]  
  72.             public static extern IntPtr GetSystemPaletteEntries(IntPtr hdc, int iStartIndex, int nEntries, byte[] lppe);  
  73.         [DllImport("gdi32")]  
  74.             public static extern uint SetDCBrushColor(IntPtr hdc,  uint crColor);  
  75.         [DllImport("gdi32")]  
  76.             public static extern IntPtr CreateSolidBrush(uint crColor);  
  77.         [DllImport("gdi32")]  
  78.             public static extern int SetBkMode(IntPtr hDC, BackgroundMode mode);  
  79.         [DllImport("gdi32")]  
  80.             public static extern int SetViewportOrgEx(IntPtr hdc,  int x, int y,  int param);  
  81.         [DllImport("gdi32")]  
  82.             public static extern uint SetTextColor(IntPtr hDC, uint colorRef);  
  83.         [DllImport("gdi32")]  
  84.             public static extern int SetStretchBltMode(IntPtr hDC, int StrechMode);  
  85.         #endregion  
  86.  
  87.         #region Uxtheme.dll functions  
  88.         [DllImport("uxtheme.dll")]  
  89.         static public extern int SetWindowTheme(IntPtr hWnd, string AppID, string ClassID);  
  90.         #endregion  
  91.      
  92.         #region User32.dll functions  
  93.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  94.             public static extern IntPtr GetDC(IntPtr hWnd);  
  95.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  96.             public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);  
  97.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  98.             static public extern IntPtr GetDesktopWindow();  
  99.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  100.             static public extern bool ShowWindow(IntPtr hWnd, short State);  
  101.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  102.             static public extern bool UpdateWindow(IntPtr hWnd);  
  103.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  104.             static public extern bool SetForegroundWindow(IntPtr hWnd);  
  105.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  106.             static public extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, uint flags);  
  107.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  108.             static public extern bool OpenClipboard(IntPtr hWndNewOwner);  
  109.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  110.             static public extern bool CloseClipboard();  
  111.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  112.             static public extern bool EmptyClipboard();  
  113.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  114.             static public extern IntPtr SetClipboardData( uint Format, IntPtr hData);  
  115.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  116.             static public extern bool GetMenuItemRect(IntPtr hWnd, IntPtr hMenu, uint Item, ref RECT rc);  
  117.         [DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]  
  118.             public static extern IntPtr GetParent(IntPtr hWnd);  
  119.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  120.             public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);  
  121.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  122.             public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);  
  123.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  124.             public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref RECT lParam);  
  125.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  126.             public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref POINT lParam);  
  127.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  128.             public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TBBUTTON lParam);  
  129.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  130.             public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TBBUTTONINFO lParam);  
  131.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  132.             public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref REBARBANDINFO lParam);  
  133.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  134.             public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TVITEM lParam);  
  135.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  136.             public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref LVITEM lParam);  
  137.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  138.             public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref HDITEM lParam);  
  139.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  140.             public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref HD_HITTESTINFO hti);  
  141.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  142.             public static extern IntPtr PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);  
  143.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  144.             public static extern IntPtr SetWindowsHookEx(int hookid, HookProc pfnhook, IntPtr hinst, int threadid);  
  145.         [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]  
  146.             public static extern bool UnhookWindowsHookEx(IntPtr hhook);  
  147.         [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]  
  148.             public static extern IntPtr CallNextHookEx(IntPtr hhook, int code, IntPtr wparam, IntPtr lparam);  
  149.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  150.             public static extern IntPtr SetFocus(IntPtr hWnd);  
  151.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  152.             public extern static int DrawText(IntPtr hdc, string lpString, int nCount, ref RECT lpRect, int uFormat);  
  153.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  154.             public extern static IntPtr SetParent(IntPtr hChild, IntPtr hParent);  
  155.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  156.             public extern static IntPtr GetDlgItem(IntPtr hDlg, int nControlID);  
  157.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  158.             public extern static int GetClientRect(IntPtr hWnd, ref RECT rc);  
  159.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  160.             public extern static int InvalidateRect(IntPtr hWnd,  IntPtr rect, int bErase);  
  161.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  162.             public static extern bool WaitMessage();  
  163.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  164.             public static extern bool PeekMessage(ref MSG msg, int hWnd, uint wFilterMin, uint wFilterMax, uint wFlag);  
  165.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  166.             public static extern bool GetMessage(ref MSG msg, int hWnd, uint wFilterMin, uint wFilterMax);  
  167.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  168.             public static extern bool TranslateMessage(ref MSG msg);  
  169.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  170.             public static extern bool DispatchMessage(ref MSG msg);  
  171.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  172.             public static extern IntPtr LoadCursor(IntPtr hInstance, uint cursor);  
  173.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  174.             public static extern IntPtr SetCursor(IntPtr hCursor);  
  175.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  176.             public static extern IntPtr GetFocus();  
  177.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  178.             public static extern bool ReleaseCapture();  
  179.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  180.             public static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);  
  181.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  182.             public static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);  
  183.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  184.             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);  
  185.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  186.             public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);  
  187.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  188.             public static extern bool ClientToScreen(IntPtr hWnd, ref POINT pt);  
  189.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  190.             public static extern bool TrackMouseEvent(ref TRACKMOUSEEVENTS tme);  
  191.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  192.             public static extern bool SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);  
  193.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  194.             public static extern ushort GetKeyState(int virtKey);  
  195.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  196.             public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);  
  197.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  198.             public static extern int GetClassName(IntPtr hWnd,  out STRINGBUFFER ClassName, int nMaxCount);  
  199.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  200.             public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);  
  201.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  202.             public static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hRegion, uint flags);  
  203.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  204.             public static extern IntPtr GetWindowDC(IntPtr hWnd);  
  205.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  206.             public static extern int FillRect(IntPtr hDC, ref RECT rect, IntPtr hBrush);  
  207.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  208.             public static extern int GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT wp);  
  209.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  210.             public static extern int SetWindowText(IntPtr hWnd, string text);  
  211.         [DllImport("User32.dll", CharSet=CharSet.Auto)]  
  212.             public static extern int GetWindowText(IntPtr hWnd, out STRINGBUFFER text, int maxCount);  
  213.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  214.             static public extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);   
  215.         [DllImport("user32.dll", CharSet=CharSet.Auto)]   
  216.             static public extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);   
  217.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  218.             static public extern int ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);  
  219.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  220.             static public extern int GetSystemMetrics(int nIndex);  
  221.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  222.             static public extern int SetScrollInfo(IntPtr hwnd,  int bar, ref SCROLLINFO si, int fRedraw);  
  223.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  224.             public static extern int ShowScrollBar(IntPtr hWnd, int bar,  int show);  
  225.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  226.             public static extern int EnableScrollBar(IntPtr hWnd, uint flags, uint arrows);  
  227.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  228.             public static extern int BringWindowToTop(IntPtr hWnd);  
  229.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  230.             public static extern int GetScrollInfo(IntPtr hwnd, int bar, ref SCROLLINFO si);  
  231.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  232.         static public extern int ScrollWindowEx(IntPtr hWnd, int dx, int dy,   
  233.             ref RECT rcScroll, ref RECT rcClip, IntPtr UpdateRegion, ref RECT rcInvalidated, uint flags);  
  234.         [DllImport("user32.dll", CharSet=CharSet.Auto)]  
  235.             public static extern int IsWindow(IntPtr hWnd);  
  236.         [DllImport("user32",CharSet=CharSet.Auto)]   
  237.             public static extern int GetKeyboardState(byte[] pbKeyState);  
  238.         [DllImport("user32")]   
  239.         public static extern int ToAscii(int uVirtKey, //[in] Specifies the virtual-key code to be translated.   
  240.             int uScanCode, // [in] Specifies the hardware scan code of the key to be translated. The high-order bit of this value is set if the key is up (not pressed).   
  241.             byte[] lpbKeyState, // [in] Pointer to a 256-byte array that contains the current keyboard state. Each element (byte) in the array contains the state of one key. If the high-order bit of a byte is set, the key is down (pressed). The low bit, if set, indicates that the key is toggled on. In this function, only the toggle bit of the CAPS LOCK key is relevant. The toggle state of the NUM LOCK and SCROLL LOCK keys is ignored.  
  242.             byte[] lpwTransKey, // [out] Pointer to the buffer that receives the translated character or characters.   
  243.             int fuState); // [in] Specifies whether a menu is active. This parameter must be 1 if a menu is active, or 0 otherwise.  
  244.         #endregion  
  245.  
  246.         #region Common Controls functions  
  247.         [DllImport("comctl32.dll")]  
  248.             public static extern bool InitCommonControlsEx(INITCOMMONCONTROLSEX icc);  
  249.         [DllImport("comctl32.dll")]  
  250.             public static extern bool InitCommonControls();  
  251.         [DllImport("comctl32.dll", EntryPoint="DllGetVersion")]  
  252.             public extern static int GetCommonControlDLLVersion(ref DLLVERSIONINFO dvi);  
  253.         [DllImport("comctl32.dll")]  
  254.             public static extern IntPtr ImageList_Create(int width, int height, uint flags, int count, int grow);  
  255.         [DllImport("comctl32.dll")]  
  256.             public static extern bool ImageList_Destroy(IntPtr handle);  
  257.         [DllImport("comctl32.dll")]  
  258.             public static extern int ImageList_Add(IntPtr imageHandle, IntPtr hBitmap, IntPtr hMask);  
  259.         [DllImport("comctl32.dll")]  
  260.             public static extern bool ImageList_Remove(IntPtr imageHandle, int index);  
  261.         [DllImport("comctl32.dll")]  
  262.             public static extern bool ImageList_BeginDrag(IntPtr imageHandle, int imageIndex, int xHotSpot, int yHotSpot);  
  263.         [DllImport("comctl32.dll")]  
  264.             public static extern bool ImageList_DragEnter(IntPtr hWndLock, int x, int y);  
  265.         [DllImport(以上是关于C#可以直接调用的Win32API(和VCL做的整理工作非常类似)的主要内容,如果未能解决你的问题,请参考以下文章

    C#调用Win32 API 的方法

    C#调用Win32 api时的内存操作

    如何使用 C# 拦截 Win32 API 调用?

    c# 调用 win32 API的 SendMessage 函数 ,里面的属性用法?

    python可以直接调用win32的api吗

    python可以直接调用win32的api吗