鼠标位置的颜色不正确

问题描述 投票:0回答:1
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

代码可以粘贴到 VISUAL STUDIO 2013 中,并且将立即运行(无需添加任何标签或自己的东西即可看到它的功能,显然允许更改代码

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

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

在 X 中似乎有大约 400-500 像素的偏差

在 Y 中似乎相差约 90 像素

有人知道这是从哪里来的吗?我该如何解决这个问题?

vb.net visual-studio-2013 cursor-position
1个回答
0
投票

我也有同样的问题。我认为这是因为 DPI 缩放。将比例设置为 100%,您可能会在光标位置获得正确的颜色

© www.soinside.com 2019 - 2024. All rights reserved.