http://www.pinvoke.net/
磐实文章站(首页)首页 >Visual Basic软件开发资料 > API 函数
http://www.panshsoft.com/Sort_VB/API_fun/
GetWindowRect用法
http://blog.csdn.net/coolszy/article/details/5601455
函数功能:该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。
函数原型:BOOL GetWindowRect(HWND hWnd,LPRECTlpRect);
参数:
hWnd:窗口句柄。
lpRect:指向一个RECT结构的指针,该结构接收窗口的左上角和右下角的屏幕坐标。
返回值:如果函数成功,返回值为非零:如果函数失败,返回值为零。若想获得更多错误信息,请调用GetLastError函数。
C#中使用该函数首先导入命名空间:
然后写API引用部分的代码,放入 class 内部
这个函数有两个个参数,第一个参数是指定窗口句柄;第二个参数接收窗口的左上角和右下角的屏幕坐标,它是Rect结构。Rect结构定义如下:
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
演示代码:
- IntPtr hwnd = FindWindow("","计算器");
- Rect rect = new Rect();
- GetWindowRect(hwnd, out lpRect);
SetCursorPos用法
http://blog.csdn.net/dangdaa/article/details/7001814
http://blog.csdn.net/coolszy/article/details/5608745
函数原型:BOOL SetCursorPOS(int X,int Y);
参数:
X:指定光标的新的X坐标,以屏幕坐标表示。
Y:指定光标的新的Y坐标,以屏幕坐标表示。
返回值:如果成功,返回非零值;如果失败,返回值是零,若想获得更多错误信息,请调用GetLastError函数。
备注:该光标是共享资源,仅当该光标在一个窗口的客户区域内时它才能移动该光标。
C#中使用该函数首先导入命名空间:
- using System.Runtime.InteropServices;
然后写API引用部分的代码,放入 class 内部
- [DllImport("user32.dll", EntryPoint ="SetCursorPos")]
- private staticexternint SetCursorPos(int x,int y);
这个函数有两个个参数,第一个参数是指定光标的新的X坐标;第二个参数是指定光标的新的Y坐标。例如:
- SetCursorPos(100, 100);
【声明】
vb
Public Declare Function OffsetRect Lib "user32" Alias "OffsetRect" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long
vb.net
Public Declare Function OffsetRect Lib "user32" Alias "OffsetRect" (lpRect As RECT, ByVal x As Integer, ByVal y As Integer) As Integer
c#
[DllImport("user32", EntryPoint="OffsetRect")]
public static extern int OffsetRect (
ref RECT lpRect,
int x,
int y
);
【说明】
该函数通过应用一个指定的偏移,从而让矩形移动起来。x会添加到右侧和左侧区域。y添加到顶部和底部区域。偏移方向则取决于参数是正数还是负数,以及采用的是什么坐标系统
【返回值】
Long,非零表示成功,零表示失败。会设置GetLastError
【参数表】
lpRect --------- RECT,欲移动的矩形
x -------------- Long,水平偏移量
y -------------- Long,垂直偏移量
C#调用WindowsAPI之GetClientRect用法
http://www.cnblogs.com/zhuiyi/archive/2012/07/17/2595309.html
函数原型:BOOL GetClientRect(HWND hWnd,LPRECT lpRect);
参数:
GetLastError 函数。
备注:Windows CE:命令条包含在客户区中。
速查:Windows NT: 3.1以上版本:Windows:95以上版本: Windows CE:1.0以上版本:头文件:winuser.h;库文件:user32.lib
第一步,声明结构
public struct RECT
{
public uint Left;
public uint Top;
public uint Right;
public uint Bottom;
}
第二步,导入user32.dll
public static extern bool GetClientRect(
IntPtr hwnd,
out RECT lpRect
);
第三步,设定一个RECT
第四步,应用
注:GetClientRect 函数所取得的Left 及Top 值是0;Right 及Bottom 值是width 和height。也就是说,Right的就是宽度,Bottom的值就是高度了
ClientToScreen
http://www.panshsoft.com/Sort_VB/API_fun/Control_msg/2012-04-26/923.php
vb
Public Declare Function ClientToScreen Lib "user32" Alias "ClientToScreen" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
vb.net
Public Declare Function ClientToScreen Lib "user32" Alias "ClientToScreen" (ByVal hwnd As Integer, lpPoint As POINTAPI) As Integer
c#
[DllImport("user32", EntryPoint="ClientToScreen")]
public static extern int ClientToScreen (
int hwnd,
ref POINTAPI lpPoint
);
ClientToScreen是把坐标从当前窗体转化成全屏幕的!!!
ScreenToClient是把屏幕坐标转化成相对当前窗体的坐标!!!!
【说明】
判断窗口内以客户区坐标表示的一个点的屏幕坐标
【返回值】
Long,非零表示成功,零表示失败
【参数表】
hwnd ----------- Long,判断客户区坐标时那个窗口的句柄
lpPoint -------- POINTAPI,用hwnd窗口的客户区坐标表示的点,这个参数会包含屏幕坐标系统中相同的点
[DllImport("user32", EntryPoint="ScreenToClient")]
public static extern int ScreenToClient (
int hwnd,
ref POINTAPI lpPoint
);
【说明】
判断屏幕上一个指定点的客户区坐标
【返回值】
Long,非零表示成功,零表示失败
【参数表】
hwnd ----------- Long,一个窗口的句柄,该窗口定义了要使用的客户区坐标系统
lpPoint -------- POINTAPI,屏幕坐标系统中包含了屏幕点的结构。这个函数会随同相应的客户区坐标(由hwnd决定)载入结构
http://www.byywee.com/page/M0/S443/443605.html
/// <summary>
/// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在GDI函数中使用该句柄来在设备上下文环境中绘图。hWnd:设备上下文环境被检索的窗口的句柄
/// </summary>
[DllImport("user32.dll", CharSet= CharSet.Auto)]
publicstaticextern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll", ExactSpelling=true, CharSet= CharSet.Auto)]
/// <summary>
/// 该函数获得一个指定子窗口的父窗口句柄。
/// </summary>
publicstaticextern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int which);
[DllImport("user32.dll")]
public static extern void SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
int X, int Y, int width, int height, uint flags);
//这个API作用是获取窗口的属性
[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
static extern int GetWindowLong(HandleRef hWnd, int nIndex);
//这个API是专门设置窗口的属性的
[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
//获取窗体属性句柄
int windowLong = (GetWindowLong(new HandleRef(form, form.Handle), -16));
//设置窗体属性句柄
SetWindowLong(new HandleRef(form, form.Handle), -16, windowLong | WndMessage.WS_SYSMENU | WndMessage.WS_MINIMIZEBOX);
//注意,这里是重点:WS_SYSMENU允许有系统菜单 WS_MINIMIZEBOX:可以最大最小化
//这两个参数是上面那个类里提供的
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
private static extern long GetWindowLong(IntPtr hwnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);
//private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
private static extern bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam);
/// <summary>
/// MulDiv(a, b, c) 就是计算 a * b / c ,不过在 a * b > 2^32 仍保证结果正确
/// </summary>
/// <param name="nNumber"></param>
/// <param name="nNumerator"></param>
/// <param name="nDenominator"></param>
/// <returns></returns>
[DllImport("kernel32")]
public static extern int MulDiv(int nNumber, int nNumerator, int nDenominator);
[DllImport("gdi32", EntryPoint="CreateCompatibleDC")]
public static extern int CreateCompatibleDC (
int hdc
);
【说明】
创建一个与特定设备场景一致的内存设备场景
【返回值】
Long,新设备场景句柄,若出错则为零
【参数表】
hdc ------------ Long,设备场景句柄。新的设备场景将与它一致。也可能为0以创建一个与屏幕一致的设备场景
【其它说明】
在绘制之前,先要为该设备场景选定一个位图。不再需要时,该设备场景可用DeleteDC函数删除。删除前,其所有对象应回复初始状态
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateSolidBrush(int crColor);
[DllImport("gdi32.dll")]
public static externbool ExtFloodFill(IntPtr hdc,int nXStart, int nYStart,
int crColor,uint fuFillType);
[DllImport("gdi32.dll")]
public static externbool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static externint GetPixel(IntPtr hdc,int x, int y);
//函数功能:该函数装载图标,光标,或位图。
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr LoadImage(IntPtr hinst,[MarshalAs(UnmanagedType.LPTStr)]string lpszName, uint uType,int cxDesired, int cyDesired, uint fuLoad);
LPTSTR对应StringBuilder,String,Byte[],char[]都行,后两者在知道长度的情况下是没问题的,你这没有,但应该也行,写法如下:
StringBuilder:
[DllImport("<Unknown>", EntryPoint="SCardListReadersW")]
public static extern int SCardListReadersW(uint hContext, [In,MarshalAs(UnmanagedType.LPWStr)] string mszGroups, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder mszReaders, ref uint pcchReaders) ;
string:
[DllImport("<Unknown>", EntryPoint="SCardListReadersW")]
public static extern int SCardListReadersW(uint hContext, [In, MarshalAs(UnmanagedType.LPWStr)] string mszGroups,
[Out,MarshalAs(UnmanagedType.LPWStr)]string mszReaders, ref uint pcchReaders) ;
"<Unknown>", 换成你的DLL
In,可以省略
c#
[DllImport("user32", EntryPoint="IsRectEmpty")]
public static extern int IsRectEmpty (
ref RECT lpRect
);
【说明】
判断一个矩形是否为空
【返回值】
Long,非零表示成功,零表示失败。会设置GetLastError
【参数表】
lpRect --------- RECT,要检查的矩形
GetScrollInfo获取控件滚动条信息
SCROLLINFO si = new SCROLLINFO(); si.cbSize = (uint)Marshal.SizeOf(si); si.fMask = (uint)ScrollInfoMask.SIF_ALL; WIN32.GetScrollInfo(this.Handle, (int)SBOrientation.SB_VERT, ref si) [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetScrollInfo(IntPtr hwnd, int fnBar, ref SCROLLINFO lpsi); [Serializable, StructLayout(LayoutKind.Sequential)] public class SCROLLINFO { public uint cbSize; public uint fMask; public int nMin; public int nMax; public uint nPage; public int nPos; public int nTrackPos; } public enum ScrollInfoMask : uint { SIF_RANGE = 0x1, SIF_PAGE = 0x2, SIF_POS = 0x4, SIF_DISABLENOSCROLL = 0x8, SIF_TRACKPOS = 0x10, SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS), }
c#
[DllImport("gdi32", EntryPoint="SetBkColor")]
public static extern int SetBkColor (
int hdc,
int crColor
);
【说明】
为指定的设备场景设置背景颜色。背景颜色用于填充阴影刷子、虚线画笔以及字符(如背景模式为OPAQUE)中的空隙。也在位图颜色转换期间使用。参考SetBkMode
【返回值】
Long,前一个背景色,CLR_INVALID表示出错
【参数表】
hdc ------------ Long,设备场景的句柄
crColor -------- Long,新背景颜色的RGB颜色值
[DllImport("gdi32", EntryPoint="ExtTextOut")]
public static extern int ExtTextOut (
int hdc,
int x,
int y,
int wOptions,
ref Rect lpRect,
string lpString,
int nCount,
ref int lpDx
);
【说明】
经过扩展的文本描绘函数。也请参考SetTextAlign函数
【返回值】
Long,非零表示成功,零表示失败。会设置GetLastError
【参数表】
hdc ------------ Long,设备场景的句柄
x,y ------------ Long,以逻辑坐标表示的一个点,指定了绘图起点
wOptions ------- Long,下述标志常数的任意组合
ETO_CLIPPED
将文本剪切出指定的矩形
ETO_GLPYH_INDEX
ETO_CLIPPED:正文将裁剪到矩形中。
ETO_GLYPH_INDEX:LpString指向由GetCharacterPlacement返回的数组,如果没有进一步的特殊语言处理的要求,则此数组直接由GDI解析,仅对字体应用符号索引,但此标志可用于位图和向量字体,以表示不必做进一步的语言处理,GDI应用直接处理此字符串。
ETO_RTLREADING:在Middle_Eastern Windows中如果指定了此值,且Hebrew或Arabic字体被选进设备环境,则此字符串用以从右到左的阅读顺序来输出。如果没有指定此值,则字符串以从左到右的顺序输出。在SetTextAlign中设置TA_RTLREADING值可获得同样的效果。为向后兼容,此值作为保留值。
ETO_GLYPH_INDEX和ETO_RTLREADING值不能在一起使用。因为ETO_GLYPH_INDEX表示所有的语言处理已经完成,函数就会忽略被指定的ETO_RTLREADING值。
lpRect --------- Rect,指定一个矩形,用于对文本进行格式化处理。可指定长整数0,在不用矩形区域的前提下描绘文本
lpString
String,欲描绘的字串
lpString是一个字样索引表。参考对GetCharacterPlacement函数的说明。只适用于Win95 - ETO_OPAQUE
在正式描绘文本前,用当前的背景色填充矩形
nCount --------- Long,字串中要显示出来的字符数
lpDx ----------- Long,如果不是零,这个参数就代表指向一个Long值数组的指针。该数组对每一对字符的间距进行了说明(采用逻辑单位)。其中第一个条目是第一和第二个字符的间距;第二个条目是第二和第三个字符的间距;以此类推。如果为零,函数就使用字体的默认间距设置
FindWindow 用来根据类名和窗口名来得到窗口句柄的。但是这个函数不能查找子窗口,也不区分大小写。
如果要从一个窗口的子窗口中查找需要使用FindWindowEX。
1.在C#中使用方法如下:
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("User32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
IntPtr hWnd = FindWindow(null, "test Demo");
这样会查找所有title是"test Demo"的窗口。
[DllImport("user32", EntryPoint="WindowFromPoint")]
public static extern int WindowFromPoint (
int xPoint,
int yPoint
);
【说明】
返回包含了指定点的窗口的句柄。忽略屏蔽、隐藏以及透明窗口
【返回值】
Long,包含了指定点的窗口的句柄。如指定的点处没有窗口存在,则返回零
【参数表】
xPoint --------- Long,x点值
yPoint --------- Long,y点值
以下以GDI32中關於Bitmaps的相關函數為例
//Platform Invoke Data Types
// http://msdn.microsoft.com/en-us/library/ac7ay120.aspx
//
using System;
using System.Runtime.InteropServices;
//alias Win32 types to Framework types
//IntPtr
using HANDLE = System.IntPtr;
using HDC = System.IntPtr;
using HBITMAP = System.IntPtr;
using LPVOID = System.IntPtr;
using HINSTANCE = System.IntPtr;
//String
using LPSTR = System.String;
using LPCSTR = System.String;
using LPWSTR = System.String;
using LPCWSTR = System.String;
using LPTSTR = System.String;
using LPCTSTR = System.String;
//UInt32
using DWORD = System.UInt32;
using UINT = System.UInt32;
using ULONG = System.UInt32;
using COLORREF = System.UInt32;
//Int32
using INT = System.Int32;
using LONG = System.Int32;
using BOOL = System.Int32;
//Other
using BYTE = System.Byte;
using SHORT = System.Int16;
using WORD = System.UInt16;
using CHAR = System.Char;
using FLOAT = System.Single;
using DOUBLE = System.Double;
namespace Win32
{
public class Bitmaps
{
#region Bitmaps (資料結構)
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPFILEHEADER
{
public ushort bfType;
public int bfSize;
public ushort bfReserved1;
public ushort bfReserved2;
public uint bfOffBits;
}
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPINFOHEADER
{
public uint biSize;
public int biWidth;
public int biHeight;
public ushort biPlanes;
public ushort biBitCount;
public uint biCompression;
public int biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public uint biClrUsed;
public uint biClrImportant;
}
[StructLayout(LayoutKind.Sequential)]
public struct RGBQUAD
{
public byte Red;
public byte Green;
public byte Blue;
public byte reserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPINFO
{
public BITMAPINFOHEADER header;
[MarshalAs(UnmanagedType.ByValArray,SizeConst=1)]
public RGBQUAD[] bmiColors;
}
#endregion Bitmaps (資料結構)
#region Bitmaps (函數)
/*
//TODO: AlphaBlend
/// <summary>
/// Displays a bitmap with transparent or semitransparent pixels.
/// </summary>
/// <param name="hdcDest"></param>
/// <param name="nXOriginDest"></param>
/// <param name="nYOriginDest"></param>
/// <param name="nWidthDest"></param>
/// <param name="nHeightDest"></param>
/// <param name="hdcSrc"></param>
/// <param name="nXOriginSrc"></param>
/// <param name="nYOriginSrc"></param>
/// <param name="nWidthSrc"></param>
/// <param name="nHeightSrc"></param>
/// <param name="blendFunction"></param>
/// <returns></returns>
/// <remarks>System.Drawing.Graphics.DrawImage</remarks>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
BOOL AlphaBlend(
HDC hdcDest, // handle to destination DC
int nXOriginDest, // x-coord of upper-left corner
int nYOriginDest, // y-coord of upper-left corner
int nWidthDest, // destination width
int nHeightDest, // destination height
HDC hdcSrc, // handle to source DC
int nXOriginSrc, // x-coord of upper-left corner
int nYOriginSrc, // y-coord of upper-left corner
int nWidthSrc, // source width
int nHeightSrc, // source height
BLENDFUNCTION blendFunction // alpha-blending function
);
*/
/// <summary>
/// BitBlt Performs a bit-block transfer.
/// </summary>
/// <param name="hdcDest"></param>
/// <param name="nXDest"></param>
/// <param name="nYDest"></param>
/// <param name="nWidth"></param>
/// <param name="nHeight"></param>
/// <param name="hdcSrc"></param>
/// <param name="nXSrc"></param>
/// <param name="nYSrc"></param>
/// <param name="dwRop"></param>
/// <returns></returns>
/// <remarks>System.Drawing.Graphics.DrawImage</remarks>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
BOOL BitBlt(
HDC hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
DWORD dwRop // raster operation code
);
/// <summary>
/// Creates a bitmap.
/// </summary>
/// <param name="nWidth"></param>
/// <param name="nHeight"></param>
/// <param name="cPlanes"></param>
/// <param name="cBitsPerPel"></param>
/// <param name="lpvBits"></param>
/// <returns></returns>
/// <remarks>System.Drawing.Bitmap constructor</remarks>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
HBITMAP CreateBitmap(
int nWidth, // bitmap width, in pixels
int nHeight, // bitmap height, in pixels
UINT cPlanes, // number of color planes
UINT cBitsPerPel, // number of bits to identify color
LPVOID lpvBits // color data array
);
//TODO: CreateBitmapIndirect
/*
/// <summary>
/// Creates a bitmap.
/// </summary>
/// <param name="lpbm"></param>
/// <returns></returns>
HBITMAP CreateBitmapIndirect(
LPBITMAP lpbm // bitmap data
);
*/
/// <summary>
/// Creates a bitmap compatible with a device.
/// </summary>
/// <param name="hdc"></param>
/// <param name="nWidth"></param>
/// <param name="nHeight"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
HBITMAP CreateCompatibleBitmap(
HDC hdc, // handle to DC
int nWidth, // width of bitmap, in pixels
int nHeight // height of bitmap, in pixels
);
/// <summary>
/// Creates a device-dependent bitmap (DDB) from a DIB.
/// </summary>
/// <param name="hdc"></param>
/// <param name="bmih"></param>
/// <param name="fdwInit"></param>
/// <param name="lpbInit"></param>
/// <param name="bmi"></param>
/// <param name="fuUsage"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern ///
HBITMAP CreateDIBitmap(
HDC hdc, // handle to DC
ref BITMAPINFOHEADER bmih, // bitmap data
DWORD fdwInit, // initialization option
LPVOID lpbInit, // initialization data
ref BITMAPINFO bmi, // color-format data
UINT fuUsage // color-data usage
);
//TODO: CreateDIBSection
/*
/// <summary>
/// Creates a DIB that applications can write to directly.
/// </summary>
/// <param name="hdc"></param>
/// <param name="BITMAPINFO"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
HBITMAP CreateDIBSection(
HDC hdc, // handle to DC
CONST BITMAPINFO *pbmi, // bitmap data
UINT iUsage, // data type indicator
VOID **ppvBits, // bit values
HANDLE hSection, // handle to file mapping object
DWORD dwOffset // offset to bitmap bit values
);
*/
/// <summary>
/// Fills an area of the display surface with the current brush.
/// </summary>
/// <param name="hdc"></param>
/// <param name="nXStart"></param>
/// <param name="nYStart"></param>
/// <param name="crColor"></param>
/// <param name="fuFillType"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
BOOL ExtFloodFill(
HDC hdc, // handle to DC
int nXStart, // starting x-coordinate
int nYStart, // starting y-coordinate
COLORREF crColor, // fill color
UINT fuFillType // fill type
);
/// <summary>
/// Gets the dimensions of a bitmap.
/// </summary>
/// <param name="hBitmap"></param>
/// <param name="Dimension"></param>
/// <returns></returns>
/// <remarks>System.Drawing.Bitmap.Size</remarks>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
BOOL GetBitmapDimensionEx(
HBITMAP hBitmap, // handle to bitmap
ref Int32 Dimension // dimensions
);
/*
//TODO: GetDIBColorTable
/// <summary>
/// Retrieves RGB color values from a DIB section bitmap.
/// </summary>
/// <param name="hdc"></param>
/// <param name="uStartIndex"></param>
/// <param name="cEntries"></param>
/// <param name="pColors"></param>
/// <returns></returns>
/// <remarks>System.Drawing.Bitmap.Palette</remarks>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
UINT GetDIBColorTable(
HDC hdc, // handle to DC
UINT uStartIndex, // color table index of first entry
UINT cEntries, // number of entries to retrieve
RGBQUAD* pColors // array of color table entries
);
*/
/// <summary>
/// Copies a bitmap into a buffer.
/// </summary>
/// <param name="hdc"></param>
/// <param name="hbmp"></param>
/// <param name="uStartScan"></param>
/// <param name="cScanLines"></param>
/// <param name="lpvBits"></param>
/// <param name="bi"></param>
/// <param name="uUsage"></param>
/// <returns></returns>
/// <remarks>System.Drawing.Bitmap.LockBits</remarks>
int GetDIBits(
HDC hdc, // handle to DC
HBITMAP hbmp, // handle to bitmap
UINT uStartScan, // first scan line to set
UINT cScanLines, // number of scan lines to copy
LPVOID lpvBits, // array for bitmap bits
ref BITMAPINFO bi, // bitmap data buffer
UINT uUsage // RGB or palette index
);
/// <summary>
/// Gets the RGB color value of the pixel at a given coordinate.
/// </summary>
/// <param name="hdc"></param>
/// <param name="nXPos"></param>
/// <param name="nYPos"></param>
/// <returns></returns>
/// <remarks>System.Drawing.Bitmap.GetPixel</remarks>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
COLORREF GetPixel(
HDC hdc, // handle to DC
int nXPos, // x-coordinate of pixel
int nYPos // y-coordinate of pixel
);
/// <summary>
/// Gets the current stretching mode.
/// </summary>
/// <param name="hdc"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
int GetStretchBltMode(
HDC hdc // handle to DC
);
/*
//TODO: GradientFill
/// <summary>
/// Fills rectangle and triangle structures.
/// </summary>
/// <param name="hdc"></param>
/// <param name="pVertex"></param>
/// <param name="dwNumVertex"></param>
/// <param name="pMesh"></param>
/// <param name="dwNumMesh"></param>
/// <param name="dwMode"></param>
/// <returns></returns>
/// <remarks>System.Drawing.Graphics.FillRectangle(Drawing2D.LinearGradiantBrush, Rectangle)</remarks>
BOOL GradientFill(
HDC hdc, // handle to DC
PTRIVERTEX pVertex, // array of vertices
ULONG dwNumVertex, // number of vertices
PVOID pMesh, // array of gradients
ULONG dwNumMesh, // size of gradient array
ULONG dwMode // gradient fill mode
);
*/
/// <summary>
/// Loads a bitmap from a module's executable file.
/// </summary>
/// <param name="hInstance"></param>
/// <param name="lpBitmapName"></param>
/// <returns></returns>
/// <remarks>
/// hInstance = System.Diagnostics.Process.GetCurrentProcess().Handle;
///
/// System.Drawing.Bitmap.Bitmap(Type, String)
/// </remarks>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
HBITMAP LoadBitmap(
HINSTANCE hInstance, // handle to application instance
LPCTSTR lpBitmapName // name of bitmap resource
);
/// <summary>
/// Combines the color data in the source and destination bitmaps.
/// </summary>
/// <param name="hdcDest"></param>
/// <param name="nXDest"></param>
/// <param name="nYDest"></param>
/// <param name="nWidth"></param>
/// <param name="nHeight"></param>
/// <param name="hdcSrc"></param>
/// <param name="nXSrc"></param>
/// <param name="nYSrc"></param>
/// <param name="hbmMask"></param>
/// <param name="xMask"></param>
/// <param name="yMask"></param>
/// <param name="dwRop"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
BOOL MaskBlt(
HDC hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of source and destination
int nHeight, // height of source and destination
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coord of upper-left corner of source
int nYSrc, // y-coord of upper-left corner of source
HBITMAP hbmMask, // handle to monochrome bit mask
int xMask, // horizontal offset into mask bitmap
int yMask, // vertical offset into mask bitmap
DWORD dwRop // raster operation code
);
//TODO: PlgBlt
/*
/// <summary>
/// Performs a bit-block transfer.
/// </summary>
/// <param name="hdcDest"></param>
/// <param name="POINT"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
BOOL PlgBlt(
HDC hdcDest, // handle to destination DC
CONST POINT *lpPoint, // destination vertices
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coord of source upper-left corner
int nYSrc, // y-coord of source upper-left corner
int nWidth, // width of source rectangle
int nHeight, // height of source rectangle
HBITMAP hbmMask, // handle to bitmask
int xMask, // x-coord of bitmask upper-left corner
int yMask // y-coord of bitmask upper-left corner
);
*/
/// <summary>
/// Sets the preferred dimensions to a bitmap.
/// </summary>
/// <param name="hBitmap"></param>
/// <param name="nWidth"></param>
/// <param name="nHeight"></param>
/// <param name="lpSize"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
BOOL SetBitmapDimensionEx(
HBITMAP hBitmap, // handle to bitmap
int nWidth, // bitmap width in .01-mm units
int nHeight, // bitmap height in .01-mm units
ref Int32 lpSize // original dimensions
);
//TODO: SetDIBColorTable
/*
/// <summary>
/// Sets RGB values in a DIB.
/// </summary>
/// <param name="hdc"></param>
/// <param name="uStartIndex"></param>
/// <param name="cEntries"></param>
/// <param name="RGBQUAD"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
UINT SetDIBColorTable(
HDC hdc, // handle to DC
UINT uStartIndex, // color table index of first entry
UINT cEntries, // number of color table entries
CONST RGBQUAD *pColors // array of color table entries
);
*/
/// <summary>
/// Sets the pixels in a bitmap using color data from a DIB.
/// </summary>
/// <param name="hdc"></param>
/// <param name="hbmp"></param>
/// <param name="uStartScan"></param>
/// <param name="cScanLines"></param>
/// <param name="lpvBits"></param>
/// <param name="lpbmi"></param>
/// <param name="fuColorUse"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
int SetDIBits(
HDC hdc, // handle to DC
HBITMAP hbmp, // handle to bitmap
UINT uStartScan, // starting scan line
UINT cScanLines, // number of scan lines
LPVOID lpvBits, // array of bitmap bits
ref BITMAPINFO lpbmi, // bitmap data
UINT fuColorUse // type of color indexes to use
);
/// <summary>
/// Sets the pixels in a rectangle using color data from a DIB.
/// </summary>
/// <param name="hdc"></param>
/// <param name="XDest"></param>
/// <param name="YDest"></param>
/// <param name="dwWidth"></param>
/// <param name="dwHeight"></param>
/// <param name="XSrc"></param>
/// <param name="YSrc"></param>
/// <param name="uStartScan"></param>
/// <param name="cScanLines"></param>
/// <param name="lpvBits"></param>
/// <param name="bmi"></param>
/// <param name="fuColorUse"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
int SetDIBitsToDevice(
HDC hdc, // handle to DC
int XDest, // x-coord of destination upper-left corner
int YDest, // y-coord of destination upper-left corner
DWORD dwWidth, // source rectangle width
DWORD dwHeight, // source rectangle height
int XSrc, // x-coord of source lower-left corner
int YSrc, // y-coord of source lower-left corner
UINT uStartScan, // first scan line in array
UINT cScanLines, // number of scan lines
LPVOID lpvBits, // array of DIB bits
ref BITMAPINFO bmi, // bitmap information
UINT fuColorUse // RGB or palette indexes
);
/// <summary>
/// Sets the color for a pixel.
/// </summary>
/// <param name="hdc"></param>
/// <param name="X"></param>
/// <param name="Y"></param>
/// <param name="crColor"></param>
/// <returns></returns>
/// <remarks>System.Drawing.Bitmap.SetPixel</remarks>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
COLORREF SetPixel(
HDC hdc, // handle to DC
int X, // x-coordinate of pixel
int Y, // y-coordinate of pixel
COLORREF crColor // pixel color
);
/// <summary>
/// Sets a pixel to the best approximation of a color.
/// </summary>
/// <param name="hdc"></param>
/// <param name="X"></param>
/// <param name="Y"></param>
/// <param name="crColor"></param>
/// <returns></returns>
/// <remarks>System.Drawing.Bitmap.SetPixel(…, Graphics.GetNearestColor(Color))</remarks>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
BOOL SetPixelV(
HDC hdc, // handle to device context
int X, // x-coordinate of pixel
int Y, // y-coordinate of pixel
COLORREF crColor // new pixel color
);
/// <summary>
/// Sets the bitmap stretching mode.
/// </summary>
/// <param name="hdc"></param>
/// <param name="iStretchMode"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
int SetStretchBltMode(
HDC hdc, // handle to DC
int iStretchMode // bitmap stretching mode
);
/// <summary>
/// Copies a bitmap and stretches or compresses it.
/// </summary>
/// <param name="hdcDest"></param>
/// <param name="nXOriginDest"></param>
/// <param name="nYOriginDest"></param>
/// <param name="nWidthDest"></param>
/// <param name="nHeightDest"></param>
/// <param name="hdcSrc"></param>
/// <param name="nXOriginSrc"></param>
/// <param name="nYOriginSrc"></param>
/// <param name="nWidthSrc"></param>
/// <param name="nHeightSrc"></param>
/// <param name="dwRop"></param>
/// <returns></returns>
/// <remarks>System.Drawing.Graphics.DrawImage</remarks>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
BOOL StretchBlt(
HDC hdcDest, // handle to destination DC
int nXOriginDest, // x-coord of destination upper-left corner
int nYOriginDest, // y-coord of destination upper-left corner
int nWidthDest, // width of destination rectangle
int nHeightDest, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXOriginSrc, // x-coord of source upper-left corner
int nYOriginSrc, // y-coord of source upper-left corner
int nWidthSrc, // width of source rectangle
int nHeightSrc, // height of source rectangle
DWORD dwRop // raster operation code
);
/// <summary>
/// Copies the color data in a DIB.
/// </summary>
/// <param name="hdc"></param>
/// <param name="XDest"></param>
/// <param name="YDest"></param>
/// <param name="nDestWidth"></param>
/// <param name="nDestHeight"></param>
/// <param name="XSrc"></param>
/// <param name="YSrc"></param>
/// <param name="nSrcWidth"></param>
/// <param name="nSrcHeight"></param>
/// <param name="lpBits"></param>
/// <param name="lpBitsInfo"></param>
/// <param name="iUsage"></param>
/// <param name="dwRop"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
int StretchDIBits(
HDC hdc, // handle to DC
int XDest, // x-coord of destination upper-left corner
int YDest, // y-coord of destination upper-left corner
int nDestWidth, // width of destination rectangle
int nDestHeight, // height of destination rectangle
int XSrc, // x-coord of source upper-left corner
int YSrc, // y-coord of source upper-left corner
int nSrcWidth, // width of source rectangle
int nSrcHeight, // height of source rectangle
LPVOID lpBits, // bitmap bits
ref BITMAPINFO lpBitsInfo, // bitmap data
UINT iUsage, // usage options
DWORD dwRop // raster operation code
);
/// <summary>
/// Performs a bit-block transfer of color data.
/// </summary>
/// <param name="hdcDest"></param>
/// <param name="nXOriginDest"></param>
/// <param name="nYOriginDest"></param>
/// <param name="nWidthDest"></param>
/// <param name="hHeightDest"></param>
/// <param name="hdcSrc"></param>
/// <param name="nXOriginSrc"></param>
/// <param name="nYOriginSrc"></param>
/// <param name="nWidthSrc"></param>
/// <param name="nHeightSrc"></param>
/// <param name="crTransparent"></param>
/// <returns></returns>
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern
BOOL TransparentBlt(
HDC hdcDest, // handle to destination DC
int nXOriginDest, // x-coord of destination upper-left corner
int nYOriginDest, // y-coord of destination upper-left corner
int nWidthDest, // width of destination rectangle
int hHeightDest, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXOriginSrc, // x-coord of source upper-left corner
int nYOriginSrc, // y-coord of source upper-left corner
int nWidthSrc, // width of source rectangle
int nHeightSrc, // height of source rectangle
UINT crTransparent // color to make transparent
);
#endregion Bitmaps
}
}
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
namespace Desktop
{
public class Win32APICall
{
[DllImport("gdi32.dll",EntryPoint="DeleteDC")]
public static extern IntPtr DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll",EntryPoint="DeleteObject")]
public static extern IntPtr DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll",EntryPoint="BitBlt")]
public static extern bool BitBlt(IntPtr hdcDest,int nXDest,
int nYDest,int nWidth,int nHeight,IntPtr hdcSrc,
int nXSrc,int nYSrc,int dwRop);
[DllImport ("gdi32.dll",EntryPoint="CreateCompatibleBitmap")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,
int nWidth, int nHeight);
[DllImport ("gdi32.dll",EntryPoint="CreateCompatibleDC")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport ("gdi32.dll",EntryPoint="SelectObject")]
public static extern IntPtr SelectObject(IntPtr hdc,IntPtr hgdiobjBmp);
[DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll",EntryPoint="GetDC")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll",EntryPoint="GetSystemMetrics")]
public static extern int GetSystemMetrics(int nIndex);
[DllImport("user32.dll",EntryPoint="ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
public static Bitmap GetDesktop()
{
int screenX;
int screenY;
IntPtr hBmp;
IntPtr hdcScreen = GetDC(GetDesktopWindow());
IntPtr hdcCompatible = CreateCompatibleDC(hdcScreen);
screenX = GetSystemMetrics(0);
screenY = GetSystemMetrics(1);
hBmp = CreateCompatibleBitmap(hdcScreen, screenX, screenY);
if (hBmp!=IntPtr.Zero)
{
IntPtr hOldBmp = (IntPtr) SelectObject(hdcCompatible, hBmp);
BitBlt(hdcCompatible, 0, 0,screenX,screenY, hdcScreen, 0, 0,13369376);
SelectObject(hdcCompatible, hOldBmp);
DeleteDC(hdcCompatible);
ReleaseDC(GetDesktopWindow(), hdcScreen);
Bitmap bmp = System.Drawing.Image.FromHbitmap(hBmp);
DeleteObject(hBmp);
GC.Collect();
return bmp;
}
return null;
}
}
}
2、声明
在需要的应用程序声明
private Bitmap myBitmap;
3、抓取
myBitmap = Win32APICall.GetDesktop();
4、结束
Color myColor = myBitmap.GetPixel(MousePosition.X,MousePosition.Y);
label1.BackColor = myColor;
/// <summary>
/// 获得低16位值
/// </summary>
/// <param name="lParam"></param>
/// <returns></returns>
public static int GET_X_LPARAM(int lParam)
{
return (lParam & 0xffff);
}
/// <summary>
/// 获得高16位值
/// </summary>
/// <param name="lParam"></param>
/// <returns></returns>
public static int GET_Y_LPARAM(int lParam)
{
return (lParam >> 16);
}
[C#][Windows API] 常用Windows原生方法整理(Windows API)
http://www.dotblogs.com.tw/optimist9266/archive/2011/06/06/27194.aspx
1. 滑鼠相關:
mouse_event:用於模擬各種滑鼠點擊行為。(聽說可以模擬移動,但我沒成功過。)
1 |
[DllImport( "User32" )]
|
2 |
public extern static void mouse_event( int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
|
SetCursorPos:用於設定滑鼠游標在螢幕中的座標位置。
1 |
[DllImport( "User32" )]
|
2 |
public extern static void SetCursorPos( int x, int y);
|
GetCursorPos:用於取得滑鼠游標在螢幕中的座標位置。
1 |
[DllImport( "User32" )]
|
2 |
public extern static bool GetCursorPos( out Point p);
|
ShowCursor:用於設定是否顯示滑鼠游標在螢幕上。(僅作用於該執行緒上的所有視窗)
1 |
[DllImport( "User32" )]
|
2 |
public extern static int ShowCursor( bool bShow);
|
2. 視窗相關:
FindWindow:透過字串尋找目前開啟中的視窗,其標題與之符合的視窗控制代碼。
1 |
[DllImport( "USER32.DLL" , CharSet = CharSet.Unicode, ThrowOnUnmappableChar = true )]
|
2 |
public static extern IntPtr FindWindow( string lpClassName, string lpWindowName);
|
[範例:待續]
SetForegroundWindow:試著讓指定視窗控制代碼的視窗取得焦點。
1 |
[DllImport( "USER32.DLL" )]
|
2 |
public static extern bool SetForegroundWindow(IntPtr hWnd);
|
[範例:待續]
3. 鍵盤相關:
keybd_event:用於模擬鍵盤按下放開行為。
1 |
[DllImport( "user32.dll" )]
|
2 |
public static extern void keybd_event( byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
|
GetKeyState:取得鍵盤按鍵的狀態,也可用來取得NumLock、CapsLock、ScrollLock是否啟動。
1 |
[DllImport( "user32.dll" , CharSet = CharSet.Auto, ExactSpelling = true , CallingConvention = CallingConvention.Winapi)]
|
2 |
public static extern short GetKeyState( int keyCode);
|
[範例:取得鍵盤按鍵與CapsLock、NumLock、ScrollLock狀態]
4.Windows Hook相關:
SetWindowsHookEx:用來向Windows註冊Hook來接收全域事件。
1 |
[DllImport( "user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
2 |
public static extern int SetWindowsHookEx( int idHook, NativeStructs.HookProc lpfn, IntPtr hInstance, int threadId);
|
[範例:接收全域滑鼠事件(Windows Hook for Mouse)]
UnhookWindowsHookEx:用來向Window解除已經註冊的Hook。
1 |
[DllImport( "user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
2 |
public static extern bool UnhookWindowsHookEx( int idHook);
|
[範例:接收全域滑鼠事件(Windows Hook for Mouse)]
CallNextHookEx:將Hook事件傳給Windows的下一個Hook。
1 |
[DllImport( "user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
2 |
public static extern int CallNextHookEx( int idHook, int nCode, IntPtr wParam, IntPtr lParam);
|
[範例:接收全域滑鼠事件(Windows Hook for Mouse)]
5.輸入法相關:
1 |
[DllImport( "Imm32.dll" )]
|
2 |
public static extern IntPtr ImmGetContext(IntPtr hWnd);
|
1 |
[DllImport( "Imm32.dll" )]
|
2 |
public static extern IntPtr ImmAssociateContext(IntPtr hWnd, IntPtr hIMC);
|
NativeMethods類別的完整程式碼:
01 |
internal static class NativeMethods
|
02 |
{ |
03 |
#region #####Mouse#####
|
04 |
[STAThread]
|
05 |
[DllImport( "User32" )]
|
06 |
public extern static void mouse_event( int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
|
07 |
[DllImport( "User32" )]
|
08 |
public extern static void SetCursorPos( int x, int y);
|
09 |
[DllImport( "User32" )]
|
10 |
public extern static bool GetCursorPos( out Point p);
|
11 |
[DllImport( "User32" )]
|
12 |
public extern static int ShowCursor( bool bShow);
|
13 |
#endregion
|
14 |
15 |
#region #####Window#####
|
16 |
[DllImport( "USER32.DLL" , CharSet = CharSet.Unicode, ThrowOnUnmappableChar = true )]
|
17 |
public static extern IntPtr FindWindow( string lpClassName, string lpWindowName);
|
18 |
[DllImport( "USER32.DLL" )]
|
19 |
public static extern bool SetForegroundWindow(IntPtr hWnd);
|
20 |
#endregion
|
21 |
22 |
#region #####Keyboard#####
|
23 |
[DllImport( "user32.dll" )]
|
24 |
public static extern void keybd_event( byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
|
25 |
[DllImport( "user32.dll" , CharSet = CharSet.Auto, ExactSpelling = true , CallingConvention = CallingConvention.Winapi)]
|
26 |
public static extern short GetKeyState( int keyCode);
|
27 |
#endregion
|
28 |
29 |
#region #####Art#####
|
30 |
31 |
[DllImport( "shell32.dll" )]
|
32 |
public static extern IntPtr SHGetFileInfo( string pszPath, uint dwFileAttributes, ref Art.GetIncoIntPtrStruct.SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
|
33 |
34 |
[DllImport( "user32.dll" )]
|
35 |
public static extern bool ClientToScreen(IntPtr hWnd, ref Art.DismantleElmentStruct.POINT lpPoint);
|
36 |
37 |
[DllImport( "user32.dll" )]
|
38 |
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
|
39 |
40 |
#endregion
|
41 |
42 |
#region #####Device#####
|
43 |
[DllImport( "User32.dll" )]
|
44 |
public static extern bool GetLastInputInfo( ref Device.LASTINPUTINFO info);
|
45 |
#endregion
|
46 |
47 |
#region #####Hook#####
|
48 |
49 |
[DllImport( "user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
50 |
public static extern int SetWindowsHookEx( int idHook, NativeStructs.HookProc lpfn, IntPtr hInstance, int threadId);
|
51 |
[DllImport( "user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
52 |
public static extern bool UnhookWindowsHookEx( int idHook);
|
53 |
[DllImport( "user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
54 |
public static extern int CallNextHookEx( int idHook, int nCode, IntPtr wParam, IntPtr lParam);
|
55 |
56 |
#endregion
|
57 |
58 |
#region #####Window#####
|
59 |
[DllImport( "user32.dll" )]
|
60 |
public static extern int GetForegroundWindow();
|
61 |
[DllImport( "user32.dll" , CharSet = CharSet.Auto)]
|
62 |
public static extern int GetWindowText( int hWnd, String str, Int32 count);
|
63 |
[DllImport( "user32.dll" , CharSet = CharSet.Auto)]
|
64 |
public static extern bool SetWindowText( int hWnd, string lpString);
|
65 |
[DllImport( "user32.dll" , CharSet = CharSet.Auto)]
|
66 |
public static extern int GetWindowTextLength( int hWnd);
|
67 |
[DllImport( "user32.dll" )]
|
68 |
public static extern IntPtr WindowFromPoint(Point Point);
|
69 |
[DllImport( "user32.dll" )]
|
70 |
public static extern int GetFocus();
|
71 |
#endregion
|
72 |
73 |
#region #####Input Method Editor#####
|
74 |
[DllImport( "Imm32.dll" )]
|
75 |
public static extern IntPtr ImmGetContext(IntPtr hWnd);
|
76 |
[DllImport( "Imm32.dll" )]
|
77 |
public static extern IntPtr ImmAssociateContext(IntPtr hWnd, IntPtr hIMC);
|
78 |
[DllImport( "Imm32.dll" )]
|
79 |
public static extern Boolean ImmReleaseContext(IntPtr hWnd, IntPtr hIMC);
|
80 |
#endregion
|
81 |
82 |
[DllImport( "user32" )]
|
83 |
public static extern int GetDoubleClickTime();
|
84 |
[DllImport( "user32" )]
|
85 |
public static extern int ToAscii( int uVirtKey, int uScanCode, byte [] lpbKeyState, byte [] lpwTransKey, int fuState);
|
86 |
[DllImport( "user32" )]
|
87 |
public static extern int GetKeyboardState( byte [] pbKeyState);
|
88 |
[DllImport( "kernel32.dll" , CharSet = CharSet.Auto, SetLastError = true )]
|
89 |
public static extern IntPtr GetModuleHandle( string lpModuleName);
|
90 |
[DllImport( "user32.dll" )]
|
91 |
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
|
92 |
} |
Win32的API函数是微软自己的东西,可以直接在C#中直接调用,在做WinForm时还是很有帮助的。有时候我们之直接调用Win32 的API,可以很高效的实现想要的效果。
代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->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); #endregion }