powershell——UI系列:获取当前鼠标坐标

01 前言

这是一个小工具,当时为了配合搞Windows API自动化(模拟鼠标键盘操作)写的。虽然目前来说有比较简单的一些的方法——按键精灵之类以及UI Automation ,仍然用powershell实现了下,带界面的(PowerShell Studio设计)。

02 正文

代码如下:

<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by:  SAPIEN Technologies, Inc., PowerShell Studio 2014 v4.1.58
     Generated on:       2017-10-02 0:07
     Generated by:       hokis
    --------------------------------------------------------------------------------
    .DESCRIPTION
        GUI script generated by PowerShell Studio 2014
#>
#----------------------------------------------
#region Application Functions
#----------------------------------------------

function OnApplicationLoad
{
	#Note: This function is not called in Projects
	#Note: This function runs before the form is created
	#Note: To get the script directory in the Packager use: Split-Path $hostinvocation.MyCommand.path
	#Note: To get the console output in the Packager (Windows Mode) use: $ConsoleOutput (Type: System.Collections.ArrayList)
	#Important: Form controls cannot be accessed in this function
	#TODO: Add modules and custom code to validate the application load
	
	return $true #return true for success or false for failure
}

function OnApplicationExit
{
	#Note: This function is not called in Projects
	#Note: This function runs after the form is closed
	#TODO: Add custom code to clean up and unload modules when the application exits
	
	$script:ExitCode = 0 #Set the exit code for the Packager
}

#endregion Application Functions

