鼠标位置的颜色已关闭

时间:2015-08-24 15:36:01

标签: vb.net visual-studio-2013 cursor-position

Public Class Form1
Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer) As Integer
Dim BMP As New Bitmap(1, 1)
Dim G As Graphics = Graphics.FromImage(BMP)
Dim XCoorLabel As New Label
Dim YCoorLabel As New Label
Dim ColorLabel As New Label
Dim ColorExam As New Panel

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Preparing the Form
    Me.Width = 500
    Me.Height = 150
    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
    'Setting up all the labels and 1 panel
    With XCoorLabel
        .Location = New Point(12, 9)
        .Name = "xCoorlabel"
        .Size = New Drawing.Size(50, 25)
        .Visible = True
        .ForeColor = Color.DarkGray
    End With
    With YCoorLabel
        .Location = New Point(69, 9)
        .Name = "YCoorLabel"
        .Size = New Drawing.Size(50, 25)
        .Visible = True
        .ForeColor = Color.DarkGray
    End With
    With ColorLabel
        .Location = New Point(12, 36)
        .Name = "ColorLabel"
        .Size = New Drawing.Size(200, 25)
        .Visible = True
        .ForeColor = Color.DarkGray
    End With
    With ColorExam
        .Location = New Point(12, 66)
        .Name = "ColorLabel"
        .Size = New Drawing.Size(20, 10)
        .Visible = True
    End With
    'Adding everything to the form
    Me.Controls.Add(XCoorLabel)
    Me.Controls.Add(YCoorLabel)
    Me.Controls.Add(ColorLabel)
    Me.Controls.Add(ColorExam)
    'Starting the timer
    Timer1.Enabled = True
End Sub
Private Sub keyPressed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    'Stopping the timer when 'W' is pressed
    If e.KeyValue = Keys.W Then
        Timer1.Enabled = False
    End If
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'Getting the cursor position and place it in the text label
    Dim mousepos As Point = Cursor.Position
    XCoorLabel.Text = "X" & mousepos.X
    YCoorLabel.Text = "Y" & mousepos.Y
    'Getting the screenimage and checking what color is on the location
    G.CopyFromScreen(mousepos, Point.Empty, BMP.Size)
    'placing the RGB color values in text in the label
    ColorLabel.Text = BMP.GetPixel(0, 0).ToString
    'Change the panels color according to the color that was found on the position
    Dim Icolor As Integer = GetPixel(GetDC(0), mousepos.X, mousepos.Y)
    ColorExam.BackColor = System.Drawing.ColorTranslator.FromOle(Icolor)
    End Sub
End Class

代码可以被粘贴到2013年的可视化工作室,并且可以立即使用(无需自己添加任何标签或内容以查看其功能,显然允许更改代码)< /强>

上面是我的代码。我想尝试一些东西,找到当前的鼠标位置,以及哪个颜色位于该位置(不仅仅是从背景,从屏幕本身)。

问题是,这段代码实际上是有效的(可能不是最好的代码,但它确实起作用)。 可悲的是,我有一个问题,当我将鼠标悬停在某个地方时,它显示的颜色不匹配,当我检查它时,我注意到它已关闭。

在X中,似乎关闭了大约400-500像素

在Y中,似乎关闭了大约90像素

有没有人知道它的来源,以及我如何解决这个问题?

0 个答案:

没有答案