开发者

Default phantom caption in VB6 textbox

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...


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:

   

Default phantom caption in VB6 textbox


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


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


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