在Windows XP中设置ComboBox提示横幅 [英] Set ComboBox cue banner in Windows XP

查看:95
本文介绍了在Windows XP中设置ComboBox提示横幅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新一个VB.net项目,该项目需要将提示横幅添加到文本框和只读组合框(DropDownStyle = DropDownList).我正在开发的机器是Windows7.我在一个提示文本中添加了一个类,该类扩展了一个组合框并添加了一个提示文本属性.这是将提示文本添加到组合框的方式:

I'm updating a VB.net project that needs to have cue banners added to text boxes and read-only comboboxes (DropDownStyle=DropDownList). The machine I'm developing on is Windows 7. I'm adding the cue text in a class that extends a combobox and adds a cue text property. This is how the cue text is added to the combobox:

 '"Me" refers to a combobox that has been extended to include a Cue Text property
 SendMessage(New HandleRef(Me, Me.Handle), CB_SETCUEBANNER, IntPtr.Zero, _cueText)

以上代码来自此博客,网址为: http://www.aaronlerch.com/blog/page/7/,位于C#中;我将其翻译为VB.我尝试了在其他地方找到的其他类似变体,所有这些都具有相同的结果:当我在Windows 7中运行该程序时,它非常适用于文本框和组合框.它仅适用于Windows XP中的文本框.

The above code is from this blog bost: http://www.aaronlerch.com/blog/page/7/, which is in C#; I translated it to VB. I tried other similar variations that I found elsewhere, all with the same result: it works great for text boxes and comboboxes when I run the program in Windows 7; it only works for text boxes in Windows XP.

我已经在不同的论坛上阅读了很多有关确保选择视觉样式并禁用东亚语言和复杂脚本的评论.我已经完成了所有这些工作,但是仍然没有在XP上运行它.

I've read lots of comments on different forums about making sure visual styles are selected and disabling east Asia languages and complex scripts. I've done all that, but still haven't gotten it to work on XP.

有人为组合框在XP上工作提供提示横幅吗?

Has anyone gotten cue banners for comboboxes to work on XP?

推荐答案

我使用各种博客和论坛帖子,创建了一个类,该类扩展了ComboBox控件并实现了可在Windows 7和XP上使用的CueText属性.我在这里找到了最相关的信息:

Using various blog and forum posts, I created a class that extends the ComboBox control and implements a CueText property that works on Windows 7 and XP. I found the most relevant pieces of information here:

  1. 如何在XP中设置提示文字: http://www.aaronlerch.com/blog/2007/12/01/watermarked-edit-controls/

