可以修改Autocomplete高度和宽度的TextBox.(ComboBox也试用)

Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Text

Public Class TextBoxEx
    Inherits TextBox
    Private acDropDownHeight As Integer = 106
    Private acDropDownWidth As Integer = 170

    ‘<EditorBrowsable(EditorBrowsableState.Always), _  

    <Browsable(True), Description("The width, in pixels, of the auto complete drop down box"), DefaultValue(170)> _
    Public Property AutoCompleteDropDownWidth() As Integer
        Get
            Return acDropDownWidth
        End Get

        Set(value As Integer)
            acDropDownWidth = value
        End Set
    End Property

    ‘<EditorBrowsable(EditorBrowsableState.Always), _  

    <Browsable(True), Description("The height, in pixels, of the auto complete drop down box"), DefaultValue(106)> _
    Public Property AutoCompleteDropDownHeight() As Integer
        Get
            Return acDropDownHeight
        End Get

        Set(value As Integer)
            acDropDownHeight = value
        End Set
    End Property

    Protected Overrides Sub OnHandleCreated(e As EventArgs)
        MyBase.OnHandleCreated(e)

        ACWindow.RegisterOwner(Me)
    End Sub

    ‘‘‘ <summary>
    ‘‘‘ Provides an encapsulation of an Auto complete drop down window
    ‘‘‘ handle and window proc.
    ‘‘‘ </summary>
    Private Class ACWindow
        Inherits NativeWindow
        Private Shared ReadOnly ACWindows As Dictionary(Of IntPtr, ACWindow)

        Private Const WM_WINDOWPOSCHANGED As UInt32 = &H47

        Private Const WM_NCDESTROY As UInt32 = &H82

        Private Const SWP_NOSIZE As UInt32 = &H1

        Private Const SWP_NOMOVE As UInt32 = &H2

        Private Const SWP_NOZORDER As UInt32 = &H4

        Private Const SWP_NOREDRAW As UInt32 = &H8

        Private Const SWP_NOACTIVATE As UInt32 = &H10
        Private Const WM_SIZING As UInt32 = &H214

        Private Const GA_ROOT As UInt32 = 2
        Private Shared ReadOnly owners As List(Of TextBoxEx)

        <DllImport("user32.dll")> _
        Private Shared Function EnumThreadWindows(dwThreadId As Integer, lpfn As EnumThreadDelegate, lParam As IntPtr) As Boolean
        End Function

        <DllImport("user32.dll")> _
        Private Shared Function GetAncestor(hWnd As IntPtr, gaFlags As UInt32) As IntPtr
        End Function

        <DllImport("kernel32.dll")> _
        Private Shared Function GetCurrentThreadId() As Integer
        End Function

        <DllImport("user32.dll")> _
        Private Shared Sub GetClassName(hWnd As IntPtr, lpClassName As StringBuilder, nMaxCount As Integer)
        End Sub

        <DllImport("user32.dll")> _
        Private Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr, X As Integer, Y As Integer, cx As Integer, cy As Integer, _
            uFlags As UInteger) As Boolean
        End Function

        <DllImport("user32.dll")> _
        Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
        End Function

        Private Delegate Function EnumThreadDelegate(hWnd As IntPtr, lParam As IntPtr) As Boolean

        <StructLayout(LayoutKind.Sequential)> _
        Private Structure RECT
            Public ReadOnly Left As Integer

            Public ReadOnly Top As Integer

            Public ReadOnly Right As Integer

            Public ReadOnly Bottom As Integer

            Public ReadOnly Property Location() As Point
                Get
                    Return New Point(Left, Top)
                End Get
            End Property
        End Structure

        Private owner As TextBoxEx

        Shared Sub New()
            ACWindows = New Dictionary(Of IntPtr, ACWindow)()

            owners = New List(Of TextBoxEx)()
        End Sub

        ‘‘‘ <summary>
        ‘‘‘ Creates a new ACWindow instance from a specific window handle.
        ‘‘‘ </summary>
        Private Sub New(handle As IntPtr)
            AssignHandle(handle)
        End Sub

        ‘‘‘ <summary>
        ‘‘‘ Registers a ComboBoxEx for adjusting the Complete Dropdown window size.
        ‘‘‘ </summary>
        Public Shared Sub RegisterOwner(owner As TextBoxEx)
            If (owners.Contains(owner)) Then
                Return
            End If

            owners.Add(owner)

            EnumThreadWindows(GetCurrentThreadId(), AddressOf EnumThreadWindowCallback, IntPtr.Zero)
        End Sub

        ‘‘‘ <summary>
        ‘‘‘ This callback will receive the handle for each window that is
        ‘‘‘ associated with the current thread. Here we match the drop down window name
        ‘‘‘ to the drop down window name and assign the top window to the collection
        ‘‘‘ of auto complete windows.
        ‘‘‘ </summary>
        Private Shared Function EnumThreadWindowCallback(hWnd As IntPtr, lParam As IntPtr) As Boolean
            If (GetClassName(hWnd) = "Auto-Suggest Dropdown") Then
                Dim handle As IntPtr = GetAncestor(hWnd, GA_ROOT)

                If (Not ACWindows.ContainsKey(handle)) Then
                    ACWindows.Add(handle, New ACWindow(handle))
                End If
            End If

            Return True
        End Function

        ‘‘‘ <summary>
        ‘‘‘ Gets the class name for a specific window handle.
        ‘‘‘ </summary>
        Private Shared Function GetClassName(hRef As IntPtr) As String
            Dim lpClassName = New StringBuilder(256)

            GetClassName(hRef, lpClassName, 256)

            Return lpClassName.ToString()
        End Function

        ‘‘‘ <summary>
        ‘‘‘ Overrides the NativeWindow‘s WndProc to handle when the window
        ‘‘‘ attributes changes.
        ‘‘‘ </summary>
        Protected Overrides Sub WndProc(ByRef m As Message)
            ‘If m.Msg = WM_SIZING Then
            ‘    If owner IsNot Nothing Then
            ‘        Dim rr As RECT = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(RECT)), RECT)
            ‘        owner.acDropDownWidth = (rr.Right - rr.Left)
            ‘        owner.acDropDownHeight = (rr.Bottom - rr.Top)
            ‘    End If
            ‘End If
            Console.WriteLine(m.Msg.ToString())
            If (m.Msg = WM_WINDOWPOSCHANGED) Then
                ‘ If the owner has not been set we need to find the ComboBoxEx that  

                ‘ is associated with this dropdown window. We do it by checking if  

                ‘ the upper-left location of the drop-down window is within the   

                ‘ ComboxEx client rectangle.   

                If (owner Is Nothing) Then
                    Dim ownerRect As Rectangle = Nothing

                    Dim acRect = New RECT()

                    For Each cbo As TextBoxEx In owners
                        GetWindowRect(Handle, acRect)

                        ownerRect = cbo.RectangleToScreen(cbo.ClientRectangle)

                        ‘If (ownerRect.Contains(acRect.Location)) Then
                        owner = cbo

                        ‘ TODO: might not be correct. Was : Exit For
                        ‘Exit For
                        ‘ End If
                    Next

                    owners.Remove(owner)
                End If

                If ((owner IsNot Nothing)) Then
                    SetWindowPos(Handle, IntPtr.Zero, -5, 0, owner.AutoCompleteDropDownWidth, owner.AutoCompleteDropDownHeight, _
                        SWP_NOMOVE Or SWP_NOZORDER Or SWP_NOACTIVATE)
                End If
            End If

            If (m.Msg = WM_NCDESTROY) Then
                ACWindows.Remove(Handle)
            End If

            MyBase.WndProc(m)
        End Sub
    End Class

