|
5#
楼主 |
发表于 2015-3-16 14:18:59
|
只看该作者
第三部分 TabControl的实现代码
本帖最后由 站到终点站 于 2015-3-17 11:40 编辑
- Private Sub Form_Load()
- mMouseLeaveBackColor = RGB(155, 155, 155) '鼠标移开后显示的背景色
- mMouseOnBackColor = RGB(0, 255, 255) '鼠标在其上显示的背景色
- mSelectedBackColor = RGB(255, 0, 0) '当前项被选中时的背景色
-
- mMouseLeaveFontColor = RGB(255, 255, 255) '鼠标移开后显示的前景色
- mMouseOnFontColor = RGB(0, 0, 255) '鼠标在其上显示的前景色
- mSelectedFontColor = RGB(0, 0, 0) '当前项被选中时的前景色
- mMouseLeaveBorderColor = RGB(155, 155, 155) '鼠标移开后显示的边框色
- mMouseOnBorderColor = RGB(155, 155, 155) '鼠标在其上显示的边框色
- mSelectedBorderColor = RGB(155, 155, 155) '当前项被选中时的边框色
-
- mFormHeaderHwnd = FindWindowEx(Me.Hwnd, 0&, "OFormSub", vbNullString)
- mFormMainHwnd = FindWindowEx(Me.Hwnd, mFormHeaderHwnd, "OFormSub", vbNullString)
- mMainDC = GetWindowDC(mFormMainHwnd)
-
- SetBkMode mMainDC, TRANSPARENT
-
- Me.InsideHeight = 30 * TwipsPerPixelY
- For I = 1 To 3
- AddTabBar "", "Tab" & I, ""
- Next
- 'ReDraw
- End Sub
- Private Sub Form_Unload(Cancel As Integer)
- ReleaseDC mFormMainHwnd, mMainDC
- Set mTabBars = Nothing
- End Sub
- Private Sub 主体_Click()
- Dim intX As Integer
- Dim mCurrentSelected As Integer
-
- For intX = 1 To mTabBars.count
- If mMousePoint.x >= mTabBars(intX).Left And mMousePoint.x <= mTabBars(intX).Right And _
- mMousePoint.y >= mTabBars(intX).Top And mMousePoint.y <= mTabBars(intX).Bottom Then
- mTabBars(intX).Selected = True
- ReDrawTabBar intX
- mCurrentSelected = intX
- Exit For
- End If
- 'Debug.Print "(x,y):(" & pX & "," & py & ")" & vbTab & "(Rx):(" & mTabBars(intX).Left & ")"
- Next
- If mPreTabBarSelected <> mCurrentSelected Then
- If mPreTabBarSelected > 0 And mCurrentSelected > 0 Then
- mTabBars(mPreTabBarSelected).Selected = False
- ReDrawTabBar mPreTabBarSelected
- End If
- mPreTabBarSelected = mCurrentSelected
- RaiseEvent TabClick(mCurrentSelected)
- End If
- End Sub
- Private Sub 主体_DblClick(Cancel As Integer)
- 'Static dblClickCount As Long
- 'dblClickCount = dblClickCount + 1
- 'AddTabBar "", "双击添加Tab" & dblClickCount, ""
- 'ReDraw
- End Sub
- Private Sub 主体_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
- Dim intX As Integer
- Dim pX As Long, pY As Long
- Dim mCurrentOn As Integer
-
- pX = x / TwipsPerPixelX()
- pY = y / TwipsPerPixelY()
-
- For intX = 1 To mTabBars.count
- If pX >= mTabBars(intX).Left And pX <= mTabBars(intX).Right And _
- pY >= mTabBars(intX).Top And pY <= mTabBars(intX).Bottom Then
- mTabBars(intX).IsMouseOn = True
- ReDrawTabBar intX
- mCurrentOn = intX
- Exit For
- End If
- 'Debug.Print "(x,y):(" & pX & "," & py & ")" & vbTab & "(Rx):(" & mTabBars(intX).Left & ")"
- Next
- If mPreTabBarOn <> mCurrentOn Then
- If mPreTabBarOn > 0 Then
- mTabBars(mPreTabBarOn).IsMouseOn = False
- ReDrawTabBar mPreTabBarOn
- End If
- If mCurrentOn > 0 Then
- mPreTabBarOn = mCurrentOn
- Else
- mPreTabBarOn = 0
- End If
- End If
- 'ReDraw
- mMousePoint.x = pX
- mMousePoint.y = pY
- End Sub
- Private Sub 主体_Paint()
- ReDraw
- End Sub
- Public Property Get TabCount() As Long
- TabCount = mTabBars.count
- End Property
- Public Sub AddTabBar(ByVal Key As String, ByVal Text As String, ByVal TargetForm As String)
- Dim mTabBar As New clsAccTabBar
- Dim lngText As Long
- Dim mTextSize As Size
-
- lngText = LenB(StrConv(Text, vbFromUnicode))
- GetTextExtentPoint32 mMainDC, Text, lngText, mTextSize
- If TabCount = 0 Then
- mTabBar.Left = 0
- mTabBar.Top = 0
- mTabBar.Right = mTextSize.cx + 16
- mTabBar.Bottom = 30
- Else
- mTabBar.Left = mTabBars(TabCount).Right + 0.6
- mTabBar.Top = 0
- mTabBar.Right = mTabBar.Left + mTextSize.cx + 16
- mTabBar.Bottom = 30
- End If
- mTabBar.Text = Text
- mTabBars.Add mTabBar
- ReDrawTabBar mTabBars.count
- End Sub
- Public Sub RemoveTabBar()
- On Error GoTo Err_Handle
- 'Dim FormDrawer As New clsAccGDI
- Dim mRect As Rect
- Dim mLastIndex As Integer
-
- mLastIndex = mTabBars.count
-
- mRect.Left = mTabBars(mLastIndex).Left
- mRect.Right = mTabBars(mLastIndex).Right
- mRect.Bottom = mTabBars(mLastIndex).Bottom
- mRect.Top = mTabBars(mLastIndex).Top
- FillTargetRect RGB(255, 255, 255), mRect
- If mTabBars(mLastIndex).Selected Then mPreTabBarSelected = 0
- 'rt = DrawText(mMainDC, mTabBars(Index).Text, LenB(StrConv(mTabBars(Index).Text, vbFromUnicode)), mRect, DT_CENTER Or DT_VCENTER Or DT_SINGLELINE)
- mTabBars.Remove mLastIndex
- GoTo Exit_Sub
- Err_Handle:
- MsgBox "出错!"
- Exit_Sub:
- 'Set FormDrawer = Nothing
- End Sub
- Public Sub ReDraw()
- On Error GoTo Err_Handle
- 'Dim FormDrawer As New clsAccGDI
- Dim mRect As Rect
- Dim mTabCount As Long
-
- mTabCount = TabCount
-
- For I = 1 To mTabCount
- mRect.Left = mTabBars(I).Left
- mRect.Right = mTabBars(I).Right
- mRect.Bottom = mTabBars(I).Bottom
- mRect.Top = mTabBars(I).Top
- If mTabBars(I).Selected Then
- FillTargetRect RGB(255, 0, 0), mRect
- Else
- If mTabBars(I).IsMouseOn Then
- FillTargetRect RGB(0, 255, 0), mRect
- Else
- FillTargetRect RGB(255, 255, 0), mRect
- End If
- End If
- rt = DrawText(mMainDC, mTabBars(I).Text, LenB(StrConv(mTabBars(I).Text, vbFromUnicode)), mRect, DT_CENTER Or DT_VCENTER Or DT_SINGLELINE)
- Next
- GoTo Exit_Sub
- Err_Handle:
- MsgBox "出错!"
- Exit_Sub:
- 'Set FormDrawer = Nothing
- End Sub
- Public Sub ReDrawTabBar(Index As Integer)
- On Error GoTo Err_Handle
- 'Dim FormDrawer As New clsAccGDI
- Dim mRect As Rect
-
- mRect.Left = mTabBars(Index).Left
- mRect.Right = mTabBars(Index).Right
- mRect.Bottom = mTabBars(Index).Bottom
- mRect.Top = mTabBars(Index).Top
- If mTabBars(Index).Selected Then
- FillTargetRect RGB(255, 0, 0), mRect
- Else
- If mTabBars(Index).IsMouseOn Then
- FillTargetRect RGB(0, 255, 0), mRect
- Else
- FillTargetRect RGB(255, 255, 0), mRect
- End If
- End If
- rt = DrawText(mMainDC, mTabBars(Index).Text, LenB(StrConv(mTabBars(Index).Text, vbFromUnicode)), mRect, DT_CENTER Or DT_VCENTER Or DT_SINGLELINE)
- GoTo Exit_Sub
- Err_Handle:
- MsgBox "出错!"
- Exit_Sub:
- 'Set FormDrawer = Nothing
- End Sub
- Private Sub FillTargetRect(DrawColor As OLE_COLOR, TargetRect As Rect)
- Dim hOldBrush As Long
- Dim hBrush As Long
-
- hBrush = CreateSolidBrush(CLng(DrawColor))
- hOldBrush = SelectObject(mMainDC, hBrush)
- FillRect mMainDC, TargetRect, hBrush
- 'Rectangle hdc, TargetRect.Left, TargetRect.Top, TargetRect.Right, TargetRect.Bottom
- SelectObject mMainDC, hOldBrush
- DeleteObject hBrush
- End Sub
复制代码
|
|