简而言之,Windows 7和XP对提示横幅文本的设置略有不同,因此您需要检查程序在哪个操作系统上运行,然后适当地处理提示文本.对于XP,您需要使用 EM_SETCUEBANNER As Integer =& H1501 ,对于Windows 7,您需要将 EMB_SETCUEBANNER As Integer =& H1703 .组合框(如果该应用程序在XP上运行).您可以在下面的代码中查看详细信息.要确定正在运行的操作系统,请参阅MS KB文章304289(VB)或304283(C#).(我会发布链接,但是我没有足够的声誉点来发布两个以上.)

In a nutshell, Windows 7 and XP set the cue banner text slightly differently, so you need to check which operating system the program is running on and then handle the cue text appropriately. You need to use EM_SETCUEBANNER As Integer = &H1501 for XP and CB_SETCUEBANNER As UInteger = &H1703 for Windows 7. You also need to single out the text part of the combo box if the app is running on XP. You can see the details in the code below. To figure out which OS is running, see MS KB articles 304289 (VB) or 304283 (C#). (I would post the links, but I don't have enough reputation points to post more than two.)

一个警告是,如果组合框是只读的(DropDownStyle = DropDownList),则在XP上将不起作用.Windows 7似乎都可以正常工作.如果您的应用程序需要在XP上运行,并且您需要组合框为只读但仍显示提示文本,则可以执行以下操作:

One caveat is that this will not work on XP if the combo boxes are read only (DropDownStyle = DropDownList). Windows 7 seems to work fine either way. If your application needs to run on XP and you need the combo boxes to be read only but still display the cue text, here's what you can do:

  1. 创建一个组合框并使用默认的DropDownStyle"DropDown"
  2. 处理组合框的KeyPress事件,并在该方法中告诉它已按以下方式处理该事件: e.Handled = True.这将防止键入文本.
  3. 在设计视图中使用工具箱"创建一个空白的ContextMenuStrip,单击组合框,查看其属性,并将其ContextMenuStrip设置为刚创建的ContextMenuStrip.当用户右键单击组合框时,这将导致什么都不会发生,因此他们无法将文本粘贴到该组合框中.

这是继承ComboBox控件并添加适用于XP和7的CueText属性的类的VB代码.您唯一要做的就是弄清楚哪个OS正在运行:

Here's the VB code for a class that inherits the ComboBox control and adds a CueText property that works for XP and 7. The only thing you'll have to do is figure out which OS is running:

Imports System.ComponentModel
Imports System.Runtime.InteropServices

Public Class CueComboBox
Inherits ComboBox

' Occurs when the CueText property value changes.
Public Event CueTextChanged As EventHandler

'Windows XP
Private Shared EM_SETCUEBANNER As Integer = &H1501
'Windows 7
Private Shared CB_SETCUEBANNER As UInteger = &H1703

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal        wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As Int32
End Function

<DllImport("user32.dll")> _
Private Shared Function GetComboBoxInfo(ByVal hwnd As IntPtr, ByRef pcbi As COMBOBOXINFO) As Boolean
End Function

<StructLayout(LayoutKind.Sequential)> _
Private Structure COMBOBOXINFO
    Public cbSize As Integer
    Public rcItem As RECT
    Public rcButton As RECT
    Public stateButton As IntPtr
    Public hwndCombo As IntPtr
    Public hwndItem As IntPtr
    Public hwndList As IntPtr
End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure RECT
    Public left As Integer
    Public top As Integer
    Public right As Integer
    Public bottom As Integer
End Structure

Private Shared Function GetComboBoxInfo(ByVal control As Control) As COMBOBOXINFO
    Dim info As New COMBOBOXINFO()
    'a combobox is made up of three controls, a button, a list and textbox;
    'we want the textbox
    info.cbSize = Marshal.SizeOf(info)
    GetComboBoxInfo(control.Handle, info)
    Return info
End Function


Private _cueText As String = [String].Empty

' Gets or sets the text that will display as a cue to the user.
<Description("The text value to be displayed as a cue to the user.")> _
<Category("Appearance")> <DefaultValue("")> <Localizable(True)> _
Public Property CueText() As String
    Get
        Return _cueText
    End Get
    Set(ByVal value As String)
        If value Is Nothing Then
            value = [String].Empty
        End If

        If Not _cueText.Equals(value, StringComparison.CurrentCulture) Then
            _cueText = value
            UpdateCue()
            OnCueTextChanged(EventArgs.Empty)
        End If
    End Set
End Property

<EditorBrowsable(EditorBrowsableState.Advanced)> _
Protected Overridable Sub OnCueTextChanged(ByVal e As EventArgs)
    RaiseEvent CueTextChanged(Me, e)
End Sub


Protected Overrides Sub OnHandleCreated(ByVal e As EventArgs)
    UpdateCue()
    MyBase.OnHandleCreated(e)
End Sub


Private Sub UpdateCue()
    ' If the handle isn't yet created, this will be called when it is created
    If Me.IsHandleCreated Then
        ' Windows XP sets the cue banner differently than Windows 7
        If Form1.OPERATING_SYSTEM = "Windows XP" Then
            Dim info As COMBOBOXINFO = GetComboBoxInfo(Me)
            SendMessage(info.hwndItem, EM_SETCUEBANNER, 0, _cueText)
        Else
            SendMessage(New HandleRef(Me, Me.Handle), CB_SETCUEBANNER, IntPtr.Zero, _cueText)
        End If
    End If
End Sub

End Class

这篇关于在Windows XP中设置ComboBox提示横幅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