我有一个设置为只读的 Windows 窗体,我注意到 CTRL+F 不起作用。有没有办法构建一个搜索功能或栏来查看我的 Windows 窗体中的特定文本/字符串?这是我的代码的样子:
# Create Window
add-type -assembly System.Windows.Forms
$form=New-Object System.Windows.Forms.Form
$form.StartPosition='CenterScreen'
# Textbox
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Dock = [System.Windows.Forms.DockStyle]::Fill
$textBox.ReadOnly =$true
$textBox.Multiline = $true
$textBox.ScrollBars = "Vertical"
$textBox.Font = New-Object System.Drawing.Font("Arial",12)
$textBox.ForeColor = "White"
$textBox.Text = $object
$textBox.BackColor = "Black"
$Form.Controls.Add($textBox)
# Button
$btn=New-Object System.Windows.Forms.Button
$btn.Text='Finish'
$btn.DialogResult='Ok'
$btn.Dock='bottom'
$form.Controls.Add($btn)
if($form.ShowDialog() -eq 'Ok'){
$tb.lines
}
是否可以将其作为 KeyDown 事件处理?如果是这样,您将如何记录 CTRL+F 并利用它来搜索窗口中的文本?
# Create Window
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName Microsoft.VisualBasic
$form = New-Object System.Windows.Forms.Form
$form.StartPosition = 'CenterScreen'
$form.KeyPreview = $true
$form.Add_KeyDown({
if ($_.Control -and $_.KeyCode -eq "F") {
$stringToFind = [Microsoft.VisualBasic.Interaction]::InputBox("Find what", "Find")
$pos = $textBox.Text.IndexOf($stringToFind);
if ($pos -ne -1) {
$textBox.SelectionStart = $pos;
$textBox.SelectionLength = $stringToFind.Length;
}
}
})
# Textbox
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Dock = [System.Windows.Forms.DockStyle]::Fill
$textBox.ReadOnly =$true
$textBox.Multiline = $true
$textBox.ScrollBars = "Vertical"
$textBox.Font = New-Object System.Drawing.Font("Arial",12)
$textBox.ForeColor = "White"
$textBox.Text = $object
$textBox.BackColor = "Black"
$Form.Controls.Add($textBox)
# Button
$btn = New-Object System.Windows.Forms.Button
$btn.Text = 'Finish'
$btn.DialogResult = 'Ok'
$btn.Dock = 'bottom'
$form.Controls.Add($btn)
if ($form.ShowDialog() -eq 'Ok') {
$tb.lines
}
感谢您的启发性代码!我尝试稍微扩展你的示例 - ctrl-f 让你给出一个搜索字符串,f3 和 f4 分别跳转结果。它基于 RichTextbox 控件 - 文本框不接受滚动到事件所需的 ScrollToCaret()。只需在文本框中填充一些文本并进行搜索即可。退出并退出。 我还没有设法将光标实际放置在搜索发生的位置。任何改进的想法都将受到高度赞赏! 问候约翰
function Search($SearchString){
$Text = $t.text
$newPos = 0
$LastPos = 0
while ($pos -ne -1){
$pos = $Text.IndexOf($SearchString)
if($pos -ne -1){
$RealPos = $LastPos + $pos
$global:arrFound += $RealPos
$LastPos = $RealPos + $global:Find.length
$NewPos = $pos + $global:Find.length
$Text = $Text.Substring($NewPos)
}
}
}
function DisplaySearch{
if($global:arrFound){
$t.Select($global:arrFound[$global:FoundIndex], $global:Find.Length)
$t.ScrollToCaret()
}
}
#Form
$fWidth = 1200
$fHeight = 1100
$f = New-Object System.Windows.Forms.Form
$f.Size = New-Object System.Drawing.Size($fWidth, $fHeight)
$f.Text = 'Decryption'
$f.StartPosition = "CenterScreen"
$f.FormBorderStyle = 'Fixed3D'
$f.MaximizeBox = $false
$f.Add_Load({
})
$f.Add_Shown({
$t.Focus()
})
#KeyCodes
$f.KeyPreview = $true
$f.Add_KeyDown({if ($_.Control -and $_.KeyCode -eq "F")
{
$global:arrFound = @()
$global:Find = [Microsoft.VisualBasic.Interaction]::InputBox("global:Find what", "global:Find", $global:Find)
Search $global:Find
$FindUpper = $global:Find.ToUpper()
Search $FindUpper
$FindUpper = $global:Find.ToLower()
Search $FindLower
$FindFirstCapital = $global:Find.ToLower()
$first = $FindFirstCapital.Substring(0, 1)
$first = $first.ToUpper()
$FindFirstCapital = $first + $FindFirstCapital.Substring(1)
Search $FindFirstCapital
if($global:arrFound){
$global:arrFound = $global:arrFound | sort
$global:FoundIndex = 0
}
DisplaySearch
}
})
$f.Add_KeyDown({if ($_.KeyCode -eq "F3")
{
if($global:arrFound){
$global:FoundIndex += 1
if($global:FoundIndex -ge $global:arrFound.length){$global:FoundIndex = 0}
DisplaySearch
}
}
})
$f.Add_KeyDown({if ($_.KeyCode -eq "F4")
{
if($global:arrFound){
$global:FoundIndex -= 1
if($global:FoundIndex -lt 0){$global:FoundIndex = $global:arrFound.length - 1}
DisplaySearch
}
}
})
$f.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{
$t.Text = ""
$f.Close()
}
})
#Text
$t = New-Object System.Windows.Forms.RichTextBox
$t.Location = New-Object System.Drawing.Point(10, 80)
$t.Size = New-Object System.Drawing.Size(($fWidth - 40), ($fHeight - 36))
$t.Multiline = $true
$t.Scrollbars = "Vertical"
$f.Controls.Add($t)
$f.ShowDialog()