如何在VB6.0中执行此操作...
文本框中的默认标题为空时,请说“在此输入名称”。但是当用户填写时,标题将被替换......
答案 0 :(得分:7)
自Windows XP以来,Windows API内置了cue banners(或提示文本)。它不是由VB 6直接公开的,但这并不能阻止你通过几个API调用来实现它。
与实现自己的自定义样式相比,使用此路线有几个优点。首先,它已经免费提供,这意味着你必须做很少的工作才能使用它。其次,它已经过全面测试和专业抛光。第三,每当下一版本的Windows出现时,它都会自动升级。
您需要的所有代码均可在此处找到: SendMessage: Use Cue Banners to Prompt Users
如页面所述,您需要确保已在EXE中包含清单,以便利用Windows XP主题和功能。关于代码的唯一真正棘手的部分是你需要确保传递 Unicode 字符串。
最终效果如下所示:
答案 1 :(得分:2)
如果您想要文本框中的默认值,直到用户选择开始输入的框:
为您的文本框使用GotFocus()事件并插入以下代码:txtName.text = ""
If txtName.text = "" Then
txtName.text = "Enter Name Here"
End If
答案 2 :(得分:0)
Private Sub Text1_Change()
If Trim(Text1.Text) = "" Then
Label1.Caption = "Enter Name Here"
Else
Label1.Caption = ""
End If
End Sub
Private Sub Text1_Click()
Label1.Caption = "Enter Name Here"
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If Trim(Text1.Text) = "" Then
Label1.Caption = "Enter Name Here"
Else
Label1.Caption = ""
End If
End Sub
答案 3 :(得分:0)
原生win32替代方案:
Private Const ECM_FIRST As Long = &H1500
Private Const EM_SETCUEBANNER As Long = (ECM_FIRST + 1)
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function InitCommonControls Lib "comctl32" () As Long
Private Sub Form_Initialize()
InitCommonControls
End Sub
Private Sub Form_Load()
Dim sCueCaption As String
sCueCaption = StrConv("Enter Name Here", vbUnicode)
Call SendMessage(Text1.hwnd, EM_SETCUEBANNER, 0&, ByVal sCueCaption)
End Sub
这需要一个清单,因此以下内容保存为“<exename>.exe.manifest
”(也可以使用资源)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly
xmlns="urn:schemas-microsoft-com:asm.v1"
manifestVersion="1.0">
<assemblyIdentity
type="win32"
processorArchitecture="*"
version="6.0.0.0"
name="test"
/>
<description>Enter your Description Here</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
language="*"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
/>
</dependentAssembly>
</dependency>
</assembly>