当前位置:网站首页>VB. Net class library to obtain the color under the mouse in the screen (Advanced - 3)

VB. Net class library to obtain the color under the mouse in the screen (Advanced - 3)

2022-06-26 21:56:00 Xiaoyu 163

In the solution :

Create a new one new class:myScreen

quote :

  One more this time System.Windows.Forms, Because we need to use button event

Then write the three segment function

Public Class myScreen
    Public Image As Bitmap
    Private ispick As Boolean = False
    Private bmp As Bitmap
    Private g As Graphics

    ''' <summary>
    '''  Be careful , Please do not call this function alone , also , This function corresponds to button The event is MouseDown. Get the mouse status and return the mouse style 
    ''' </summary>
    ''' <param name="e"> Mouse events , It's the left key </param>
    ''' <param name="cur"> Incoming mouse style , And returns a mouse style of Cross</param>
    ''' <returns></returns>
    Public Function PickColorDown(e As MouseEventArgs, cur As Cursor)
        If e.Button = MouseButtons.Left Then
            ispick = True
            Dim scr As Screen = Screen.PrimaryScreen
            Dim recSc As Rectangle = scr.Bounds
            bmp = New Bitmap(recSc.Width, recSc.Height)
            g = Graphics.FromImage(bmp)
            g.CopyFromScreen(New Point(0, 0), New Point(0, 0), New Size(recSc.Width, recSc.Height))
            cur = Cursors.Cross
            Return cur
        End If
    End Function
    ''' <summary>
    '''  Be careful , Please do not call this function alone , also , This function corresponds to button The event is MouseMove. Get the mouse condition and return the color of the mouse pointer area 
    ''' </summary>
    ''' <param name="e"> Mouse events </param>
    ''' <param name="but"> obtain button</param>
    ''' <returns></returns>
    Public Function PickColorMove(e As MouseEventArgs, but As Button)
        Dim x, y As Integer
        Dim p As Point = New Point(e.X, e.Y)
        Dim colorpoint As Color
        If ispick = True Then
            x = but.PointToScreen(p).X
            y = but.PointToScreen(p).Y
            colorpoint = bmp.GetPixel(x, y)
            Return colorpoint
        End If

    End Function
    ''' <summary>
    '''  Be careful , Please do not call this function alone , also , This function corresponds to button The event is MouseUp. Get the mouse status and return the mouse style 
    ''' </summary>
    ''' <param name="cur"> Incoming mouse style , And returns a mouse style of Default</param>
    ''' <returns></returns>
    Public Function PickColorUp(cur As Cursor)
        ispick = False
        cur = Cursors.Default
        Return cur
    End Function


End Class

  Here is a reference VB.net Big guy's article .

Call on form :

in front : Make a statement myScreen Class

 Public GDIscr As New GDI_Make_NET4_5.myScreen

  Code in :

Private Sub Button3_MouseDown(sender As Object, e As MouseEventArgs) Handles Button3.MouseDown
        Cursor = GDIscr.PickColorDown(e, Cursor)

    End Sub

    Private Sub Button3_MouseMove(sender As Object, e As MouseEventArgs) Handles Button3.MouseMove
        Pic.BackColor = GDIscr.PickColorMove(e, Button3)
    End Sub

    Private Sub Button3_MouseUp(sender As Object, e As MouseEventArgs) Handles Button3.MouseUp
        Cursor = GDIscr.PickColorUp(Cursor)
    End Sub

have a look :

 

原网站

版权声明
本文为[Xiaoyu 163]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206262040131679.html