请问GDI高手:怎样画十字光标?

weixin_38051760 2016-05-19 11:32:24
我想在一个Datagridview中画一个大的十字光标,
从网上找了一段代码,是用在Picturebox中的,
用在Datagridview里就有问题,当鼠标点下再抬起时,
会留下一个光标在屏幕上,直到该控件刷新时才消失,
另外,当鼠标进入控件时,有时候也会留下两道线。
不用这个方法也可以,能实现就行。谢谢!

Private Declare Function SetROP2 Lib "Gdi32.dll" Alias "SetROP2" (ByVal hdc As IntPtr, ByVal nDrawMode As Integer) As Integer
Private Declare Function CreatePen Lib "Gdi32.dll" Alias "CreatePen" (ByVal fnPenStyle As Integer, ByVal width As Integer, ByVal color As Integer) As IntPtr
Private Declare Function MoveToEx Lib "Gdi32.dll" Alias "MoveToEx" (ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal lppoint As IntPtr) As Integer
Private Declare Function LineTo Lib "Gdi32.dll" Alias "LineTo" (ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer
Private Declare Function SelectObject Lib "Gdi32.dll" Alias "SelectObject" (ByVal hdc As IntPtr, ByVal obj As IntPtr) As IntPtr
Private dgvcursor_x, dgvcursor_y As Integer
Private Sub DataGridView2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView2.MouseMove

Dim g1 As Graphics = DataGridView2.CreateGraphics

Dim hdc As IntPtr = g1.GetHdc

Dim hpen As IntPtr = CreatePen(0, 1, &HFF)
Dim rop As Integer = SetROP2(hdc, 7)
Dim oldpen As IntPtr = SelectObject(hdc, hpen)

If dgvcursor_x > 0 Then '删除上一次画线
MoveToEx(hdc, dgvcursor_x, 0, IntPtr.Zero)
LineTo(hdc, dgvcursor_x, DataGridView2.Height)

MoveToEx(hdc, 0, dgvcursor_y, IntPtr.Zero)
LineTo(hdc, DataGridView2.Width, dgvcursor_y)
End If

dgvcursor_x = e.X
dgvcursor_y = e.Y

MoveToEx(hdc, dgvcursor_x, 0, IntPtr.Zero)
LineTo(hdc, dgvcursor_x, DataGridView2.Height)

MoveToEx(hdc, 0, dgvcursor_y, IntPtr.Zero)
LineTo(hdc, DataGridView2.Width, dgvcursor_y)



SelectObject(hdc, oldpen)
SetROP2(hdc, rop)

g1.ReleaseHdc(hdc)
end sub


Work8862
...全文
25 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复

476

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