End Class
时间: 2024-10-18 21:41:01

可以修改Autocomplete高度和宽度的TextBox.(ComboBox也试用)的相关文章

BPM实例分享:如何设置开窗高度与宽度?

版本: V9.2.7 问题描述: DropDown的开窗查询中高度与宽度设置无效 解决方案: 修改MvcSheetAll,具体修改文件位置在: /Portal/WFRes/_Scripts/MVCSheet/Controls/SheetTextBox.js 修改内容如下图: 备注: H3中所有项目文件属性都默认为只读,打开长文本MvcSheetAll.js 时需更改后才能文本编辑格式化,至于压缩问题,需代码格式化.

Excel随着表格内容自动调整高度或宽度

Excel随着表格内容自动调整高度或宽度 在使用excel的时候,有时我们需要根据输入内容的多少调整表格的高度和宽度,如何让EXCEL自动调整呢.因本人目前实用的Office为2013版本,因此暂时先介绍此版本实现方法,其他2003版EXCEL.WPS版EXCEL的设置也大致相同,有时间再补上.2013版本实现方法如写下: 1.实现自动调整宽度.选中全部表格(Ctrl+A),如图选择开始--格式--自动调整列宽即可.另附上设置前后的效果图. 2.实现自动调整高度.分两步设置,首先选中全部表格(C

(iOS)修改UITextField高度

修改UITextField高度 === --- ## 是否可以通过修改frame改变高度 网上流传的代码中使用如下一份代码: //    以下代码任然不能改变UITextField高度 CGRect rect = _userNameField.bounds; rect.size.height = 88; rect.size.width = 20; _userNameField.bounds = rect; 所以不可以. --- ## 通过自定义子类实现修改UITextField高度 在子类中覆盖

如何让高度、宽度不定的容器保持水平、垂直居中

这个题目似乎解决的办法很多,JS是最能够确保各种浏览器中一致性的,但是仍然可以使用CSS的方式来解决.这个问题分解为两个方面,第一解决左右居中的问题,第二解决上下居中的问题. 1.左右居中. 左右居中最为简单,使用 text-align:center; 就可以让绝大多数的对象居中对齐,并且这个属性也获得了几乎全部浏览器的支持.实际上,这个属性定义的是块级对象内部文字的对齐方式,内部的文字或者图像一般是内联对象. 也许有人会提到,为什么不用 margin:0 auto;这个办法呢?这是一个好问题,

CSS中的高度和宽度

1.简单的说,常规流向的块级元素,width为auto时,会尽量充满父元素的内容宽度,而height为auto时,则是由其内部的不浮动的子元素的高度决定(无浮动,绝对定位). 2.width:100%;width:auto <div>    <p>1</p></div><style type="text/css">div{ width:600px; overflow:auto;}p{ width:100%;      padd

Activity中获取view的高度和宽度为0的原因以及解决方案

在activity中可以调用View.getWidth.View.getHeight().View.getMeasuredWidth() .View.getgetMeasuredHeight()来获得某个view的宽度或高度,但是在onCreate().onStrart().onResume()方法中会返回0,这是应为当前activity所代表的界面还没显示出来没有添加到WindowPhone的DecorView上或要获取的view没有被添加到DecorView上或者该View的visibili

兼容的可视区高度和宽度

function viewWidth(){ return window.innerWidth || document.documentElement.clientWidth; } function viewHeight(){ return window.innerHeight || document.documentElement.clientHeight; } document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight =

as.3通过SWF元数据控制舞台的高度与宽度

package { import fl.controls.Label; import fl.controls.TextInput; import flash.display.Sprite; import flash.events.Event; [SWF(backgroundColor='0x00FF00',width='400',height='400',frameRate='29')] public class Main extends Sprite  { private var userNa

js 获取浏览器高度和宽度值

js 获取浏览器高度和宽度值 IE中: document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.documentElement.clientWidth ==> 可见区域宽度 document.documentElement.clientHeight ==> 可见区域高度 FireFox中: document.body.clientWidth ==> BODY