|
■No100786 (KOZ) に返信
完成したコードが無いのも残念なので、投下しておきます。
GetIconInfo で取得した ICONINFO 構造体(Class として宣言)を Cursor.Tag プロパティに保持しておき、不要になったら削除します。
Imports System.Runtime.InteropServices
Public Class Form1
Private Cursor_Image As Cursor
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
DestroyCursor(Cursor_Image)
Using bmp As New Bitmap(16, 16)
Using g As Graphics = Graphics.FromImage(bmp)
g.DrawLine(Pens.Red, 0, 0, 0, 5)
g.DrawLine(Pens.Red, 0, 0, 5, 0)
g.DrawLine(Pens.Red, 0, 0, 15, 15)
End Using
Cursor_Image = CreateCursor(bmp, 0, 0)
PictureBox1.Cursor = Cursor_Image
End Using
End Sub
Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
MyBase.OnFormClosed(e)
DestroyCursor(Cursor_Image)
End Sub
Private Shared Function CreateCursor(bmp As Bitmap, x As Integer, y As Integer) As Cursor
Dim hIcon As IntPtr = bmp.GetHicon()
Dim info As New ICONINFO()
GetIconInfo(hIcon, info)
DestroyIcon(hIcon)
info.xHotspot = x
info.yHotspot = y
info.fIcon = 0
Dim hCursor As IntPtr = CreateIconIndirect(info)
Dim cursor = New Cursor(hCursor) With {.Tag = info}
Return cursor
End Function
Private Shared Sub DestroyCursor(ByRef cursor As Cursor)
If cursor IsNot Nothing Then
DestroyIcon(cursor.Handle)
Dim info As ICONINFO = TryCast(cursor.Tag, ICONINFO)
If info IsNot Nothing Then
DeleteObject(info.hbmColor)
DeleteObject(info.hbmMask)
End If
cursor.Dispose()
cursor = Nothing
End If
End Sub
<StructLayout(LayoutKind.Sequential)>
Private Class ICONINFO
Public fIcon As Integer
Public xHotspot As Integer
Public yHotspot As Integer
Public hbmMask As IntPtr
Public hbmColor As IntPtr
End Class
<DllImport("user32.dll")>
Private Shared Function GetIconInfo(hIcon As IntPtr, iconinfo As ICONINFO) As Boolean
End Function
<DllImport("user32.dll")>
Private Shared Function CreateIconIndirect(iconInfo As ICONINFO) As IntPtr
End Function
<DllImport("user32.dll")>
Private Shared Function DestroyIcon(hIcon As IntPtr) As Boolean
End Function
<DllImport("gdi32.dll")>
Private Shared Function DeleteObject(hObject As IntPtr) As Boolean
End Function
End Class
|