Public Class Form1 Inherits System.Windows.Forms.Form
<DllImport("user32.dll")> _ Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function
#Region " Windows フォーム デザイナで生成されたコード "
Public Sub New() MyBase.New()
' この呼び出しは Windows フォーム デザイナで必要です。 InitializeComponent()
' InitializeComponent() 呼び出しの後に初期化を追加します。
End Sub
' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub
' Windows フォーム デザイナで必要です。 Private components As System.ComponentModel.IContainer
' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。 'Windows フォーム デザイナを使って変更してください。 ' コード エディタを使って変更しないでください。 Friend WithEvents ComboBoxEX1 As WindowsApplicationCombotest.Form1.ComboBoxEX <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.components = New System.ComponentModel.Container Me.ComboBoxEX1 = New WindowsApplicationCombotest.Form1.ComboBoxEX(Me.components) Me.SuspendLayout() ' 'ComboBoxEX1 ' Me.ComboBoxEX1.Items.AddRange(New Object() {"test1", "test2", "test3", "test4", "test5"}) Me.ComboBoxEX1.Location = New System.Drawing.Point(0, 0) Me.ComboBoxEX1.Name = "ComboBoxEX1" Me.ComboBoxEX1.Size = New System.Drawing.Size(121, 20) Me.ComboBoxEX1.TabIndex = 1 Me.ComboBoxEX1.Text = "ComboBoxEX1" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.Add(Me.ComboBoxEX1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False)
End Sub
#End Region
Dim ToolTipEX1 As New WindowsApplicationCombotest.Form1.ToolTipEX
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'ToolTipEX1.Style = ToolTipEX.ttStyleEnum.TTBalloon ToolTipEX1.Style = ToolTipEX.ttStyleEnum.TTStandard
ToolTipEX1.CreateToolTip(Me.ComboBoxEX1.Handle.ToInt32) End Sub
Private Sub ComboBoxEX1_SelectListChange(ByVal sender As Object, ByVal e As SelectEventArgs) Handles ComboBoxEX1.SelectListChange If ToolTipEX1.Visible = True Then ToolTipEX1.Hide() End If ToolTipEX1.TipText = e.selectstring ToolTipEX1.Show(ComboBoxEX1.Width / 2, ComboBoxEX1.Height + 10, Me.ComboBoxEX1.Handle.ToInt32) End Sub
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed ToolTipEX1.Destroy() ToolTipEX1 = Nothing End Sub
Private Sub ComboBoxEX1_DropDownClose(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxEX1.DropDownClose If ToolTipEX1.Visible = True Then ToolTipEX1.Hide() End If End Sub
#Region "ComboBoxEX" Public Class ComboBoxEX Inherits System.Windows.Forms.ComboBox
#Region " コンポーネント デザイナで生成されたコード "
Public Sub New(ByVal Container As System.ComponentModel.IContainer) MyClass.New()
'Windows.Forms クラス作成デザイナのサポートに必要です。 Container.Add(Me) End Sub
'Component は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub
' コンポーネント デザイナで必要です。 Private components As System.ComponentModel.IContainer
' メモ : 以下のプロシージャはコンポーネント デザイナで必要です。 ' コンポーネント デザイナを使って変更してください。 ' コード エディタを使って変更しないでください。 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() components = New System.ComponentModel.Container End Sub
#End Region
Private Const WM_USER As Integer = &H400 Private Const WM_COMMAND As Integer = &H111 Private Const WM_CTLCOLORLISTBOX As Integer = &H134
Private Const OCM__BASE As Integer = WM_USER + &H1C00 Private Const OCM_COMMAND As Integer = OCM__BASE + WM_COMMAND
Private Const CBN_CLOSEUP As Integer = 8
Private Const CB_GETCURSEL As Integer = &H147 Private Const CB_GETDROPPEDSTATE As Integer = &H157
Public Delegate Sub SelectEventhandler(ByVal sender As Object, ByVal e As SelectEventArgs) Public Event SelectListChange As SelectEventhandler
Public Event DropDownClose As EventHandler
Protected Overrides Sub RefreshItem(ByVal index As Integer)
End Sub
Protected Overrides Sub SetItemsCore(ByVal items As System.Collections.IList)
End Sub
Private Function HIWORD(ByVal value As Integer) As Short Return CShort(New System.Drawing.Point(value).Y) End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) If (m.Msg = OCM_COMMAND) Then Dim code As Integer = HIWORD(m.WParam.ToInt32) If code = CBN_CLOSEUP Then RaiseEvent DropDownClose(Me, New EventArgs) End If ElseIf (m.Msg = WM_CTLCOLORLISTBOX) Then
Dim ds As Long ds = SendMessage(Me.Handle, CB_GETDROPPEDSTATE, 0, 0) If ds = 1 Then Dim Mye As SelectEventArgs = New SelectEventArgs Mye.selectindex = SendMessage(Me.Handle, CB_GETCURSEL, 0, 0)
If (Mye.selectindex <> -1) Then Mye.hwnd = m.LParam Mye.selectstring = Me.SelectedItem.ToString RaiseEvent SelectListChange(Me, Mye) End If
Private Declare Sub InitCommonControls Lib "comctl32.dll" () Private Declare Function CreateWindowEx Lib "user32.dll" Alias "CreateWindowExA" ( _ ByVal dwExStyle As Integer, _ ByVal lpClassName As String, _ ByVal lpWindowName As String, _ ByVal dwStyle As Integer, _ ByVal X As Integer, _ ByVal Y As Integer, _ ByVal nWidth As Integer, _ ByVal nHeight As Integer, _ ByVal hWndParent As Integer, _ ByVal hMenu As Integer, _ ByVal hInstance As Integer, _ ByRef lpParam As Integer) As Integer
Private Declare Function SendMessageBT Lib "user32.dll" Alias "SendMessageA" ( _ ByVal hwnd As Integer, _ ByVal wMsg As Integer, _ ByVal wParam As Integer, _ ByRef lParam As TOOLINFO) As Integer
Private Declare Function SendMessageStr Lib "user32.dll" Alias "SendMessageA" ( _ ByVal hwnd As Integer, _ ByVal wMsg As Integer, _ ByVal wParam As Integer, _ ByVal lParam As String) As Integer
Private Declare Function SendMessageLong Lib "user32.dll" Alias "SendMessageA" ( _ ByVal hwnd As Integer, _ ByVal wMsg As Integer, _ ByVal wParam As Integer, _ ByVal lParam As Integer) As Integer
Private Declare Function DestroyWindow Lib "user32.dll" ( _ ByVal hwnd As Integer) As Integer
Private Declare Function ClientToScreen Lib "user32.dll" ( _ ByVal hwnd As Integer, _ ByRef lpPoint As POINTAPI) As Integer
Private Const HWND_TOP As Long = 0 Private Const HWND_BOTTM As Long = 1 Private Const HWND_TOPMOST As Long = -1 Private Const HWND_NOTOPMOST As Long = -2
Private Const SWP_NOSIZE As Integer = &H1 Private Const SWP_NOMOVE As Integer = &H2 Private Const SWP_NOACTIVATE As Integer = &H10 Private Const SWP_SHOWWINDOW As Integer = &H40
'Windows API Constants Private Const WM_USER As Short = &H400S Private Const WM_SETFONT = &H30 Private Const CW_USEDEFAULT As Integer = &H80000000
'Windows API Types Private Structure RECT Dim left_Renamed As Integer Dim top As Integer Dim right_Renamed As Integer Dim bottom As Integer End Structure
Private Structure POINTAPI Dim X As Integer Dim Y As Integer End Structure
'Tooltip Window Constants Private Const TTS_NOPREFIX As Short = &H2S Private Const TTF_TRANSPARENT As Short = &H100S Private Const TTF_CENTERTIP As Short = &H2S Private Const TTM_ADDTOOLA As Integer = (WM_USER + 4) Private Const TTM_ACTIVATE As Integer = WM_USER + 1 Private Const TTM_UPDATETIPTEXTA As Integer = (WM_USER + 12) Private Const TTM_SETMAXTIPWIDTH As Integer = (WM_USER + 24) Private Const TTM_SETTIPBKCOLOR As Integer = (WM_USER + 19) Private Const TTM_SETTIPTEXTCOLOR As Integer = (WM_USER + 20) Private Const TTM_SETTITLE As Integer = (WM_USER + 32) Private Const TTS_BALLOON As Short = &H40S Private Const TTS_ALWAYSTIP As Short = &H1S Private Const TTF_SUBCLASS As Short = &H10S Private Const TTF_TRACK As Short = &H20S Private Const TTF_IDISHWND As Short = &H1S Private Const TTM_SETDELAYTIME As Integer = (WM_USER + 3) Private Const TTDT_AUTOPOP As Short = 2 Private Const TTDT_INITIAL As Short = 3 Private Const TTM_TRACKACTIVATE As Integer = WM_USER + 17 Private Const TTM_TRACKPOSITION As Integer = WM_USER + 18 Private Const WS_POPUP As Integer = &H80000000 Private Const WS_EX_TOPMOST As Integer = &H8
Private Const TOOLTIPS_CLASSA As String = "tooltips_class32"
''Tooltip Window Types Private Structure TOOLINFO Dim lSize As Integer Dim lFlags As Integer Dim hwnd As Integer Dim lId As Integer Dim lpRect As RECT Dim hInstance As Integer Dim lpStr As String Dim lParam As Integer End Structure
Public Enum ttStyleEnum TTStandard TTBalloon End Enum
'local variable(s) to hold property value(s) Private m_Style As ttStyleEnum Private m_TipText As String Private m_PopupOnDemand As Boolean
Private m_Visible As Boolean
'private data Private m_lTTHwnd As Integer ' hwnd of the tooltip Private m_TipFont As Font Private m_lParentHwnd As Integer ' hwnd of the window the tooltip attached to Private ti As TOOLINFO
Public Sub New() MyBase.New() InitCommonControls()
m_PopupOnDemand = False End Sub Protected Overrides Sub Finalize() Destroy() MyBase.Finalize() End Sub
'////////////////////////////////////////////////////// Public Property TipText() As String Get Return m_TipText End Get Set(ByVal Value As String) m_TipText = Value ti.lpStr = Value If m_lTTHwnd <> 0 Then SendMessageBT(m_lTTHwnd, TTM_UPDATETIPTEXTA, 0, ti) End If End Set End Property
'////////////////////////////////////////////////////// Public Property PopupOnDemand() As Boolean Get Return m_PopupOnDemand End Get Set(ByVal Value As Boolean) m_PopupOnDemand = Value 'If m_lTTHwnd <> 0 Then 'End If End Set End Property
'////////////////////////////////////////////////////// Public Property Style() As ttStyleEnum Get Style = m_Style End Get Set(ByVal Value As ttStyleEnum) m_Style = Value End Set End Property
'////////////////////////////////////////////////////// Public ReadOnly Property Visible() As Boolean Get Visible = m_Visible End Get End Property
ソースコード3/3-- 'X and Y are in Pixel so dont send vbTwips value Public Sub Show(Optional ByRef X As Integer = 0, Optional ByRef Y As Integer = 0, Optional ByRef hWndClient As Integer = 0)
Dim pt As POINTAPI Dim ptTip As Integer Dim ret As Integer
With pt .X = X .Y = Y End With
ret = ClientToScreen(hWndClient, pt)
ptTip = pt.Y * &H10000 ptTip = ptTip + pt.X
' These two messages will set the position of the tooltip: ret = SendMessageLong(m_lTTHwnd, TTM_TRACKPOSITION, 0, ptTip) ret = SendMessageBT(m_lTTHwnd, TTM_TRACKACTIVATE, True, ti)
m_Visible = True End Sub
Public Sub Hide() Dim ret As Integer Dim udtToolinfo As TOOLINFO udtToolinfo.lSize = Len(udtToolinfo) udtToolinfo.lFlags = TTF_TRACK udtToolinfo.hwnd = m_lParentHwnd udtToolinfo.lId = 1 ret = SendMessageBT(m_lTTHwnd, TTM_TRACKACTIVATE, 0, udtToolinfo)
m_Visible = False End Sub
Public Function CreateToolTip(ByVal ParentHwnd As Integer) As Boolean Dim lWinStyle As Integer If m_lTTHwnd <> 0 Then DestroyWindow(m_lTTHwnd) End If m_lParentHwnd = ParentHwnd
'create baloon style if desired If m_Style = ttStyleEnum.TTBalloon Then lWinStyle = lWinStyle Or TTS_BALLOON End If
'now set our tooltip info structure With ti 'NOTE: dont incude TTF_SUBCLASS for on demand ' if we want it centered, then set that flag If m_PopupOnDemand = False Then .lFlags = TTF_SUBCLASS Or TTF_IDISHWND Else .lFlags = TTF_IDISHWND Or TTF_TRACK Or TTF_TRANSPARENT End If
'set the hwnd prop to our parent control's hwnd .hwnd = m_lParentHwnd .lId = m_lParentHwnd '0 .hInstance = 0 'VB6.GetHInstance.ToInt32 .lpStr = m_TipText '.lpRect = lpRect .lSize = Len(ti) End With
Dim a As Integer
''add the tooltip structure a = SendMessageBT(m_lTTHwnd, TTM_ADDTOOLA, 0, ti)
End Function
Public Sub Destroy() If m_lTTHwnd <> 0 Then DestroyWindow(m_lTTHwnd) End If End Sub End Class #End Region
#Region "SelectEventArgs" '追加イベント用 Public Class SelectEventArgs Inherits EventArgs
Public selectindex As Long Public selectstring As String Public hwnd As IntPtr
エクスプローラというと‥‥ツリービューやリストビューの部分ですか? OS によって挙動が異なるとは気付きませんでした。マウスカーソル位置のものではないツールチップが表示されることもあるのですね。勉強になりました。 #あれ、でも手元の XP ではそうなってないような‥‥ Home Edition だから? それとも何か勘違いしてるのかなぁ(ーー;)
> エクスプローラというと‥‥ツリービューやリストビューの部分ですか? > OS によって挙動が異なるとは気付きませんでした。マウスカーソル位置のものではないツールチップが表示されることもあるのですね。勉強になりました。 > #あれ、でも手元の XP ではそうなってないような‥‥ Home Edition だから? それとも何か勘違いしてるのかなぁ(ーー;)
分類:[.NET]
いつも参考させていただいています。AREXと申します。
ComboBoxのドロップダウン部の一行毎に違うTooltipを表示させたいのですが、
ドロップダウン部でのマウスカーソル位置や選択行を拾うことすら出来ずにいます。
どなたかご存知でしたらご教授お願いします。