即使在应用程序之外,如何使图像跟随光标?

发布于 2024-09-25 18:28:02 字数 139 浏览 7 评论 0原文

在带有 Winforms 的 VB.Net 中,即使图像离开表单,如何使图像跟随光标?我想在拖放操作期间执行此操作。 BCL 和/或 GDI 比 P/Invokes 更适合我的用例。

如果您熟悉 PeaZip,它会在从其界面拖动文件时执行此类操作。

In VB.Net with Winforms how do I make an image follow the cursor even when it leaves the form? I want to do this during a drag and drop operation. BCL and/or GDI is better for my usecase than P/Invokes.

If you are familiar with PeaZip, it does something of this sort when dragging a file from it's interface.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如痴如狂 2024-10-02 18:28:02

有关使用 P/Invokes 执行此操作的信息:

CodeProject:自定义鼠标光标 (VB.NET)< /a>

文章中的关键内容:

<DllImport("user32.dll", EntryPoint:="CreateIconIndirect")> _
Private Shared Function CreateIconIndirect(ByVal iconInfo As IntPtr) As IntPtr
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
End Function

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

For information on doing it with P/Invokes:

CodeProject: Custom Mouse Cursors (VB.NET)

Key ones from the article:

<DllImport("user32.dll", EntryPoint:="CreateIconIndirect")> _
Private Shared Function CreateIconIndirect(ByVal iconInfo As IntPtr) As IntPtr
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
End Function

<DllImport("gdi32.dll")> _
Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
End Function
り繁华旳梦境 2024-10-02 18:28:02

最简单的方法似乎只是简单地将鼠标光标更改为默认鼠标光标和您想要跟随它的图像的合成图像。完成后将其更改回默认鼠标光标。

有多种方法可以使用 P/Invokes 来做到这一点,但这里有一个关于如何在没有它们的情况下做到这一点的理论。在我遇到的示例中,P/Invoke 主要用于获取指向已创建并编组到非托管内存中的游标结构的指针。然后,指针被传递给 System.Windows.Forms.Cursor 类的构造函数。

但是,接受内存流的游标类中有一个重载。如果可以在内存流中构造相同的游标文件结构,则可以将其传递到构造函数中,这将为我们提供要更改的游标。 “cur”图像文件与“ico”文件非常相似,并且相当简单。它由一个小标题、一个图像目录和最后的图像数据本身组成(维基百科条目ICO 文件格式)。构建然后写入内存流应该不会太难。

The easiest way to do this seems to be just plain changing the mouse cursor to a composite image of the default mouse cursor and the image you want following it. Then after you are finished change it back to the default mouse cursor.

There are ways to do this with P/Invokes, but here is a theory on how to do it without them. In examples I've run across P/Invokes are primarily used to get a pointer to/for a cursor Structure that has been created and Marshalled into unmanaged memory. Then the Pointer is passed to the Constructor for the System.Windows.Forms.Cursor Class.

However, there is an overload in the Cursor Class that accepts a Memory Stream. If the same cursor file structure could be constructed in a Memory Stream, it could then be passed into the Constructor, which would give us the Cursor to change to. The 'cur' image file is very similar to an 'ico' file, and is fairly simple. It consists of a small header, an image directory, and finally the image data itself (Wikipedia entry on the ICO file format). This should not be too hard to construct and then write out to a Memory Stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文