在这个屏幕放大镜上绘制一个漂亮的交叉指示器?

时间:2014-01-10 15:15:57

标签: .net vb.net drawing gdi+ paint

我想绘制一个漂亮的“指示器”,如十字架或其他东西,以确切地知道在屏幕放大镜中将鼠标放在哪里,在像素周围绘制一个交叉指示器。

我的GDI +知识非常不合理。

这就是放大镜窗口的样子:

enter image description here

我想画一个像指示器,当前的鼠标或像这样的交叉指示器(但这太丑了,有任何像素精确度因为我使用了MsPaint)

enter image description here

有人可以帮我这么做吗?

这就是我绘制图像的方式:

''' <summary>
''' Repaints the Magnifier.
''' </summary>
Private Sub Repaint()

    ' Region Length.
    Dim lengthX As Single = Me.Width * ZoomFactor
    Dim lengthY As Single = (Me.Height - ZoomFactor_KryptonTrackBar.Bounds.Height) * ZoomFactor

    ' Center Image Around The Mouse.
    Dim offsetX As Single = (Me.Width * ZoomFactor) \ 2
    Dim offsetY As Single = (Me.Height * ZoomFactor) \ 2

    ' Actual Area To Blit To.
    Dim blitAreaX As Integer = Me.Width
    Dim blitAreaY As Integer = Me.Height - ZoomFactor_KryptonTrackBar.Bounds.Height

    bmp = New Bitmap(CInt(blitAreaX), CInt(blitAreaY))
    g1 = ZoomFactor_PictureBox.CreateGraphics
    g2 = Graphics.FromImage(bmp)

    ' Set the image quality.
    g1.SmoothingMode = SmoothingMode.None
    g1.CompositingQuality = CompositingQuality.HighSpeed
    g1.PixelOffsetMode = PixelOffsetMode.HighSpeed
    g1.InterpolationMode = InterpolationMode.NearestNeighbor

    g2.SmoothingMode = SmoothingMode.None
    g2.CompositingQuality = CompositingQuality.HighSpeed
    g2.PixelOffsetMode = PixelOffsetMode.HighSpeed
    g2.InterpolationMode = InterpolationMode.NearestNeighbor

    ' Devicecontext (DC) of the Desktop and the Graphics object.
    Dim hWndWindow As IntPtr = GetDesktopWindow()
    Dim hdcWindow As IntPtr = GetDC(hWndWindow)
    Dim hdcGraphics As IntPtr = g2.GetHdc()

    ' BitBlt the Screen (Captures Transparent Windows & Prevents Mirror Effect)
    BitBlt(hdcGraphics.ToInt32, 0, 0, blitAreaX, blitAreaY,
           hdcWindow.ToInt32, MousePosition.X - offsetX, MousePosition.Y - offsetY,
           TernaryRasterOperations.SRCCOPY Or
           TernaryRasterOperations.CAPTUREBLT Or
           TernaryRasterOperations.NOMIRRORBITMAP)

    ' Free Memory
    ReleaseDC(hWndWindow, hdcWindow)
    g2.ReleaseHdc(hdcGraphics)

    ' Paint
    g1.DrawImage(bmp, New Rectangle(0, 0, blitAreaX, blitAreaY), 0, 0, lengthX, lengthY, GraphicsUnit.Pixel)

    ' Set Magnifier position.
    ' Do this after painting to reduce blinking effects.
    SetMangifierPosition()

End Sub
  

更新

我做了一个修改来绘制默认的十字鼠标光标,但它没有正确对齐,我不知道为什么会发生这种情况,因为我将控件大小的一半除以绘制图像的中心光标所在位图:

注意:但是如果我以100%放大,那么我可以注意到光标仍然是未对齐的,但它正在向右像素投影,我不明白这一点。

...
    ' Paint
    g1.DrawImage(bmp, New Rectangle(0, 0, blitAreaX, blitAreaY), 0, 0, lengthX, lengthY, GraphicsUnit.Pixel)
...

    ' And paint the mouse cursor
    Cursors.Cross.Draw(g1, New Rectangle(ZoomFactor_PictureBox.Width \ 2, ZoomFactor_PictureBox.Height \ 2, 0, 0))

enter image description here

  

更新2

解决:

enter image description here

    ' Paint
    g1.DrawImage(bmp, New Rectangle(0, 0, blitAreaX, blitAreaY), 0, 0, lengthX, lengthY, GraphicsUnit.Pixel)

    ' Set the cursor Rectangle.
    Dim CursorRect As New Rectangle With
    {
     .Size = New Size(0, 0),
     .Location = New Point((ZoomFactor_PictureBox.Width \ 2) - Cursors.Cross.Size.Width \ 2,
                           (ZoomFactor_PictureBox.Height \ 2) - Cursors.Cross.Size.Height \ 2 - 10)
    }

    ' Paint the mouse cursor
    Cursors.Cross.Draw(g1, CursorRect)

1 个答案:

答案 0 :(得分:2)

您可以使用透明背景存储十字线的png,并将DrawImage存储在您的顶部:

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage(v=vs.110).aspx

或者手动绘制g1.DrawLine四次:

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawline(v=vs.110).aspx