在C#中用graphics截取屏幕时, 无法截取鼠标.用下面的方法可先截取屏幕,再将鼠标画上去。画鼠标的代码如下:
[StructLayout(LayoutKind.Sequential)]
struct POINT
{
public Int32 x;
public Int32 y;
}
[StructLayout(LayoutKind.Sequential)]
struct CURSORINFO
{
public Int32 cbSize; // Specifies the size, in bytes, of the structure.
// The caller must set this to Marshal.SizeOf(typeof(CURSORINFO)).
public Int32 flags; // Specifies the cursor state. This parameter can be one of the following values:
// 0 The cursor is hidden.
// CURSOR_SHOWING The cursor is showing.
public IntPtr hCursor; // Handle to the cursor.
public POINT ptScreenPos; // A POINT structure that receives the screen coordinates of the cursor.
}
[DllImport("user32.dll")]
static extern bool GetCursorInfo(out CURSORINFO pci);
private const Int32 CURSOR_SHOWING = 0x00000001;
public const int DI_NORMAL = 0x0003;
[DllImport("user32.dll", SetLastError = true)]
static extern bool DrawIconEx(IntPtr hdc, int xLeft, int yTop, IntPtr hIcon,
int cxWidth, int cyHeight, int istepIfAniCur, IntPtr hbrFlickerFreeDraw,
int diFlags);
public void DrawMouse(Graphics g)
{
CURSORINFO pci=new CURSORINFO();
pci.cbSize = Marshal.SizeOf(pci);
GetCursorInfo(out pci);
IntPtr dc = g.GetHdc();
DrawIconEx(dc, pci.ptScreenPos.x, pci.ptScreenPos.y, pci.hCursor, 32, 32, 1, IntPtr.Zero , DI_NORMAL);
g.ReleaseHdc();
}