【问题标题】:How to capture the screen and mouse pointer using Windows APIs?如何使用 Windows API 捕获屏幕和鼠标指针?
【发布时间】:2011-07-19 15:46:20
【问题描述】:

我正在使用以下代码在位图中捕获屏幕。屏幕被捕获,但我无法在屏幕上获得鼠标指针。您能否建议一些替代方法以便也捕获鼠标?

    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;
        }
    }

【问题讨论】:

    标签: c# .net graphics


    【解决方案1】:
    [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;
    }
    

    【讨论】:

    • 注意,这段代码会在错误的位置绘制一些光标,因为你需要调用'GetIconInfo'来获取偏移量。还有一些会出现褪色,更多细节见:stackoverflow.com/questions/918990
    【解决方案2】:

    如果您不是在寻找当前使用的光标的精确副本,您可以使用以下代码,您只需在原始代码中添加一行!

    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);
                //Following code is all you needed!
                graphics.DrawIcon(new Icon("Sample.ico"),Cursor.Position.X-50,Cursor.Position.Y-50);
                //The reason I minus 50 in the position is because you need to "offset" the position. Please go check out the post WholsRich commented.
            }
            return bitmap;
        }
    }
    

    你可以上网下载各种图标。

    或使用ICO Convert 制作您自己的。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-27
      • 2010-12-10
      • 1970-01-01
      • 2011-04-30
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多