根据新的行数,算出 ComboBox 的 List 区以 pixels 计所需的高度,然后重设之: Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, _ lpRect As RECT) As Long Private Declare Function ScreenToClientAny Lib "user32" Alias "ScreenToClient" _ (ByVal hWnd As Long, lpPoint As Any) As Long Private Declare Function MoveWindow Lib "user32" (ByVal hWnd As Long, _ ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _ ByVal nHeight As Long, ByVal bRepaint As Long) As Long' Set a new height for the list area of a ComboBox control ' ' NEWHEIGHT is measured in pixelsSub ComboBoxSetHeight(CB As ComboBox, ByVal newHeight As Long) Dim lpRect As RECT Dim wi As Long
' get combobox rectangle, relative to screen GetWindowRect CB.hWnd, lpRect wi = lpRect.Right - lpRect.Left
' convert to form's client coordinates ScreenToClientAny CB.Parent.hWnd, lpRect
' enforce the new height MoveWindow CB.hWnd, lpRect.Left, lpRect.Top, wi, newHeight, TrueEnd Sub
这个要用HOOK的方法才能实现。下面图片中的“比赛风格”那儿,是改变的下拉列表框宽度。
第二个,启用WinXP Style控件风格。这样它默认的最大高度是30项。
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, _
lpRect As RECT) As Long
Private Declare Function ScreenToClientAny Lib "user32" Alias "ScreenToClient" _
(ByVal hWnd As Long, lpPoint As Any) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hWnd As Long, _
ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _
ByVal nHeight As Long, ByVal bRepaint As Long) As Long' Set a new height for the list area of a ComboBox control
'
' NEWHEIGHT is measured in pixelsSub ComboBoxSetHeight(CB As ComboBox, ByVal newHeight As Long)
Dim lpRect As RECT
Dim wi As Long
' get combobox rectangle, relative to screen
GetWindowRect CB.hWnd, lpRect
wi = lpRect.Right - lpRect.Left
' convert to form's client coordinates
ScreenToClientAny CB.Parent.hWnd, lpRect
' enforce the new height
MoveWindow CB.hWnd, lpRect.Left, lpRect.Top, wi, newHeight, TrueEnd Sub
一是写一个manifest文件,方法自己百度;二是把实现WinXP风格的资源加到程序中。第1个方法不好:不灵活(每次需要按程序文件名更改manifest的文件名及内容)、要拖个“小尾巴”。
我都是用的第2个方法,需要用的直接加到工程中就行了,我做的这个资源文件是可以“通用”的。另外要注意:
如果启用了XP风格、你的程序想要在WinXP/Win2003中正常运行,就必须在“程序入口”调用初始化函数。
如果你的程序只会在Vista或者更高版本的系统中运行,那么可以不管这个初始化函数。简单的初始化代码如下图中所示。
如果程序是从“窗口”启动的,必须在Form_Initialize()(只要启动窗体中调用一次就行)加入。
如果是从Sub Main()启动的,那么就要在Main过程中调用。
“Call InitCommonControls”必须在任何窗体(包括用MsgBox显示的消息窗口)加载之前执行,
否则窗体加载会失败,程序直接被结束(没有任何提示)。
简单的说,让你的程序首先执行初始化函数调用就好了。
把下图”另存为“rar文件,用WinRAR打开,就可以解压出我的XP_Style.res文件了。
WinXP风格只有在编译成exe之后运行才有效,在IDE中是没有效果的。如果想在”设计时有效“,可以给VB6.exe写个manifest文件。
我曾经试过,写了manifest文件后,属性窗口中的”颜色选择“受影响,那儿是”一块白板“。