首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SharpDX在屏幕捕获中包括鼠标光标

使用SharpDX在屏幕捕获中包括鼠标光标
EN

Stack Overflow用户
提问于 2016-07-02 19:18:52
回答 1查看 1.1K关注 0票数 1

我正在使用ScreenCapture示例 of SharpDX库来截取全屏游戏的截图(因为这里常见的GDI+捕获技术不起作用,即使是Windos默认的"Print“键也不能对游戏进行适当的屏幕截图)。

我在应用程序中使用的代码实际上与上面链接的正式示例相同(翻译为VB.NET),因此我认为没有必要在这里发布相同的代码块。

我遇到的问题是,我想将鼠标光标包括在捕获的图像中。

也许SharpDX有一些方法或属性/参数来以友好的方式包含鼠标光标?如果不是,那么考虑到游戏中的鼠标光标是个性化的(不是默认的白色窗口箭头),如何才能做到这一点呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-04 15:37:05

您可以利用WinAPI的GetCursorInfo()函数获取当前游标的句柄。然后调用Icon.FromHandle()并将游标句柄作为参数传递,您将把游标作为可绘制的图标。

由于不同的游标可能有不同的热点,您将不得不考虑到这一点。为此,您可以使用WinAPI的GetIconInfo()函数来获取热点的X和Y坐标,然后您只需根据热点的位置减去光标的位置即可。

用于绘制游标的方法:

代码语言:javascript
复制
Public Shared Sub DrawCursor(ByVal Graphics As Graphics)
    Dim CursorInfo As New NativeMethods.CURSORINFO With {.cbSize = Marshal.SizeOf(GetType(NativeMethods.CURSORINFO))}

    'Get the cursor info.
    If NativeMethods.GetCursorInfo(CursorInfo) = True Then
        Using CursorIcon As System.Drawing.Icon = System.Drawing.Icon.FromHandle(CursorInfo.hCursor) 'Get the cursor icon.
            Dim IconInfo As New NativeMethods.ICONINFO

            Try
                Dim ModifierPoint As New Point(0, 0) 'Declare the modifier point (the cursor's hotspot).

                'Get the info for the cursor icon.
                If NativeMethods.GetIconInfo(CursorInfo.hCursor, IconInfo) = True Then
                    If IconInfo.fIcon = False Then 'If the cursor is an icon the hotspot will always be (0, 0).
                        ModifierPoint = New Point(IconInfo.xHotspot, IconInfo.yHotspot) 'Set the hotspot modifier.
                    End If

                End If


                'Normalize the coordinates according to the hotspot.
                Dim FinalPoint As New Point(CursorInfo.ptScreenPos.x - ModifierPoint.X, CursorInfo.ptScreenPos.y - ModifierPoint.Y)

                'Draw the cursor.
                Graphics.DrawIcon(CursorIcon, New Rectangle(FinalPoint, CursorIcon.Size))

            Finally
                'Some cleaning up...
                If IconInfo.hbmMask <> IntPtr.Zero Then
                    NativeMethods.DeleteObject(IconInfo.hbmMask)
                    IconInfo.hbmMask = IntPtr.Zero
                End If

                If IconInfo.hbmColor <> IntPtr.Zero Then
                    NativeMethods.DeleteObject(IconInfo.hbmColor)
                    IconInfo.hbmColor = IntPtr.Zero
                End If

            End Try
        End Using
    End If
End Sub

NativeMethods.vb:

代码语言:javascript
复制
Imports System.Runtime.InteropServices

Public NotInheritable Class NativeMethods
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure POINT
        Public x As Integer
        Public y As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure CURSORINFO
        Public cbSize As Integer
        Public flags As Integer
        Public hCursor As IntPtr
        Public ptScreenPos As POINT
    End Structure

    <DllImport("user32.dll")> _
    Public Shared Function GetCursorInfo(ByRef pci As CURSORINFO) As Boolean
    End Function

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure ICONINFO
        Public fIcon As Boolean
        Public xHotspot As Integer
        Public yHotspot As Integer
        Public hbmMask As IntPtr
        Public hbmColor As IntPtr
    End Structure

    <DllImport("user32.dll")> _
    Public Shared Function GetIconInfo(ByVal hIcon As IntPtr, ByRef piconinfo As ICONINFO) As Boolean
    End Function

    <DllImport("gdi32.dll")> _
    Public Shared Function DeleteObject(ByVal hObject As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
End Class

如果你拿出截图,你会把光标画在上面,如下所示:

代码语言:javascript
复制
Using g As Graphics = Graphics.FromImage(yourImage)
    DrawCursor(g)
End Using
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38163667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档