#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-GetMousePosition_psf {

	#----------------------------------------------
	#region Import the Assemblies
	#----------------------------------------------
	[void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
	[void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
	[void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
	#endregion Import Assemblies

	#----------------------------------------------
	#region Generated Form Objects
	#----------------------------------------------
	[System.Windows.Forms.Application]::EnableVisualStyles()
	$GetMousePosition = New-Object 'System.Windows.Forms.Form'
	$showTop = New-Object 'System.Windows.Forms.CheckBox'
	$postionInput = New-Object 'System.Windows.Forms.MaskedTextBox'
	$label刷新频率秒 = New-Object 'System.Windows.Forms.Label'
	$refreshFrequency = New-Object 'System.Windows.Forms.NumericUpDown'
	$label鼠标坐标 = New-Object 'System.Windows.Forms.Label'
	$btnStart = New-Object 'System.Windows.Forms.Button'
	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
	#endregion Generated Form Objects

	#----------------------------------------------
	# User Generated Script
	#----------------------------------------------
	#定时器
	$timer = New-Object System.Timers.Timer
	
	$btnStart_Click = {
		#TODO: Place custom script here
		if ($btnStart.Text -eq "开始")
		{
			$btnStart.Text = "结束"
			$timer.Interval = 1000 * $refreshFrequency.Text
			$timer.AutoReset = $true
			$timer.Enabled = $true
			#添加任务
			$timer.add_Elapsed({
				$mousePosition = [System.Windows.Forms.Cursor]::Position
				$postionInput.Text = "(" + $mousePosition.X + "," + $mousePosition.Y + ")"
			})
			#绑定到控件,否则不起作用
			$timer.SynchronizingObject = $refreshFrequency
			$timer.Start()
		}
		elseif ($btnStart.Text -eq "结束")
		{
			$btnStart.Text = "开始"
			
			$timer.Stop()
			$timer.Enabled = $false
			
		}
	}
	
	
	$showTop_CheckedChanged={
		#TODO: Place custom script here
		$GetMousePosition.TopMost = $showTop.Checked
	}
	
	# --End User Generated Script--
	#----------------------------------------------
	#region Generated Events
	#----------------------------------------------
	
	$Form_StateCorrection_Load=
	{
		#Correct the initial state of the form to prevent the .Net maximized form issue
		$GetMousePosition.WindowState = $InitialFormWindowState
	}
	
	$Form_Cleanup_FormClosed=
	{
		#Remove all event handlers from the controls
		try
		{
			$showTop.remove_CheckedChanged($showTop_CheckedChanged)
			$btnStart.remove_Click($btnStart_Click)
			$GetMousePosition.remove_Load($Form_StateCorrection_Load)
			$GetMousePosition.remove_FormClosed($Form_Cleanup_FormClosed)
		}
		catch [Exception]
		{ }
	}
	#endregion Generated Events

	#----------------------------------------------
	#region Generated Form Code
	#----------------------------------------------
	$GetMousePosition.SuspendLayout()
	$refreshFrequency.BeginInit()
	#
	# GetMousePosition
	#
	$GetMousePosition.Controls.Add($showTop)
	$GetMousePosition.Controls.Add($postionInput)
	$GetMousePosition.Controls.Add($label刷新频率秒)
	$GetMousePosition.Controls.Add($refreshFrequency)
	$GetMousePosition.Controls.Add($label鼠标坐标)
	$GetMousePosition.Controls.Add($btnStart)
	$GetMousePosition.ClientSize = '217, 136'
	$GetMousePosition.MaximizeBox = $False
	$GetMousePosition.Name = "GetMousePosition"
	$GetMousePosition.ShowIcon = $False
	$GetMousePosition.Text = "获取当前鼠标坐标"
	$GetMousePosition.TopMost = $True
	#
	# showTop
	#
	$showTop.Checked = $True
	$showTop.CheckState = 'Checked'
	$showTop.Font = "Microsoft Sans Serif, 8.25pt, style=Bold"
	$showTop.Location = '162, 91'
	$showTop.Name = "showTop"
	$showTop.Size = '59, 24'
	$showTop.TabIndex = 5
	$showTop.Text = "置顶"
	$showTop.UseVisualStyleBackColor = $True
	$showTop.add_CheckedChanged($showTop_CheckedChanged)
	#
	# postionInput
	#
	$postionInput.Font = "Microsoft Sans Serif, 9.75pt, style=Bold"
	$postionInput.ForeColor = 'Red'
	$postionInput.Location = '104, 16'
	$postionInput.Name = "postionInput"
	$postionInput.ReadOnly = $True
	$postionInput.Size = '79, 22'
	$postionInput.TabIndex = 4
	$postionInput.Text = "(0,0)"
	$postionInput.TextAlign = 'Center'
	#
	# label刷新频率秒
	#
	$label刷新频率秒.Location = '12, 48'
	$label刷新频率秒.Name = "label刷新频率秒"
	$label刷新频率秒.Size = '86, 18'
	$label刷新频率秒.TabIndex = 3
	$label刷新频率秒.Text = "刷新频率(秒)"
	#
	# refreshFrequency
	#
	$refreshFrequency.DecimalPlaces = 1
	$refreshFrequency.Increment = 0.2
	$refreshFrequency.Location = '104, 43'
	$refreshFrequency.Maximum = 3
	$refreshFrequency.Minimum = 0.4
	$refreshFrequency.Name = "refreshFrequency"
	$refreshFrequency.ReadOnly = $True
	$refreshFrequency.Size = '44, 20'
	$refreshFrequency.TabIndex = 2
	$refreshFrequency.TextAlign = 'Center'
	$refreshFrequency.Value = 0.4
	#
	# label鼠标坐标
	#
	$label鼠标坐标.Location = '12, 19'
	$label鼠标坐标.Name = "label鼠标坐标"
	$label鼠标坐标.Size = '86, 19'
	$label鼠标坐标.TabIndex = 1
	$label鼠标坐标.Text = "鼠标坐标"
	#
	# btnStart
	#
	$btnStart.Font = "Microsoft Sans Serif, 14.25pt, style=Bold"
	$btnStart.Location = '29, 77'
	$btnStart.Name = "btnStart"
	$btnStart.Size = '119, 47'
	$btnStart.TabIndex = 0
	$btnStart.Text = "开始"
	$btnStart.UseVisualStyleBackColor = $True
	$btnStart.add_Click($btnStart_Click)
	$refreshFrequency.EndInit()
	$GetMousePosition.ResumeLayout($false)
	#endregion Generated Form Code

	#----------------------------------------------

	#Save the initial state of the form
	$InitialFormWindowState = $GetMousePosition.WindowState
	#Init the OnLoad event to correct the initial state of the form
	$GetMousePosition.add_Load($Form_StateCorrection_Load)
	#Clean up the control events
	$GetMousePosition.add_FormClosed($Form_Cleanup_FormClosed)
	#Show the Form
	return $GetMousePosition.ShowDialog()

} #End Function

#Call OnApplicationLoad to initialize
if((OnApplicationLoad) -eq $true)
{
	#Call the form
	Call-GetMousePosition_psf | Out-Null
	#Perform cleanup
	OnApplicationExit
}

代码另存为.ps1,右键,“使用Powershell运行”即可。如不能运行,参考【此处】解决。
附上一张效果图:
powershell——UI系列:获取当前鼠标坐标_第1张图片

03 后记

其实来说核心代码就两行,其他大都是为了界面展示。由此可见,写界面真不是很多Coder喜欢做的事,能用几行命令搞定的事,干嘛非跟自己过不去~
最后,这段代码里面,定时器的用法也可稍微注意一下。

你可能感兴趣的:(powershell,鼠标坐标,定时器,powershell)