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

  • 问题

  • 我想在一个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

    2016年5月19日 11:32

答案

全部回复

  • 这样不知道可行否

    Cursor=Cursors.WaitCursor;

    this.UseWaitCursor = true;


    专注于.NET MIS开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2016年5月19日 15:46
  • 不行啊,我不是想改变原来的光标类型,而是想自己画一个跟着鼠标移动的十字

    Work8862

    2016年5月20日 3:53
  • 画一个跟着鼠标移动的十字

    可以参考一下tooltip控件的源代码或是找一个tooltip控件(devexpress有office style tooltip),

    这个控件跟随鼠标位置的移动, 并且tooltip中是加载一个透明背景图片(十字)

    参考DevExpress Super tooltip,在网上可以找到它的源代码


    专注于.NET MIS开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2016年5月20日 7:43
  • 您好,我在网上找了,没有找到这样的源码,能给个例子么?谢谢!

    Work8862

    2016年6月3日 1:19