Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Combo Box Height via MoveWindow 1

Status
Not open for further replies.

zemp

Programmer
Jan 27, 2002
3,301
CA
I am trying to resize the standard VB Combo box using the 'MoveWindow' API as shown in thread222-726035, thread222-757158 and thread222-438535. I have been able to use the same code to change the height on other controls (textbox, listbox) but, so far, no success with the Combo box.

A couple of questions. First is it possible with the standard Combo box and if so, what am I missing?

FYI, I am using this combo as a floating combo on a flexgrid, so it would be nice to have it fit the cell height. I know that I can use the MSForms Combobox with a picture box, but I would like to have access to the Itemdata property which is available in the standard Combo Box. Maybe it would be easier to use another property from the MSForms combobox that returns a long (.HelpContextID or data converion with the .tag property).

All thoughs welcome.

zemp
 
I guess I should have posted my code. Long day...

Code:
Private Sub Command1_Click()
   SetComboHeight Combo1, 34
End Sub

Private Sub SetComboHeight(ByRef oCombo As ComboBox, lHeight As Long)
    Dim iOrigScaleMode  As Integer
    Dim lReturn         As Long
    With oCombo
        'cache the parent's current scale mode.
        iOrigScaleMode = .Parent.ScaleMode
        'change the parent's scale mode to Pixels.
        .Parent.ScaleMode = vbPixels
        
        'Position combo, keeping it's current Left, Top, and Width positions, but changing the height.
        lReturn = MoveWindow(.hwnd, .Left, .Top, .Width, lHeight, 1)
        
        'restore the parent's scale mode to the original value.
        .Parent.ScaleMode = iOrigScaleMode
    End With
End Sub

BTW, there is no property in the MForms combo box that matches the itemdata property (one for each item in the list). I should have look more closely. Looong day....


zemp
 
This procedure is only going to affect the the drop down area of the combo box, I don't think you can change the the height of the textbox/command button portion of the combo.

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 

Actually you can change the height of a combo box via the font size property. I know that is not really what is being asked here but...

Good Luck

 
Resolved, I found a way that you can set/change the height.

Code:
Private Const CB_SETITEMHEIGHT = &H153

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub Command1_Click()
   Call SendMessage(Combo1.hwnd, CB_SETITEMHEIGHT, -1&, ByVal 13)
   Debug.Print Combo1.Height
End Sub

To change the height of the combo change the lParam parameter value to the desired height (in pixels). 1 pixel = 15 twips seems to be the conversion factor. The example above sets the Combo box height to 285 twips.

14 pixels = 300 twips
15 pixels = 315 twips
etc.



zemp
 
Just a reminder that TwipsPerPixelX and TwipsPerPixelY are variable depending (theoretically) on screen size. Although lots of Video card/monitor combinations use 15 as a fixed figure they should (and increasingly do) support proper usage.

Twips are twentieths of a point. A Point is 1/72 inch therefore there should be 1440 twips/inch. This obviously varies with screen size and resolution, but should be reflected in the TwipsPerPixel readings.

You'll see the big difference if you look at Printer.TwipsPerPixelX

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Ah - good you've discovered CB_SETITEMHEIGHT yourself, so I don't need to post :) (plus you've managed to deal the incorrect MSDN documentation that states that wParam should be 1, which caught me out when I first tried this windows message)
 
>there should be 1440 twips/inch. This obviously varies with screen size and resolution

But shouldn't, if the graphics card driver writers were less lazy (as we've remarked before)...
 
To find out the number of pixels you can use a function similar to the following,

Code:
'Twips in Pixel Y
Private Function TwipsToPixelsY(Twips As Long) As Long
   TwipsToPixelsY = Twips / Screen.TwipsPerPixelY
End Function

One other thing that I have noticed. Using this function with the Combo box and gives me a pixel value that is always 6 pixels too large (the math in the function seems correct). For example, my height measurment is 270. This returns a pixel value of 18. Using 18 as the iParam parameter (in the sendmessage call) gives the combo box a height of 360 twips (90 twips too many). Might this be because the Combo box is painted with 3D effects and the measurment is the inside height of the box? Anyone have an explanation.


zemp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top