Powershell 文本框占位符
Powershell Textbox placeholder
我创建我的自,定义我的文本框,我使用以下代码作为我的占位符文本:
$AssetText.Add_MouseClick({ $AssetText.text = “” })
$ErrorText.Add_MouseClick({ $ErrorText.text = “” })
$IssueText.Add_MouseClick({ $IssueText = “” })
$TestTagText.Add_MouseClick({ $TestTagText.text = “” })
$TroubleshootText.Add_MouseClick({ $TroubleshootText.text = “” })
$ResolutionText.Add_MouseClick({ $ResolutionText.text = “” })
它可以从 TextBox 中删除文本,但如果我在任何 TextBox 中键入大量文本,然后在其外部单击,然后返回,它会删除我正在处理的文本。
有没有其他我可以使用的功能比目前的方法更好用?因此,最初我可以单击 $TextBox 使文本消失,但是当我在框中写入自己的文本时,在 $TextBox 内外单击后不会消失?
如果您想以原生 OS 方式使用占位符,您可以将 EM_SETCUEBANNER
发送到 TextBox
以设置占位符文本:
using assembly System.Windows.Forms
using namespace System.Windows.Forms
using namespace System.Drawing
$code = @"
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hWnd,
int msg, IntPtr wParam, string lParam);
public const int EM_SETCUEBANER = 0x1501;
"@
$Win32Helpers = Add-Type -MemberDefinition $code -Name "Win32Helpers" -PassThru
$form = [Form] @{
ClientSize = [Point]::new(400,100);
Text = "Placeholder Text";
}
$form.Controls.AddRange(@(
($textBox1 = [TextBox] @{Location = [Point]::new(10,10) })
($textBox2 = [TextBox] @{Location = [Point]::new(10,40) })
))
$textBox1.add_HandleCreated({
$Win32Helpers::SendMessage($textBox1.Handle,$Win32Helpers::EM_SETCUEBANER`
, [IntPtr]0, "Start typing ...")
$Win32Helpers::SendMessage($textBox2.Handle,$Win32Helpers::EM_SETCUEBANER`
, [IntPtr]0, "Start typing ...")
})
$null = $form.ShowDialog()
$form.Dispose()
这是为文本框设置占位符文本的另一种方法。该方法依赖于处理 WM_PAINT
message and has been explained and implemented here.
此方法与其他方法(发送 EM_SETCUEBANNER
)的区别在于此方法也适用于多行文本框。
using assembly System.Windows.Forms
using namespace System.Windows.Forms
using namespace System.Drawing
$assemblies = "System.Windows.Forms", "System.Drawing"
$code = @"
using System.Drawing;
using System.Windows.Forms;
public class ExTextBox : TextBox
{
string hint;
public string Hint
{
get { return hint; }
set { hint = value; this.Invalidate(); }
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0xf)
{
if (!this.Focused && string.IsNullOrEmpty(this.Text)
&& !string.IsNullOrEmpty(this.Hint))
{
using (var g = this.CreateGraphics())
{
TextRenderer.DrawText(g, this.Hint, this.Font,
this.ClientRectangle, SystemColors.GrayText , this.BackColor,
TextFormatFlags.Top | TextFormatFlags.Left);
}
}
}
}
}
"@
#Add the SendMessage function as a static method of a class
Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $code -Language CSharp
# Create an instance of MyForm.
$form = [Form] @{
ClientSize = [Point]::new(400,100);
Text = "Placeholder Sample";
}
$form.Controls.AddRange(@(
($textBox1 = [ExTextBox] @{Location = [Point]::new(10,10); Hint = "Start typing!" })
($textBox2 = [ExTextBox] @{Location = [Point]::new(10,40); Hint = "Start typing!";
MultiLine = $true; Height = 50; })
))
$null = $form.ShowDialog()
$form.Dispose()
我创建我的自,定义我的文本框,我使用以下代码作为我的占位符文本:
$AssetText.Add_MouseClick({ $AssetText.text = “” })
$ErrorText.Add_MouseClick({ $ErrorText.text = “” })
$IssueText.Add_MouseClick({ $IssueText = “” })
$TestTagText.Add_MouseClick({ $TestTagText.text = “” })
$TroubleshootText.Add_MouseClick({ $TroubleshootText.text = “” })
$ResolutionText.Add_MouseClick({ $ResolutionText.text = “” })
它可以从 TextBox 中删除文本,但如果我在任何 TextBox 中键入大量文本,然后在其外部单击,然后返回,它会删除我正在处理的文本。
有没有其他我可以使用的功能比目前的方法更好用?因此,最初我可以单击 $TextBox 使文本消失,但是当我在框中写入自己的文本时,在 $TextBox 内外单击后不会消失?
如果您想以原生 OS 方式使用占位符,您可以将 EM_SETCUEBANNER
发送到 TextBox
以设置占位符文本:
using assembly System.Windows.Forms
using namespace System.Windows.Forms
using namespace System.Drawing
$code = @"
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hWnd,
int msg, IntPtr wParam, string lParam);
public const int EM_SETCUEBANER = 0x1501;
"@
$Win32Helpers = Add-Type -MemberDefinition $code -Name "Win32Helpers" -PassThru
$form = [Form] @{
ClientSize = [Point]::new(400,100);
Text = "Placeholder Text";
}
$form.Controls.AddRange(@(
($textBox1 = [TextBox] @{Location = [Point]::new(10,10) })
($textBox2 = [TextBox] @{Location = [Point]::new(10,40) })
))
$textBox1.add_HandleCreated({
$Win32Helpers::SendMessage($textBox1.Handle,$Win32Helpers::EM_SETCUEBANER`
, [IntPtr]0, "Start typing ...")
$Win32Helpers::SendMessage($textBox2.Handle,$Win32Helpers::EM_SETCUEBANER`
, [IntPtr]0, "Start typing ...")
})
$null = $form.ShowDialog()
$form.Dispose()
这是为文本框设置占位符文本的另一种方法。该方法依赖于处理 WM_PAINT
message and has been explained and implemented here.
此方法与其他方法(发送 EM_SETCUEBANNER
)的区别在于此方法也适用于多行文本框。
using assembly System.Windows.Forms
using namespace System.Windows.Forms
using namespace System.Drawing
$assemblies = "System.Windows.Forms", "System.Drawing"
$code = @"
using System.Drawing;
using System.Windows.Forms;
public class ExTextBox : TextBox
{
string hint;
public string Hint
{
get { return hint; }
set { hint = value; this.Invalidate(); }
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0xf)
{
if (!this.Focused && string.IsNullOrEmpty(this.Text)
&& !string.IsNullOrEmpty(this.Hint))
{
using (var g = this.CreateGraphics())
{
TextRenderer.DrawText(g, this.Hint, this.Font,
this.ClientRectangle, SystemColors.GrayText , this.BackColor,
TextFormatFlags.Top | TextFormatFlags.Left);
}
}
}
}
}
"@
#Add the SendMessage function as a static method of a class
Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $code -Language CSharp
# Create an instance of MyForm.
$form = [Form] @{
ClientSize = [Point]::new(400,100);
Text = "Placeholder Sample";
}
$form.Controls.AddRange(@(
($textBox1 = [ExTextBox] @{Location = [Point]::new(10,10); Hint = "Start typing!" })
($textBox2 = [ExTextBox] @{Location = [Point]::new(10,40); Hint = "Start typing!";
MultiLine = $true; Height = 50; })
))
$null = $form.ShowDialog()
$form.Dispose()