Option Explicit
Private Sub Form_Unload(Cancel As Integer)
If IsHooked Then
Unhook ' Stop checking messages.
End If
End Sub
Private Sub MSFlexGrid1_MouseUp(Button As Integer, _
Shift As Integer, x As Single, y As Single)
Static CurrentWidth As Single
' Check to see if the Cell's width has changed.
If MSFlexGrid1.CellWidth <> CurrentWidth Then
Combo1.Width = MSFlexGrid1.CellWidth
CurrentWidth = MSFlexGrid1.CellWidth
End If
End Sub
Private Sub Form_Load()
gHW = MSFlexGrid1.hWnd
Hook ' Start checking messages.
MSFlexGrid1.AllowUserResizing = flexResizeColumns
MSFlexGrid1.Cols = 4
MSFlexGrid1.Rows = 6
MSFlexGrid1.RowHeightMin = Combo1.Height
Combo1.Visible = False
Combo1.ZOrder (0)
Combo1.Width = MSFlexGrid1.CellWidth
' Load the ComboBox's list.
Combo1.AddItem "Some text"
Combo1.AddItem "Some more text"
Combo1.AddItem "Still more text"
Combo1.AddItem "Yet even more text"
Combo1.AddItem "Way more text than that"
End Sub
Private Sub MSFlexGrid1_Click()
' Position and size the ComboBox, then show it.
Combo1.Width = MSFlexGrid1.CellWidth
Combo1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
Combo1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
Combo1.Text = MSFlexGrid1.Text
Combo1.Visible = True
End Sub
Private Sub Combo1_Click()
' Place the selected item into the Cell and hide the ComboBox.
MSFlexGrid1.Text = Combo1.Text
Combo1.Visible = False
End Sub
On the Project menu, add a new Module and insert the following code:
Option Explicit
Declare Function CallWindowProc Lib "user32" Alias _
"CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias _
"SetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_WNDPROC = -4
Private IsHooked As Boolean
Private Const WM_SIZE = &H5
Private Const WM_PAINT = &HF
Private lpPrevWndProc As Long
Public gHW As Long
Public Sub Hook()
If IsHooked Then
' Do not hook it twice without unhooking,
' or you will not be able to unhook it.
Else
lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, _
AddressOf WindowProc)
IsHooked = True
End If
End Sub
Public Sub Unhook()
Dim temp As Long
temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
IsHooked = False
End Sub
Function WindowProc(ByVal hw As Long, ByVal uMsg As _
Long, ByVal wParam As Long, ByVal lParam As Long) As Long
WindowProc = CallWindowProc(lpPrevWndProc, hw, _
uMsg, wParam, lParam)
' The interior of the control is repainted, but not resized.
If uMsg = WM_SIZE Or uMsg = WM_PAINT Then
Form1.Combo1.Width = Form1.MSFlexGrid1.CellWidth
Form1.Combo1.Left = Form1.MSFlexGrid1.CellLeft + _
Form1.MSFlexGrid1.Left
Form1.Combo1.Top = Form1.MSFlexGrid1.CellTop + _
Form1.MSFlexGrid1.Top
End If
End Function