VB6 文本框中的默认幻像标题

发布于 2024-10-30 18:26:17 字数 177 浏览 1 评论 0原文

我怎样才能在VB6.0中做到这一点...

<块引用> <块引用>

文本框中有一个默认的标题,当它为空时,比如说“在此处输入名称”。但是当用户填写时,标题将被替换...

How can I do this in VB6.0...

There's a default Caption in Textbox when it is empty, let say "Enter Name Here". but when the user fills in, the Caption will be replaced...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜吻♂芭芘 2024-11-06 18:26:19

本机 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

这需要一个清单,因此以下内容保存为“.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>

Native win32 Alternative:

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

This needs a manifest so the following saved as "<exename>.exe.manifest" (Can also use a resource)

<?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>
我三岁 2024-11-06 18:26:18

如果您希望在用户选择该框开始输入之前在文本框中使用默认值:

请使用文本框的 GotFocus() 事件并插入以下代码作为代码:txtName.text = ""

in LostFocus() 事件使用:

If txtName.text = "" Then
txtName.text = "Enter Name Here"
End If

if you want a default value in the textbox until a user selects the box to begin typing:

use the GotFocus() event for your textbox and insert the following as your code: txtName.text = ""

in the LostFocus() event use:

If txtName.text = "" Then
txtName.text = "Enter Name Here"
End If
情徒 2024-11-06 18:26:18
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
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
初见你 2024-11-06 18:26:17

Windows API 自此内置了提示横幅(或提示文本) Windows XP。 VB 6 并未直接公开它,但这并不妨碍您通过进行一些 API 调用来获取它。

与实现您自己的自定义样式相比,走这条路线有几个优点。一方面,它已经免费提供,这意味着您只需做很少的工作即可使用它。其次,它已经经过充分测试和专业打磨。第三,每当下一版本的Windows发布时,它都会自动升级。

您需要的所有代码都可以在这里找到:SendMessage:使用提示横幅提示用户

正如该页面所解释的,您需要确保您的 EXE 中包含了清单,以便您可以利用 Windows XP 主题和功能。该代码唯一真正棘手的部分是您需要确保传递 Unicode 字符串。

最终效果如下所示:

   Cue Banner example

The Windows API has had cue banners (or prompt text) built in since Windows XP. It's not directly exposed by VB 6, but that doesn't stop you from getting at it by making a few API calls.

There are several advantages of going this route, versus implementing your own custom style. For one thing, it's already available for free, meaning you have to do very little work to use it. Second, it's already been fully tested and professionally polished. Third, it will automatically get upgrades whenever the next version of Windows comes out.

All the code you need is available here: SendMessage: Use Cue Banners to Prompt Users

As the page explains, you need to make sure that you've included a manifest with your EXE so that you can take advantage of the Windows XP themes and features. The only real tricky part about the code is that you need to make sure you pass a Unicode string.

The final effect looks something like this:

   Cue Banner sample

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文