API的屏幕和鼠标指针

API的屏幕和鼠标指针

本文介绍了如何捕获使用Windows API的屏幕和鼠标指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code捕捉位图的画面。屏幕捕获,但我无法得到屏幕上的鼠标指针。你可以建议使鼠标捕获以及一些替代办法?

 私人位图CaptureScreen()
    {
        //尺寸大小是多大的面积来捕捉
        // pointOrigin是区域的左上角来捕获
        INT宽度= Screen.PrimaryScreen.Bounds.X + Screen.PrimaryScreen.Bounds.Width;
        INT高= Screen.PrimaryScreen.Bounds.Y + Screen.PrimaryScreen.Bounds.Height;
        尺寸尺寸=新尺寸(宽,高);
        点pointOfOrigin =新点(0,0);

        点阵位图=新位图(size.Width,size.Height);
        {
            使用(图形显卡= Graphics.FromImage(位图))
            {
                graphics.CopyFromScreen(pointOfOrigin,新点(0,0),大小);
            }
            返回的位图;
        }
    }
 

解决方案

  [StructLayout(LayoutKind.Sequential)
结构cursorinfo段
{
    公众的Int32 CBSIZE;
    公众的Int32标志;
    公众的IntPtr HCURSOR;
    公共POINTAPI ptScreenPos;
}

[StructLayout(LayoutKind.Sequential)
结构POINTAPI
{
    公众诠释X;
    公众诠释Ÿ;
}

[的DllImport(user32.dll中)
静态外部布尔GetCursorInfo(出cursorinfo段PCI);

[的DllImport(user32.dll中)
静态的extern BOOL DrawIcon(IntPtr的的hDC,INT X,诠释Y,IntPtr的HICON);

常量的Int32 CURSOR_SHOWING = 00000001;

公共静态位图CaptureScreen(布尔CaptureMouse)
{
    位图的结果=新位图(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,PixelFormat.Format24bppRgb);

    尝试
    {
        使用(图形G = Graphics.FromImage(结果))
        {
            g.CopyFromScreen(0,0,0,0,Screen.PrimaryScreen.Bounds.Size,CopyPixelOperation.SourceCopy);

            如果(CaptureMouse)
            {
                cursorinfo段PCI;
                pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof运算(cursorinfo段));

                如果(GetCursorInfo(出PCI))
                {
                    如果(pci.flags == CURSOR_SHOWING)
                    {
                        DrawIcon(g.GetHdc(),pci.ptScreenPos.x,pci.ptScreenPos.y,pci.hCursor);
                        g.ReleaseHdc();
                    }
                }
            }
        }
    }
    抓住
    {
        结果= NULL;
    }

    返回结果;
}
 

I'm using the below code to capture the screen in a bitmap. The screen is captured, but I'm unable to get the mouse pointer on the screen. Could you suggest some alternative approach so that the mouse is captured as well?

    private Bitmap CaptureScreen()
    {
        // Size size is how big an area to capture
        // pointOrigin is the upper left corner of the area to capture
        int width = Screen.PrimaryScreen.Bounds.X + Screen.PrimaryScreen.Bounds.Width;
        int height = Screen.PrimaryScreen.Bounds.Y + Screen.PrimaryScreen.Bounds.Height;
        Size size = new Size(width, height);
        Point pointOfOrigin = new Point(0, 0);

        Bitmap bitmap = new Bitmap(size.Width, size.Height);
        {
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.CopyFromScreen(pointOfOrigin, new Point(0, 0), size);
            }
            return bitmap;
        }
    }
解决方案
[StructLayout(LayoutKind.Sequential)]
struct CURSORINFO
{
    public Int32 cbSize;
    public Int32 flags;
    public IntPtr hCursor;
    public POINTAPI ptScreenPos;
}

[StructLayout(LayoutKind.Sequential)]
struct POINTAPI
{
    public int x;
    public int y;
}

[DllImport("user32.dll")]
static extern bool GetCursorInfo(out CURSORINFO pci);

[DllImport("user32.dll")]
static extern bool DrawIcon(IntPtr hDC, int X, int Y, IntPtr hIcon);

const Int32 CURSOR_SHOWING = 0x00000001;

public static Bitmap CaptureScreen(bool CaptureMouse)
{
    Bitmap result = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format24bppRgb);

    try
    {
        using (Graphics g = Graphics.FromImage(result))
        {
            g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

            if (CaptureMouse)
            {
                CURSORINFO pci;
                pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CURSORINFO));

                if (GetCursorInfo(out pci))
                {
                    if (pci.flags == CURSOR_SHOWING)
                    {
                        DrawIcon(g.GetHdc(), pci.ptScreenPos.x, pci.ptScreenPos.y, pci.hCursor);
                        g.ReleaseHdc();
                    }
                }
            }
        }
    }
    catch
    {
        result = null;
    }

    return result;
}

这篇关于如何捕获使用Windows API的屏幕和鼠标指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 17:21