| 投稿時間:2002/09/04(Wed) 09:49 投稿者名:花ちゃん
 Eメール:
 URL :
 タイトル:Re: コンボリスト表示
 
 サンプルを作ってはいたのですが、UPしていなかったので。
 Option Explicit     'SampleNo=167 WindowsXP VB6.0(SP5) 2002.07.15
 '指定のウインドウにメッセージを送る(P750)
 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 Const CB_SETDROPPEDWIDTH = &H160
 Private StrHei As Long      'テキストの高さ
 Private StrWid As Long      'テキストの幅
 
 Private Sub Form_Load()
 Dim i            As Integer
 Dim j            As Integer
 Dim strLen       As Integer
 Dim n1           As Integer
 Dim myStr        As String
 Dim myData       As String
 Dim lngRet       As Long
 Dim MyString     As String
 myData = "12345ABCDEabcdeあいうえおアイウエオ子牛寅卯竜"
 Combo1.Clear
 For i = 1 To 10
 Randomize
 'ランダムな長さのランダムな文字列を作成
 strLen = CInt(20 * Rnd + 10)  '5 〜30 文字
 myStr = ""
 For j = 1 To strLen
 myStr = myStr & Mid$(myData, CInt(30 * Rnd + 1), 1)
 Next j
 Combo1.AddItem myStr
 '一番長い文字列を取得
 If n1 < LenB(StrConv(myStr, vbFromUnicode)) Then
 n1 = LenB(StrConv(myStr, vbFromUnicode))
 MyString = myStr
 End If
 Next i
 '一番長い文字列の幅を取得
 Call sMyTextSize(Combo1, MyString)
 'その幅をピクセル単位に変換し、スクロールバー分をプラス
 StrWid = (StrWid \ Screen.TwipsPerPixelX) + 30
 'リスト部の幅を文字列の長さに合せて設定
 lngRet = Sendmessage(Combo1.hWnd, CB_SETDROPPEDWIDTH, StrWid, ByVal 0&)
 End Sub
 Private Sub sMyTextSize(MyControl As Control, MyText As String)
 '文字列の幅を取得
 Dim MeFoName As String
 Dim MeFoSize As Integer
 Dim MeFoBold As Boolean
 'フォームのフォント設定値を取得
 With Me
 MeFoName = Me.FontName
 MeFoSize = Me.FontSize
 MeFoBold = Me.FontBold
 End With
 'コントロールのフォント設定値をフォームに設定
 With Me
 .FontName = MyControl.FontName
 .FontSize = MyControl.FontSize
 .FontBold = MyControl.FontBold
 StrHei = .TextHeight(MyText)    '文字高さを取得
 StrWid = .TextWidth(MyText)     '文字列幅を取得
 End With
 'フォームのフォント設定値を元に戻す
 With Me
 Me.FontName = MeFoName
 Me.FontSize = MeFoSize
 Me.FontBold = MeFoBold
 End With
 End Sub
 
 |