2009-06-15 15 views

答えて

1

残念ながら、答えはそれほど簡単ではありません。

ここにコード例があります:http://vbnet.mvps.org/index.html?code/comboapi/comborightalignstylebits.htmコンボボックスでリスト項目を右揃えにします。しかし、テキストのセンタリングの例はありません。

+0

最後に、私はコンボボックスよりも長いテキストを避け、それをもっと広くする=)コンボボックスは小さなものだったので、多かれ少なかれKISSの解決策を選択しました... – Johan

0

ここに私の友人がコンボボックスの内容を自動サイズ調整するために書いたコードがあります。

コード

Public Sub ComboDropDownWidth(ByRef cboThis As ComboBox, _ 
           ByVal lWidth As Long) 
    'PS 11/05/06 PUK5023 Added to automatically size a combo drop-down width to the widest entry 
    'PS 11/05/06 PUK5023 This function will custom re-size a combo drop-down width (see also ComboDropDownWidthFromContents) 
    On Error GoTo ComboDropDownWidth_Err 
    SendMessageLong cboThis.hWnd, CB_SETDROPPEDWIDTH, lWidth, 0 
Exit_Point: 
    Exit Sub 
    '' Error Handling 
ComboDropDownWidth_Err: 
    LogError "modNYFixLibrary.ComboDropDownWidth", Err.Number, Err.Description 
    GoTo Exit_Point 
    Resume 
End Sub 
Public Sub ComboDropDownWidthFromContents(ByRef cboThis As ComboBox, _ 
              Optional ByVal lMaxWidth = -1) 
    'PS 11/05/06 PUK5023 Added to automatically size a combo drop-down width to the widest entry 
    Dim i As Long 
    Dim tR As RECT 
    Dim lW As Long 
    Dim lWidth As Long 
    Dim lHDC As Long 
    On Error GoTo ComboDropDownWidthFromContents_Err 
    ' Cache the HDC of the parent form for speed: 
    lHDC = cboThis.Parent.HDC 
    ' Loop through each combo box list item & get its 
    ' width, storing the largest: 
    For i = 0 To cboThis.ListCount - 1 
     DrawText lHDC, cboThis.List(i), -1, tR, DT_CALCRECT 
     lW = tR.Right - tR.Left + 8 
     If (lW > lWidth) Then 
      lWidth = lW 
     End If 
    Next i 
    ' Don't allow width to exceed specified max 
    ' width, or the width of the screen: 
    If lMaxWidth <= 0 Then 
     lMaxWidth = Screen.Width \ Screen.TwipsPerPixelX - 16 
    End If 
    If (lWidth > lMaxWidth) Then 
     lWidth = lMaxWidth 
    End If 
    ' Combo box looks a bit strange when the 
    ' drop down portion is smaller than the 
    ' combo box itself: 
    If (lWidth < cboThis.Width \ Screen.TwipsPerPixelX) Then 
     lWidth = cboThis.Width \ Screen.TwipsPerPixelX 
     ComboDropDownWidth cboThis, lWidth 
    Else 
     'If it is longer Set the drop down width and add a little for good measure otherwise it still obscures the last character 
     ComboDropDownWidth cboThis, lWidth + 20 
    End If 
Exit_Point: 
    Exit Sub 
    '' Error Handling 
ComboDropDownWidthFromContents_Err: 
    LogError "modNYFixLibrary.ComboDropDownWidthFromContents", Err.Number, Err.Description 
    GoTo Exit_Point 
    Resume 
End Sub 
+0

誰かがこれを編集してコンテンツをフォーマットすると、VB6でフォーマットされたコードを受け入れることはできません! – MaSuGaNa

+0

私はvbAccelerator.comからリンクしているコードとよく似ています(コメントのすぐ下に!)。しかし、バグがある - 私はこのコードは、コンボボックスが親フォームとは異なるフォントを持っている場合は動作しないと思います。 – MarkJ

+0

ハ!私はコードを盗むために電子メールで仲間を喜んで告訴します! – MaSuGaNa

0

はここで自動的に内容に合わせて、コンボボックスのドロップダウン部分のサイズを設定するためにlink to VB6 codeです。

通常、vbAccelerator.comには高品質のコードがあります。興味深いことに、vbAcceleratorコードはthis answerのコードと非常によく似ています(コメントの右下)。しかし、vbAcceleratorコードは、コンボボックスが親フォームとは異なるフォントを持っているときに動作します。

関連する問題