首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >GetCursorInfo WinForms vs WPF C#

GetCursorInfo WinForms vs WPF C#
EN

Stack Overflow用户
提问于 2018-06-30 22:56:15
回答 1查看 476关注 0票数 3

为了更好地控制UI,我在将代码片段从WinForms转移到WPF时遇到了麻烦。

下面的代码在WinForms中返回True,但在WPF中返回False。我怀疑WPF面板对光标有影响,所以我尝试最小化启动应用程序,但仍然失败。由于GetCursorInfo是PInvoke,我认为它在编程语言中的工作方式应该是一样的。对此有什么建议吗?

    private CURSORINFO ci;

    [StructLayout(LayoutKind.Sequential)]
    public struct CURSORINFO
    {
        public Int32 cbSize;        // Specifies the size, in bytes, of the structure. 
        public Int32 flags;         // Specifies the cursor state.
        public IntPtr hCursor;      // Handle to the cursor. 
        Point point; // Should already marshal correctly.
    }

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetCursorInfo(ref CURSORINFO pci);

    public MainWindow()
    {
        InitializeComponent();

        ci = new CURSORINFO();

        ci.cbSize = Marshal.SizeOf(ci);

        MessageBox.Show(GetCursorInfo(ref ci).ToString());
    }    
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-01 07:40:45

    Point point; // Should already marshal correctly.

适用于System.Drawing.Point,但不适用于System.Windows.Point,WPF类型对X和Y成员使用double。所以CURSORINFO.cbSize太大了,这足以给你一个错误87 (“参数不正确”),它太大了。

请键入完整的名称。或者,如果您不想添加对System.Drawing的引用,只需自己声明点结构。别忘了,返回的信息是以像素为单位的,你通常希望在WPF应用程序中使用convert to inches

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51115996

复制
相关文章

相似问题

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