Получить PixelColor в верхней / левой позиции, чтобы изменить строку времени на заставке - PullRequest
0 голосов
/ 18 апреля 2020

Мне сказали отредактировать программу здесь, так что вот небольшая программа, которая показывает то, что я пытаюсь выполнить sh (учиться) Я использую экран. Ограничения, как вы предложили, но проблема по-прежнему заключается в том, что мышь получает свое местоположение только тогда, когда она находится внутри формы. Вне формы мышь не имеет ни малейшего представления о том, где она находится, и поэтому не может сказать мне цвет в ее положении мыши.

Мне кажется, я так близок к тому, чтобы заставить это работать ...

Public Class Form10

    'Sample Code
    'Needs 2 Textboxes, names txtStatic, txtMouse
    'Needs 2 Pictureboxes, names PBStatic, PBMouse
    'Needs Timer1 set to 1000
    'Goal is to show pixelcolor of screen under
    'form's corner, top/left at Point(450,74) "named STATIC"
    'and show color of Screen Pixel when mouse is clicked
    'anywhere on Screen "named MOUSE"

    Dim ScreenX, ScreenY As Integer
    Dim SColor, MColor As Color
    Dim MouseLoc, StaticLoc As Point

    Private Sub Form10_Load(sender As Object, e As EventArgs) _
                            Handles Me.Load
        Me.Location = New Point(450, 75)
        StaticLoc = Me.Location
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        ScreenX = My.Computer.Screen.Bounds.Width
        ScreenY = My.Computer.Screen.Bounds.Height
        Dim bm As New Bitmap(ScreenX, ScreenY)
        SColor = GetPixColor(StaticLoc)
        MColor = GetPixColor(MouseLoc)
        TxtStatic.Text = StaticLoc.ToString
        TxtMouse.Text = MouseLoc.ToString
        PBStatic.BackColor = SColor
        PBMouse.BackColor = MColor
    End Sub

    Function GetPixColor(ByVal Location As Point) As Color
        Using b As New Bitmap(1, 1)
            Using g As Graphics = Graphics.FromImage(b)
                g.CopyFromScreen(Location, Point.Empty, New Drawing.Size(1, 1))
            End Using
            Return b.GetPixel(0, 0)
        End Using
    End Function

    Private Sub Form10_MouseMove(sender As Object,
                                 e As MouseEventArgs) _
                                 Handles Me.MouseMove
        If e.Button = MouseButtons.Left Then
            Dim x1, y1 As Integer
            x1 = e.Location.X - mouseloc.X
            y1 = e.Location.Y - mouseloc.Y
            Me.Location = New Point(Me.Location.X + x1,
                                    Me.Location.Y + y1)
        End If
    End Sub

    Private Sub Form10_MouseClick(sender As Object,
                                  e As MouseEventArgs) _
                                  Handles Me.MouseClick
        MouseLoc = e.Location
    End Sub

End Class

ЗДЕСЬ ДИЗАЙНЕР В СЛУЧАЕ ЧТО-ТО НЕПРАВИЛЬНО !!

_
Partial Class Form10
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.PBMouse = New System.Windows.Forms.PictureBox()
        Me.TxtMouse = New System.Windows.Forms.TextBox()
        Me.TxtStatic = New System.Windows.Forms.TextBox()
        Me.PBStatic = New System.Windows.Forms.PictureBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        CType(Me.PBMouse, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.PBStatic, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Timer1
        '
        Me.Timer1.Interval = 1000
        '
        'PBMouse
        '
        Me.PBMouse.Location = New System.Drawing.Point(35, 341)
        Me.PBMouse.Name = "PBMouse"
        Me.PBMouse.Size = New System.Drawing.Size(337, 50)
        Me.PBMouse.TabIndex = 8
        Me.PBMouse.TabStop = False
        '
        'TxtMouse
        '
        Me.TxtMouse.Font = New System.Drawing.Font("Segoe UI", 16.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.TxtMouse.Location = New System.Drawing.Point(35, 260)
        Me.TxtMouse.Name = "TxtMouse"
        Me.TxtMouse.Size = New System.Drawing.Size(337, 64)
        Me.TxtMouse.TabIndex = 7
        '
        'TxtStatic
        '
        Me.TxtStatic.Font = New System.Drawing.Font("Segoe UI", 16.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.TxtStatic.Location = New System.Drawing.Point(35, 109)
        Me.TxtStatic.Name = "TxtStatic"
        Me.TxtStatic.Size = New System.Drawing.Size(337, 64)
        Me.TxtStatic.TabIndex = 6
        '
        'PBStatic
        '
        Me.PBStatic.Location = New System.Drawing.Point(35, 43)
        Me.PBStatic.Name = "PBStatic"
        Me.PBStatic.Size = New System.Drawing.Size(337, 50)
        Me.PBStatic.TabIndex = 5
        Me.PBStatic.TabStop = False
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(39, 14)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(72, 29)
        Me.Label1.TabIndex = 9
        Me.Label1.Text = "Static"
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label3.Location = New System.Drawing.Point(39, 224)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(128, 29)
        Me.Label3.TabIndex = 11
        Me.Label3.Text = "MousePos"
        '
        'Form10
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(12.0!, 25.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(412, 450)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.PBMouse)
        Me.Controls.Add(Me.TxtMouse)
        Me.Controls.Add(Me.TxtStatic)
        Me.Controls.Add(Me.PBStatic)
        Me.Name = "Form10"
        Me.Text = "Form10"
        Me.TopMost = True
        CType(Me.PBMouse, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.PBStatic, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Friend WithEvents Timer1 As Timer
    Friend WithEvents PBMouse As PictureBox
    Friend WithEvents TxtMouse As TextBox
    Friend WithEvents TxtStatic As TextBox
    Friend WithEvents PBStatic As PictureBox
    Friend WithEvents Label1 As Label
    Friend WithEvents Label3 As Label
End Class
...