在VB程序中,通过代码改变Combo控件只读属性Style值

类别:VB语言 点击:0 评论:0 推荐:

 

在VB程序中,如果你用如下语句动态创建一个Combo控件

Dim WithEvents cmbDropList As ComboBox
...
Set cmbDropList = Controls.Add("VB.ComboBox", "cmbDropList")
后,Combo控件的Style值是1 (VbComboDropDown 下拉式组合框,包括一个下拉式列表和一个文本框。可以从列表选择或在文本框中输入 ),若想把Style的值更改2 (VbComboDrop-DownList 2 下拉式列表。这种样式仅允许从下拉式列表中选择 )

通过语句Combo1.Style=2是不行的,因为Style是只读属性。为了突破这个限制,我动用的Spy++这个武器,对两种不同Style值的combo控件进行侦察,发现了两处不同

    1、combo控件的style的值为1-VbComboDropDown时,combo控件窗口的Styles=&H54010242,而combo控件的style的值为2-VbComboDrop-DownList时,combo控件窗口的Styles=&H54010243

    2、combo控件的style的值为1-VbComboDropDown时,combo控件里有一个Edit文本框窗口,而combo控件的style的值为2-VbComboDrop-DownList时,则没有Edit文本框窗口

我首先试着用API函数改变combo控件窗口的Styles值,

Call SetWindowLong(Combo1.hwnd, GWL_STYLE,&H54010243)

看Combo控件有没有什么改变,结果大失所望,

我再次试着用API函数杀死Combo控件里的那个Edit窗口,耶~~~,成功了

下面是我的实现代码:

Private Const GWL_STYLE = (-16)
Private Const GW_CHILD = 5

Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long

Const SW_HIDE = 0
Const SW_SHOW = 5

Dim WithEvents cmbDropList As ComboBox

Private Sub cmbDropList_Click()
  MsgBox cmbDropList.Text
End Sub

Private Sub Command1_Click()
  Dim ChildHwnd As Long
 
  Set cmbDropList = Controls.Add("VB.ComboBox", "cmbDropList")
 
  cmbDropList.Visible = True
  cmbDropList.AddItem "One"
  cmbDropList.AddItem "Two"
 
  ChildHwnd = GetWindow(cmbDropList.hwnd, GW_CHILD)  '取edit句柄
  Call DestroyWindow(ChildHwnd)                      'Kill edit窗口
  '改变cmbDropList的Style,这一语句可有可无~~~~,
  Call SetWindowLong(cmbDropList.hwnd, GWL_STYLE, GetWindowLong(cmbDropList.hwnd, GWL_STYLE) + 1)

End Sub

本文地址:http://com.8s8s.com/it/it6550.htm